服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

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

扫描关注微信公众号