java实现 excel 打印 问题
<%@pagelanguage="java"import="java.util.*"pageEncoding="gb2312"%><%Stringpath=request...
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<title>WEB页面导出为EXCEL文档的方法
</title>
</HEAD>
<body>
<BR>
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
<TR style="text-align : center;">
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
<TR style="text-align : center;">
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;">单元格合并行B</TD>
</TR>
</TABLE>
<br><br><br>
<input type="button" onclick="javascript:AllAreaWord();" value="导出页面指定区域内容到Word">
<input type="button" onclick="javascript:AllAreaExcel();" value="导出页面指定区域内容到Excel">
<input type="button" onclick="javascript:CellAreaExcel();" value="导出表单单元格内容到Excel">
<SCRIPT LANGUAGE="javascript">
//指定页面区域内容导入Excel
function AllAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
//指定页面区域“单元格”内容导入Excel
function CellAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = PrintA.rows.length;
for (i=0;i<Lenr;i++)
{
var Lenc = PrintA.rows(i).cells.length;
for (j=0;j<Lenc;j++)
{
oSheet.Cells(i+1,j+1).value = PrintA.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
}
//指定页面区域内容导入Word
function AllAreaWord()
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(PrintB);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
//window.close();
}
</SCRIPT>
</body>
</html> 展开
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<title>WEB页面导出为EXCEL文档的方法
</title>
</HEAD>
<body>
<BR>
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
<TR style="text-align : center;">
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
<TR style="text-align : center;">
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;">单元格合并行B</TD>
</TR>
</TABLE>
<br><br><br>
<input type="button" onclick="javascript:AllAreaWord();" value="导出页面指定区域内容到Word">
<input type="button" onclick="javascript:AllAreaExcel();" value="导出页面指定区域内容到Excel">
<input type="button" onclick="javascript:CellAreaExcel();" value="导出表单单元格内容到Excel">
<SCRIPT LANGUAGE="javascript">
//指定页面区域内容导入Excel
function AllAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
//指定页面区域“单元格”内容导入Excel
function CellAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = PrintA.rows.length;
for (i=0;i<Lenr;i++)
{
var Lenc = PrintA.rows(i).cells.length;
for (j=0;j<Lenc;j++)
{
oSheet.Cells(i+1,j+1).value = PrintA.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
}
//指定页面区域内容导入Word
function AllAreaWord()
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(PrintB);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
//window.close();
}
</SCRIPT>
</body>
</html> 展开
2个回答
展开全部
不用这么麻烦的
直接用TABLE就行了,就是在JSP头文件要稍微改一下,我都是这么弄的
我给你看个我做的粗糙点,大致意思有了
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" import="java.util.Vector" errorPage="" %>
<%
response.setContentType("application/vnd.ms-excel;charset=gb2312");
response.setHeader("Content-disposition","inline; filename=ywl.xls");
%>
<html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>导出EXCEL表格</title>
</head>
<body>
<h1>业务所有数据</h1>
<%Connection con= null;
Statement stmt= null;
ResultSet rs = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
ResultSet rs4 = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/xnwxt?user=root&password=87601401&useUnicode=true&characterEncoding=GB2312";
con =DriverManager.getConnection(url);
stmt=con.createStatement();
String select3="select * from xnwxt order by id desc ";
////////////////////////////////////////////////
rs3=stmt.executeQuery(select3);
out.print("<table align=center >");
out.print("<tr>");
out.print("<td width=80><strong><div align=center >");
out.print( "上报时间") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "上报渠道") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "上报人") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "号码1") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "姓名") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "号码2") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 类型") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 名称") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 编号") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 包年") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 处理") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 结算") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "备注") ;
out.print("</div></strong></td>");
out.print("</tr>");
while(rs3.next()){
out.print("<tr align=center>");
out.print("<td width=80>");out.print(new String(rs3.getString("time").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("channel").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("truename").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("mobile").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("guest").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("idcard").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("type").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("group0").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("groupid").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("bao").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("treatment").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("gratuity").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("remark").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("</tr>");
}
rs3.close();
con.close();
stmt.close();
}catch(SQLException sqle){
out.println("sqle="+sqle);
}finally{
try{
if(con!=null){
con.close();
}
}catch (SQLException sqle){
out.println("sqle="+sqle);
}
}
%>
</body>
</html>
直接用TABLE就行了,就是在JSP头文件要稍微改一下,我都是这么弄的
我给你看个我做的粗糙点,大致意思有了
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" import="java.util.Vector" errorPage="" %>
<%
response.setContentType("application/vnd.ms-excel;charset=gb2312");
response.setHeader("Content-disposition","inline; filename=ywl.xls");
%>
<html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>导出EXCEL表格</title>
</head>
<body>
<h1>业务所有数据</h1>
<%Connection con= null;
Statement stmt= null;
ResultSet rs = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
ResultSet rs4 = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/xnwxt?user=root&password=87601401&useUnicode=true&characterEncoding=GB2312";
con =DriverManager.getConnection(url);
stmt=con.createStatement();
String select3="select * from xnwxt order by id desc ";
////////////////////////////////////////////////
rs3=stmt.executeQuery(select3);
out.print("<table align=center >");
out.print("<tr>");
out.print("<td width=80><strong><div align=center >");
out.print( "上报时间") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "上报渠道") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "上报人") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "号码1") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "姓名") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "号码2") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 类型") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 名称") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 编号") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 包年") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 处理") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( " 结算") ;
out.print("</div></strong></td>");
out.print("<td width=80><strong><div align=center >");
out.print( "备注") ;
out.print("</div></strong></td>");
out.print("</tr>");
while(rs3.next()){
out.print("<tr align=center>");
out.print("<td width=80>");out.print(new String(rs3.getString("time").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("channel").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("truename").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("mobile").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("guest").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("idcard").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("type").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("group0").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("groupid").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("bao").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("treatment").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("gratuity").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("<td width=80>");out.print(new String(rs3.getString("remark").getBytes("iso_8859_1"),"gb2312"));out.print("</td>");
out.print("</tr>");
}
rs3.close();
con.close();
stmt.close();
}catch(SQLException sqle){
out.println("sqle="+sqle);
}finally{
try{
if(con!=null){
con.close();
}
}catch (SQLException sqle){
out.println("sqle="+sqle);
}
}
%>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询