我做的项目原来是先在服务器上生成一个excel文件,然后用jspsmartupload下载的,可是由于用jspsmartupload下载的excel文件由于编码问题会有损坏,而且服务器的压力也太大,所以改为在Action中生成excel文件,然后下载,方便多了。由于项目的原因,excel文件是实时生成的,对于jxl的使用,大家可以参考jxl相关的文章。
有什么问题可以和我联系。
MSN:whw_dream(AT)hotmail.com
代码如下:
test.jsp
1<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
2<html:html>
3<html:button property="button" onclick="printAll()">
4DownLoad
5</html:button>
6</html:html>
7<script language='javascript'>
8function printAll(){ location.href="<%=request.getContextPath()%>/download.do"; }
9</script>
DownloadAction.java
1import org.apache.struts.action.*;
2import javax.servlet.http.*;
3import java.io.OutputStream;
4import test.whw.upload.ExcelBean;
5/** *//**
6 * <p>Title:DownloadAction </p>
7 * <p>Description: QRRSMMS </p>
8 * <p>Copyright: Copyright (c) 2004 jiahansoft</p>
9 * <p>Company: jiahansoft</p>
10 * @author wanghw
11 * @version 1.0
12 */
13
14public class DownloadAction extends Action {
15 public ActionForward execute(ActionMapping mapping,
16 ActionForm form,
17 HttpServletRequest request,
18 HttpServletResponse response)
19 throws Exception {
20 try{
21 String fname = "test";//Excel文件名
22 OutputStream os = response.getOutputStream();//取得输出流
23 response.reset();//清空输出流
24 response.setHeader("Content-disposition", "attachment; filename=" + fname + ".xls");//设定输出文件头
25 response.setContentType("application/msexcel");//定义输出类型
26 ExcelBean eb = new ExcelBean();
27 eb.expordExcel(os);//调用生成excel文件bean
28 }catch(Exception e){
29 System.out.println(e);
30 }
31
32 return mapping.findForward("display");
33 }
34}
35
ExcelBean.java
1package test.whw.upload;
2import java.io.*;
3import jxl.*;
4import jxl.write.*;
5import jxl.format.*;
6import java.util.*;
7import java.awt.Color;
8
9public class ExcelBean {
10 public ExcelBean(){}
11 public String expordExcel(OutputStream os)throws Exception{
12 jxl.write.WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件
13 String tmptitle = "测试文件"; //标题
14 jxl.write.WritableSheet wsheet = wbook.createSheet("第一页", 0); //sheet名称
15 //设置excel标题
16 jxl.write.WritableFont wfont = new jxl.write.WritableFont(
17 WritableFont.ARIAL, 16,
18 WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE,
19 jxl.format.Colour.BLACK);
20 jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
21 wfont);
22 jxl.write.Label wlabel1;
23 wlabel1 = new jxl.write.Label(5, 0, tmptitle, wcfFC);
24 wsheet.addCell(wlabel1);
25 wfont = new jxl.write.WritableFont(
26 WritableFont.ARIAL, 14,
27 WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE,
28 jxl.format.Colour.BLACK);
29 wcfFC = new jxl.write.WritableCellFormat(
30 wfont);
31 jxl.write.Label wlabel;
32 wlabel = new jxl.write.Label(0, 0, "写入内容");
33 wsheet.addCell(wlabel); //
34 wbook.write(); //写入文件
35 wbook.close();
36 os.close();
37 return "success";
38 }
39}
40
struts-config.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
3<struts-config>
4 <action-mappings>
5 <action type="test.whw.upload.DownloadAction" path="/download">
6 <forward name="display" path="/display.jsp" />
7 </action>
8 </action-mappings>
9</struts-config>
10<!--display.jsp是成功的提示页面-->