服务热线:13616026886

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

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

struts实时生成excel文件下载

我做的项目原来是先在服务器上生成一个excel文件,然后用jspsmartupload下载的,可是由于用jspsmartupload下载的excel文件由于编码问题会有损坏,而且服务器的压力也太大,所以改为在action中生成excel文件,然后下载,方便多了。由于项目的原因,excel文件是实时生成的,对于jxl的使用,大家可以参考jxl相关的文章。
有什么问题可以和我联系。
msn:whw_dream(at)hotmail.com
代码如下:
test.jsp



<%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %>
<html:html>
<html:button property="button" onclick="printall()">
download
</html:button>
</html:html>
<script language='javascript'>
function printall(){ location.href="<%=request.getcontextpath()%><%=request.getcontextpath()%>/download.do"; }
</script>


downloadaction.java


import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.outputstream;
import test.whw.upload.excelbean;
/**
 * <p>title:downloadaction </p>
 * <p>description: qrrsmms </p>
 * <p>copyright: copyright (c) 2004 jiahansoft</p>
 * <p>company: jiahansoft</p>
 * @author wanghw
 * @version 1.0
 */

public class downloadaction extends action {
  public actionforward execute(actionmapping mapping,
                               actionform form,
                               httpservletrequest request,
                               httpservletresponse response)
      throws exception {
    try{
      string fname = "test";//excel文件名
      outputstream os = response.getoutputstream();//取得输出流
      response.reset();//清空输出流
      response.setheader("content-disposition", "attachment; filename=" + fname + ".xls");//设定输出文件头
      response.setcontenttype("application/msexcel");//定义输出类型
      excelbean eb = new excelbean();
      eb.expordexcel(os);//调用生成excel文件bean
    }catch(exception e){
      system.out.println(e);
    }

    return mapping.findforward("display");
  }
}


excelbean.java


package test.whw.upload;
import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;
import java.util.*;
import java.awt.color;

public class excelbean {
  public excelbean(){}
  public string expordexcel(outputstream os)throws exception{
    jxl.write.writableworkbook wbook = workbook.createworkbook(os); //建立excel文件
    string tmptitle = "测试文件"; //标题
    jxl.write.writablesheet wsheet = wbook.createsheet("第一页", 0); //sheet名称
    //设置excel标题
    jxl.write.writablefont wfont = new jxl.write.writablefont(
        writablefont.arial, 16,
        writablefont.bold, false, jxl.format.underlinestyle.no_underline,
        jxl.format.colour.black);
    jxl.write.writablecellformat wcffc = new jxl.write.writablecellformat(
        wfont);
    jxl.write.label wlabel1;
    wlabel1 = new jxl.write.label(5, 0, tmptitle, wcffc);
    wsheet.addcell(wlabel1);
    wfont = new jxl.write.writablefont(
        writablefont.arial, 14,
        writablefont.bold, false, jxl.format.underlinestyle.no_underline,
        jxl.format.colour.black);
    wcffc = new jxl.write.writablecellformat(
        wfont);
    jxl.write.label wlabel;
    wlabel = new jxl.write.label(0, 0, "写入内容");
    wsheet.addcell(wlabel); //
    wbook.write(); //写入文件
    wbook.close();
    os.close();
    return "success";
  }
}



struts-config.xml

<?xml version="1.0" encoding="utf-8"?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
  <action-mappings>
    <action type="test.whw.upload.downloadaction" path="/download">
      <forward name="display" path="/display.jsp" />
    </action>
  </action-mappings>
</struts-config>
<!--display.jsp是成功的提示页面-->


扫描关注微信公众号