有关bom的文章

http://expert.csdn.net/Expert/topic/1343/1343007.xml?temp=.730694 http://topic.csdn.net/t/20030120/12/1375432

Struts实时生成Excel文件下载     选择自 kingbug 的 Blog
关键字  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" οnclick="printAll()">
DownLoad
</html:button>
</html:html>
<script language='javascript'>
function printAll(){ location.href="<%=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是成功的提示页面-->


 


相关文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值