Java,jsp,javascript中如何实现将统计表格保存成Excel文件?
---------------------------------------------------------------
< SCRIPT LANGUAGE="JavaScript" >
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< tblout.rows.length;i++)
for(j=0;j< 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;
}
< /SCRIPT >
//这个问题其实你搜一下就有了
---------------------------------------------------------------
首先你的页面上要有一个按钮:save
这个按钮先做查询数据库,然后action转到保存excell的页面,即我给你的这些代码,由于我的是用这个页面是隐藏的,点击save时有跳出界面问你是打开还是保存,xxxxxxxxxxxxxx是你调用数据库的记录,
< %@ page contentType="text/Html;charset=gb2312" % >
< %@ page import="java.lang.*"% >
< %
try{
response.setContentType("Application/msexcel");
response.setHeader("Content-Disposition", "attachment;filename=trainlist.xls");
StringBuffer cont=new StringBuffer("");
% >
< HTML >
< HEAD >
< META http-equiv="Content-Style-Type" content="text/Css" >
< /HEAD >
< BODY >
< br >< br >< br >
< %
int status = khViewBean.getStatus();
if (status == 0){
int rscount = khViewBean.getRSCount();
% >
< %
cont.append("< html >< head >< META content=′text/html; charset=gb2312′ http-equiv=Content-Type >< /head >< body > ");
cont.append("< h1 > SOP??μμ2é?ˉ?á1?< /h1 >");
cont.append("< table width=′100%′ border=′1′ > ");
cont.append("< tr > ");
cont.append("< td height=′19′ >???to? < /td > ");
cont.append("< td height=′19′ >???t?? < /td > ");
cont.append("< td height=′19′ >2??? < /td > ");
cont.append("< td height=′19′ >éúD§ê±?? < /td > ");
cont.append("< /tr > ");
cont.append("< tr > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
cont.append("< /tr > ");
cont.append("< tr > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
cont.append("< td >"+xxxxxxxxxxxxxxxxxxxxxxxx +" < /td > ");
}
cont.append("< /table > ");
cont.append("< /body >< /html > ");
response.getWriter().println(cont.toString());
response.getWriter().close();
% >
< %
}
else if (status > 0) {
% >
< hr color="#6699CC" >
< span class="text" >< font color="blue" size="16" >< center >< P >?Têy?Y < /P >< /center >< /font >< /span >
< %
}
else if (status < 0) {
% >
< hr color="#6699CC" >
< span class="text" >< font color="red" size="16" >< center >< P >?¢éú′í?ó < /P >< /center >< /font >< /span >
< %
}
% >
< hr color="#6699CC" >
< %
}
catch (Exception e){
out.println(e.toString());
}
% >
< /BODY >
< /HTML >