首页 > 学院 > 开发设计 > 正文

如何实现将统计表格保存成excel文件

2019-11-18 11:57:22
字体:
来源:转载
供稿:网友

java,jsp,javascript中如何实现将统计表格保存成Excel文件?
---------------------------------------------------------------
&lt SCRIPT LANGUAGE="Javascript" &gt
function AutomateExcel()
{
var i,j;
// Start Excel and get application object.
var oXL = new ActiveXObject("Excel.Application");

oXL.Visible = true;

// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;

// Add table headers going cell by cell.
// tblout表的ID
for(i=0;i&lt tblout.rows.length;i++)
for(j=0;j&lt tblout.rows(i).cells.length;j++)
oSheet.Cells(i+1, j+1).Value = tblout.rows(i).cells(j).innerText

oXL.Visible = true;
oXL.UserControl = true;
}
&lt /SCRIPT &gt
//这个问题其实你搜一下就有了

---------------------------------------------------------------

首先你的页面上要有一个按钮:save
这个按钮先做查询数据库,然后action转到保存excell的页面,即我给你的这些代码,由于我的是用这个页面是隐藏的,点击save时有跳出界面问你是打开还是保存,xxxxxxxxxxxxxx是你调用数据库的记录,
&lt %@ page contentType="text/Html;charset=gb2312" % &gt
&lt %@ page import="java.lang.*"% &gt
&lt %
try{
response.setContentType("Application/msexcel");
response.setHeader("Content-Disposition", "attachment;filename=trainlist.xls");
StringBuffer cont=new StringBuffer("");
% &gt
&lt HTML &gt
&lt HEAD &gt
&lt META http-equiv="Content-Style-Type" content="text/CSS" &gt
&lt /HEAD &gt
&lt BODY &gt
&lt br &gt&lt br &gt&lt br &gt
&lt %
int status = khViewBean.getStatus();
if (status == 0){
int rscount = khViewBean.getRSCount();
% &gt
&lt %
cont.append("&lt html &gt&lt head &gt&lt META content=′text/html; charset=gb2312′ http-equiv=Content-Type &gt&lt /head &gt&lt body &gt ");
cont.append("&lt h1 &gt SOP??μμ2é?ˉ?á1?&lt /h1 &gt");
cont.append("&lt table width=′100%′ border=′1′ &gt ");
cont.append("&lt tr &gt ");
cont.append("&lt td height=′19′ &gt???to? &lt /td &gt ");
cont.append("&lt td height=′19′ &gt???t?? &lt /td &gt ");
cont.append("&lt td height=′19′ &gt2??? &lt /td &gt ");
cont.append("&lt td height=′19′ &gtéúD§ê±?? &lt /td &gt ");
cont.append("&lt /tr &gt ");
cont.append("&lt tr &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
cont.append("&lt /tr &gt ");
cont.append("&lt tr &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
cont.append("&lt td &gt"+xxxxxxxxxxxxxxxxxxxxxxxx +" &lt /td &gt ");
}
cont.append("&lt /table &gt ");
cont.append("&lt /body &gt&lt /html &gt ");
response.getWriter().PRintln(cont.toString());
response.getWriter().close();
% &gt
&lt %
}
else if (status  &gt 0) {
% &gt
&lt hr color="#6699CC" &gt
&lt span class="text" &gt&lt font color="blue" size="16" &gt&lt center &gt&lt P &gt?Têy?Y &lt /P &gt&lt /center &gt&lt /font &gt&lt /span &gt
&lt %
}
else if (status &lt  0) {
% &gt
&lt hr color="#6699CC" &gt
&lt span class="text" &gt&lt font color="red" size="16" &gt&lt center &gt&lt P &gt?¢éú′í?ó &lt /P &gt&lt /center &gt&lt /font &gt&lt /span &gt
&lt %
}
% &gt
&lt hr color="#6699CC" &gt
&lt %
}
catch (Exception e){
out.println(e.toString());
}
% &gt
&lt /BODY &gt
&lt /HTML &gt



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表