Java導出Excel通用方法,只需要一個list 集合。通用方法改進之處踴躍提出
1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package oa.common.utils; import java.io.OutputStream; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import java.lang.reflect.Field; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; /*** * @author lsf */ public class ExportExcel { /*************************************************************************** * @param fileName EXCEL文件名稱 * @param listTitle EXCEL文件第一行列標題集合 * @param listContent EXCEL文件正文數據集合 * @return */ public final static String exportExcel(String fileName,String[] Title, List<Object> listContent) { String result= "系統提示:Excel文件導出成功!" ; // 以下開始輸出到EXCEL try { //定義輸出流,以便打開保存對話框______________________begin HttpServletResponse response=ServletActionContext.getResponse(); OutputStream os = response.getOutputStream(); // 取得輸出流 response.reset(); // 清空輸出流 response.setHeader( "Content-disposition" , "attachment; filename=" + new String(fileName.getBytes( "GB2312" ), "ISO8859-1" )); // 設定輸出文件頭 response.setContentType( "application/msexcel" ); // 定義輸出類型 //定義輸出流,以便打開保存對話框_______________________end /** **********創建工作簿************ */ WritableWorkbook workbook = Workbook.createWorkbook(os); /** **********創建工作表************ */ WritableSheet sheet = workbook.createSheet( "Sheet1" , 0 ); /** **********設置縱橫打印(默認為縱打)、打印紙***************** */ jxl.SheetSettings sheetset = sheet.getSettings(); sheetset.setProtected( false ); /** ************設置單元格字體************** */ WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10 ); WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10 ,WritableFont.BOLD); /** ************以下設置三種單元格樣式,靈活備用************ */ // 用于標題居中 WritableCellFormat wcf_center = new WritableCellFormat(BoldFont); wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條 wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊 wcf_center.setAlignment(Alignment.CENTRE); // 文字水平對齊 wcf_center.setWrap( false ); // 文字是否換行 // 用于正文居左 WritableCellFormat wcf_left = new WritableCellFormat(NormalFont); wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 線條 wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊 wcf_left.setAlignment(Alignment.LEFT); // 文字水平對齊 wcf_left.setWrap( false ); // 文字是否換行 /** ***************以下是EXCEL開頭大標題,暫時省略********************* */ //sheet.mergeCells(0, 0, colWidth, 0); //sheet.addCell(new Label(0, 0, "XX報表", wcf_center)); /** ***************以下是EXCEL第一行列標題********************* */ for ( int i = 0 ; i < Title.length; i++) { sheet.addCell( new Label(i, 0 ,Title[i],wcf_center)); } /** ***************以下是EXCEL正文數據********************* */ Field[] fields= null ; int i= 1 ; for (Object obj:listContent){ fields=obj.getClass().getDeclaredFields(); int j= 0 ; for (Field v:fields){ v.setAccessible( true ); Object va=v.get(obj); if (va== null ){ va= "" ; } sheet.addCell( new Label(j, i,va.toString(),wcf_left)); j++; } i++; } /** **********將以上緩存中的內容寫到EXCEL文件中******** */ workbook.write(); /** *********關閉文件************* */ workbook.close(); } catch (Exception e) { result= "系統提示:Excel文件導出失敗,原因:" + e.toString(); System.out.println(result); e.printStackTrace(); } return result; } } |
測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * 導出excel * @return */ public String excelPage(){ ExportExcel excel= new ExportExcel(); String str= "" ; try { str = new String(getHTTP.getRequest().getParameter( "wineOrg.orgName" ).getBytes( "iso8859-1" ), "UTF-8" ); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } wineOrg.setOrgName(str); List<Object> li=service.exportExcel(wineOrg); String[] id="codetool">
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持! 原文鏈接:http://blog.csdn.net/johnstrive/article/details/8109919 延伸 · 閱讀
精彩推薦
|