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>
展开
 我来答
百度网友7ec2af9f
推荐于2016-11-13 · TA获得超过113个赞
知道小有建树答主
回答量:196
采纳率:0%
帮助的人:89万
展开全部
不用这么麻烦的

直接用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>
secc_secc
2008-05-13 · TA获得超过1019个赞
知道小有建树答主
回答量:933
采纳率:100%
帮助的人:699万
展开全部
如果是在html中
建议用vbs吧 直接用excelapplication 简单多了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式