struts2中POI在内存中生成文件并下载

Java代码

1.<%@pagecontentType="text/html;charset=gbk"%>

2.<%@tagliburi="/struts-tags"prefix="s"%>

3.<html>

4.<ahref="excel.action">下载文件</a>

5.</html>

<%@pagecontentType="text/html;charset=gbk"%>

<%@tagliburi="/struts-tags"prefix="s"%>

<html>

<ahref="excel.action">下载文件</a>

</html>

struts.xml文件

文件内容:

Xml代码

1.<?xmlversion="1.0"encoding="UTF-8"?>

2.<!DOCTYPEstrutsPUBLIC

3."-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

4."http://struts.apache.org/dtds/struts-2.0.dtd">

5.

6.<struts>

7.

8.<packagename="default"extends="struts-default">

9.<actionname="excel"class="ExcelDownloadAction">

10.<resultname="success"type="stream">

11.<paramname="contentType">application/vnd.ms-excel</param>

12.<paramname="contentDisposition">attachment;filename="AllUsers.xls"</param>

13.<paramname="inputName">excelFile</param>

14.</result>

15.</action>

16.</package>

17.</struts>

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstrutsPUBLIC

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<packagename="default"extends="struts-default">

<actionname="excel"class="ExcelDownloadAction">

<resultname="success"type="stream">

<paramname="contentType">application/vnd.ms-excel</param>

<paramname="contentDisposition">attachment;filename="AllUsers.xls"</param>

<paramname="inputName">excelFile</param>

</result>

</action>

</package>

</struts>

Java代码

1.importjava.io.ByteArrayInputStream;

2.importjava.io.ByteArrayOutputStream;

3.importjava.io.IOException;

4.importjava.io.InputStream;

5.

6.importorg.apache.poi.hssf.usermodel.HSSFCell;

7.importorg.apache.poi.hssf.usermodel.HSSFRow;

8.importorg.apache.poi.hssf.usermodel.HSSFSheet;

9.importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

10.

11.importcom.opensymphony.xwork2.ActionSupport;

12.

13.@SuppressWarnings("serial")

14.publicclassExcelDownloadActionextendsActionSupport{

15.

16.publicInputStreamgetExcelFile(){

17.HSSFWorkbookworkbook=newHSSFWorkbook();

18.HSSFSheetsheet=workbook.createSheet("sheet1");

19.{

20.//创建表头

21.HSSFRowrow=sheet.createRow(0);

22.HSSFCellcell=row.createCell((short)0);

23.cell.setCellValue("id");

24.cell=row.createCell((short)1);

25.cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

26.cell.setCellValue("姓");

27.cell=row.createCell((short)2);

28.cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

29.cell.setCellValue("名");

30.cell=row.createCell((short)3);

31.cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

32.cell.setCellValue("年龄");

33.

34.//创建数据

35.//第一行

36.row=sheet.createRow(1);

37.cell=row.createCell((short)0);

38.cell.setCellValue("1");

39.cell=row.createCell((short)1);

40.cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

41.cell.setCellValue("张");

42.cell=row.createCell((short)2);

43.cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

44.cell.setCellValue("四");

45.cell=row.createCell((short)3);

46.cell.setCellValue("23");

47.

48.//第二行

49.row=sheet.createRow(2);

50.cell=row.createCell((short)0);

51.cell.setCellValue("2");

52.cell=row.createCell((short)1);

53.cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

54.cell.setCellValue("李");

55.cell=row.createCell((short)2);

56.cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

57.cell.setCellValue("六");

58.cell=row.createCell((short)3);

59.cell.setCellValue("30");

60.}

61.

62.ByteArrayOutputStreambaos=newByteArrayOutputStream();

63.try{

64.workbook.write(baos);

65.}catch(IOExceptione){

66.//TODOAuto-generatedcatchblock

67.e.printStackTrace();

68.}

69.byte[]ba=baos.toByteArray();

70.ByteArrayInputStreambais=newByteArrayInputStream(ba);

71.returnbais;

72.

73.}

74.

75.@Override

76.publicStringexecute()throwsException{

77.//TODOAuto-generatedmethodstub

78.returnsuper.execute();

79.}

80.

81.}

importjava.io.ByteArrayInputStream;

importjava.io.ByteArrayOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importorg.apache.poi.hssf.usermodel.HSSFCell;

importorg.apache.poi.hssf.usermodel.HSSFRow;

importorg.apache.poi.hssf.usermodel.HSSFSheet;

importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

importcom.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")

publicclassExcelDownloadActionextendsActionSupport{

publicInputStreamgetExcelFile(){

HSSFWorkbookworkbook=newHSSFWorkbook();

HSSFSheetsheet=workbook.createSheet("sheet1");

{

//创建表头

HSSFRowrow=sheet.createRow(0);

HSSFCellcell=row.createCell((short)0);

cell.setCellValue("id");

cell=row.createCell((short)1);

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

cell.setCellValue("姓");

cell=row.createCell((short)2);

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

cell.setCellValue("名");

cell=row.createCell((short)3);

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

cell.setCellValue("年龄");

//创建数据

//第一行

row=sheet.createRow(1);

cell=row.createCell((short)0);

cell.setCellValue("1");

cell=row.createCell((short)1);

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

cell.setCellValue("张");

cell=row.createCell((short)2);

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

cell.setCellValue("四");

cell=row.createCell((short)3);

cell.setCellValue("23");

//第二行

row=sheet.createRow(2);

cell=row.createCell((short)0);

cell.setCellValue("2");

cell=row.createCell((short)1);

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

cell.setCellValue("李");

cell=row.createCell((short)2);

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

cell.setCellValue("六");

cell=row.createCell((short)3);

cell.setCellValue("30");

}

ByteArrayOutputStreambaos=newByteArrayOutputStream();

try{

workbook.write(baos);

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

byte[]ba=baos.toByteArray();

ByteArrayInputStreambais=newByteArrayInputStream(ba);

returnbais;

}

@Override

publicStringexecute()throwsException{

//TODOAuto-generatedmethodstub

returnsuper.execute();

}

}

本文出自:http://kin111.blog.51cto.com/738881/167727

相关推荐