Struts實時生成Excel文件下載(轉)
我做的項目原來是先在服務器上生成一個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()">
4
DownLoad
5
</html:button>
6
</html:html>
7
<script language='javascript'>
8
function printAll(){ location.href="<%=request.getContextPath()%>/download.do"; }
9
</script>

2

3

4

5

6

7

8

9

DownloadAction.java
1
import org.apache.struts.action.*;
2
import javax.servlet.http.*;
3
import java.io.OutputStream;
4
import 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
14
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

ExcelBean.java
1
package test.whw.upload;
2
import java.io.*;
3
import jxl.*;
4
import jxl.write.*;
5
import jxl.format.*;
6
import java.util.*;
7
import java.awt.Color;
8
9
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

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是成功的提示頁面-->

2

3

4

5

6

7

8

9

10
