龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

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

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
Java,jsp,javascript中如何实现将统计表格保存成Excel文件? --------------------------------------------------------------- lt SCRIPT LANGUAGE="JavaScript" gt function AutomateExcel() { var i,j; // Start Excel and get Applicati

  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
  


  
精彩图集

赞助商链接