




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、1. packagemon.utils;2. importjava.io.OutputStream;3. importjava.util.List;4. importjavax.servlet.http.HttpServletResponse;5. importorg.apache.struts2.ServletActionContext;6. importjava.lang.reflect.Field;7. 8. importjxl.Workbook;9. importjxl.format.Alignment;10. importjxl.format.Border;11. importjxl
2、.format.BorderLineStyle;12. importjxl.format.VerticalAlignment;13. importjxl.write.Label;14. importjxl.write.WritableCellFormat;15. importjxl.write.WritableFont;16. importjxl.write.WritableSheet;17. importjxl.write.WritableWorkbook;18. /*19. *authorlsf20. */21. publicclassExportExcel22. /*23. *param
3、fileNameEXCEL文件名稱(chēng)24. *paramlistTitleEXCEL文件第一行列標(biāo)題集合25. *paramlistContentEXCEL文件正文數(shù)據(jù)集合26. *return27. */28. publicfinalstaticStringexportExcel(StringfileName,StringTitle,ListlistContent)29. Stringresult=系統(tǒng)提示:Excel文件導(dǎo)出成功!;30. /以下開(kāi)始輸出到EXCEL31. try32. /定義輸出流,以便打開(kāi)保存對(duì)話(huà)框_begin33. HttpServletResponseresponse
4、=ServletActionContext.getResponse();34. OutputStreamos=response.getOutputStream();/取得輸出流35. response.reset();/清空輸出流36. response.setHeader(Content-disposition,attachment;filename=+newString(fileName.getBytes(GB2312),ISO8859-1);37. /設(shè)定輸出文件頭38. response.setContentType(application/msexcel);/定義輸出類(lèi)型39. /定
5、義輸出流,以便打開(kāi)保存對(duì)話(huà)框_end40. 41. /*創(chuàng)建工作簿*/42. WritableWorkbookworkbook=Workbook.createWorkbook(os);43. 44. /*創(chuàng)建工作表*/45. 46. WritableSheetsheet=workbook.createSheet(Sheet1,0);47. 48. /*設(shè)置縱橫打?。J(rèn)為縱打)、打印紙*/49. jxl.SheetSettingssheetset=sheet.getSettings();50. sheetset.setProtected(false);51. 52. 53. /*設(shè)置單元格字體
6、*/54. WritableFontNormalFont=newWritableFont(WritableFont.ARIAL,10);55. WritableFontBoldFont=newWritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);56. 57. /*以下設(shè)置三種單元格樣式,靈活備用*/58. /用于標(biāo)題居中59. WritableCellFormatwcf_center=newWritableCellFormat(BoldFont);60. wcf_center.setBorder(Border.ALL,BorderLine
7、Style.THIN);/線(xiàn)條61. wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE);/文字垂直對(duì)齊62. wcf_center.setAlignment(Alignment.CENTRE);/文字水平對(duì)齊63. wcf_center.setWrap(false);/文字是否換行64. 65. /用于正文居左66. WritableCellFormatwcf_left=newWritableCellFormat(NormalFont);67. wcf_left.setBorder(Border.NONE,BorderLineS
8、tyle.THIN);/線(xiàn)條68. wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);/文字垂直對(duì)齊69. wcf_left.setAlignment(Alignment.LEFT);/文字水平對(duì)齊70. wcf_left.setWrap(false);/文字是否換行71. 72. 73. /*以下是EXCEL開(kāi)頭大標(biāo)題,暫時(shí)省略*/74. /sheet.mergeCells(0,0,colWidth,0);75. /sheet.addCell(newLabel(0,0,XX報(bào)表,wcf_center);76. /*以下是EXCEL
9、第一行列標(biāo)題*/77. for(inti=0;iTitle.length;i+)78. sheet.addCell(newLabel(i,0,Titlei,wcf_center);79. 80. /*以下是EXCEL正文數(shù)據(jù)*/81. Fieldfields=null;82. inti=1;83. for(Objectobj:listContent)84. fields=obj.getClass().getDeclaredFields();85. intj=0;86. for(Fieldv:fields)87. v.setAccessible(true);88. Objectva=v.get(
10、obj);89. if(va=null)90. va=;91. 92. sheet.addCell(newLabel(j,i,va.toString(),wcf_left);93. j+;94. 95. i+;96. 97. /*將以上緩存中的內(nèi)容寫(xiě)到EXCEL文件中*/98. workbook.write();99. /*關(guān)閉文件*/100. workbook.close();101. 102. catch(Exceptione)103. result=系統(tǒng)提示:Excel文件導(dǎo)出失敗,原因:+e.toString();104. System.out.println(result);105.
11、 e.printStackTrace();106. 107. returnresult;108. 109. 3.通用導(dǎo)出:javaview plaincopy1. packagemon.excel.parser;2. 3. 4. importjava.io.FileOutputStream;5. importjava.io.OutputStream;6. importjava.lang.reflect.Field;7. importjava.lang.reflect.Method;8. importjava.util.ArrayList;9. importjava.util.Collectio
12、n;10. importjava.util.Date;11. importjava.util.HashMap;12. importjava.util.Iterator;13. importjava.util.List;14. importjava.util.Map;15. 16. 17. importorg.apache.poi.hssf.usermodel.HSSFRichTextString;18. importorg.apache.poi.hssf.usermodel.HSSFWorkbook;19. importorg.apache.poi.ss.usermodel.Cell;20.
13、importorg.apache.poi.ss.usermodel.RichTextString;21. importorg.apache.poi.ss.usermodel.Row;22. importorg.apache.poi.ss.usermodel.Sheet;23. importorg.apache.poi.ss.usermodel.Workbook;24. 25. 26. importcom.huateng.test.pojo.Student;27. 28. 29. publicclassExcelExport230. 31. 32. publicstaticvoidexportE
14、xcel(Stringtitle,ClasspojoClass,CollectiondataSet,33. OutputStreamout)34. /使用userModel模式實(shí)現(xiàn)的,當(dāng)excel文檔出現(xiàn)10萬(wàn)級(jí)別的大數(shù)據(jù)文件可能導(dǎo)致OOM內(nèi)存溢出35. exportExcelInUserModel(title,pojoClass,dataSet,out);36. /使用eventModel實(shí)現(xiàn),可以一邊讀一邊處理,效率較高,但是實(shí)現(xiàn)復(fù)雜,暫時(shí)未實(shí)現(xiàn)37. 38. privatestaticvoidexportExcelInUserModel(Stringtitle,ClasspojoClas
15、s,CollectiondataSet,39. OutputStreamout)40. try41. /首先檢查數(shù)據(jù)看是否是正確的42. if(dataSet=null|dataSet.size()=0)43. thrownewException(導(dǎo)出數(shù)據(jù)為空!);44. 45. if(title=null|out=null|pojoClass=null)46. 47. thrownewException(傳入?yún)?shù)不能為空!);48. 49. /聲明一個(gè)工作薄50. Workbookworkbook=newHSSFWorkbook();51. /生成一個(gè)表格52. Sheetsheet=wor
16、kbook.createSheet(title);53. 54. 55. /標(biāo)題56. ListexportFieldTitle=newArrayList();57. ListexportFieldWidth=newArrayList();58. /拿到所有列名,以及導(dǎo)出的字段的get方法59. ListmethodObj=newArrayList();60. MapconvertMethod=newHashMap();61. /得到所有字段62. Fieldfileds=pojoClass.getDeclaredFields();63. /遍歷整個(gè)filed64. for(inti=0;if
17、ileds.length;i+)65. Fieldfield=filedsi;66. Excelexcel=field.getAnnotation(Excel.class);67. /如果設(shè)置了annottion68. if(excel!=null)69. /添加到標(biāo)題70. exportFieldTitle.add(excel.exportName();71. /添加標(biāo)題的列寬72. exportFieldWidth.add(excel.exportFieldWidth();73. /添加到需要導(dǎo)出的字段的方法74. Stringfieldname=field.getName();75. /
18、System.out.println(i+列寬+excel.exportName()+excel.exportFieldWidth();76. StringBuffergetMethodName=newStringBuffer(get);77. getMethodName.append(fieldname.substring(0,1)78. .toUpperCase();79. getMethodName.append(fieldname.substring(1);80. 81. 82. MethodgetMethod=pojoClass.getMethod(getMethodName.toS
19、tring(),83. newClass);84. 85. 86. methodObj.add(getMethod);87. if(excel.exportConvertSign()=1)88. 89. StringBuffergetConvertMethodName=newStringBuffer(get);90. getConvertMethodName.append(fieldname.substring(0,1)91. .toUpperCase();92. getConvertMethodName.append(fieldname.substring(1);93. getConvert
20、MethodName.append(Convert);94. /System.out.println(convert:+getConvertMethodName.toString();95. MethodgetConvertMethod=pojoClass.getMethod(getConvertMethodName.toString(),96. newClass);97. convertMethod.put(getMethodName.toString(),getConvertMethod);98. 99. 100. 101. intindex=0;102. /產(chǎn)生表格標(biāo)題行103. Row
21、row=sheet.createRow(index);104. for(inti=0,exportFieldTitleSize=exportFieldTitle.size();iexportFieldTitleSize;i+)105. Cellcell=row.createCell(i);106. /cell.setCellStyle(style);107. RichTextStringtext=newHSSFRichTextString(108. exportFieldTitle.get(i);109. cell.setCellValue(text);110. 111. 112. 113.
22、/設(shè)置每行的列寬114. for(inti=0;iexportFieldWidth.size();i+)115. /256=65280/255116. sheet.setColumnWidth(i,256*exportFieldWidth.get(i);117. 118. Iteratorits=dataSet.iterator();119. /循環(huán)插入剩下的集合120. while(its.hasNext()121. /從第二行開(kāi)始寫(xiě),第一行是標(biāo)題122. index+;123. row=sheet.createRow(index);124. Objectt=its.next();125.
23、for(intk=0,methodObjSize=methodObj.size();kmethodObjSize;k+)126. Cellcell=row.createCell(k);127. MethodgetMethod=methodObj.get(k);128. Objectvalue=null;129. if(convertMethod.containsKey(getMethod.getName()130. 131. Methodcm=convertMethod.get(getMethod.getName();132. value=cm.invoke(t,newObject);133.
24、 else134. 135. value=getMethod.invoke(t,newObject);136. 137. cell.setCellValue(value.toString();138. 139. 140. 141. 142. workbook.write(out);143. catch(Exceptione)144. e.printStackTrace();145. 146. 147. 148. 149. 150. 151. publicstaticvoidmain(Stringargs)throwsException152. 153. 154. /構(gòu)造一個(gè)模擬的List來(lái)測(cè)試
25、,實(shí)際使用時(shí),這個(gè)集合用從數(shù)據(jù)庫(kù)中查出來(lái)155. Studentpojo2=newStudent();156. pojo2.setName(第一行數(shù)據(jù));157. pojo2.setAge(28);158. pojo2.setSex(2);159. pojo2.setDesc(abcdefghijklmnop);160. pojo2.setBirthDate(newDate();161. pojo2.setIsVip(true);162. Listlist=newArrayList();163. list.add(pojo2);164. for(inti=0;i50000;i+)165. St
26、udentpojo=newStudent();166. pojo.setName(一二三四五六七八九);167. pojo.setAge(22);168. pojo.setSex(1);169. pojo.setDesc(abcdefghijklmnop);170. pojo.setBirthDate(newDate();171. pojo.setIsVip(false);172. list.add(pojo);173. 174. /構(gòu)造輸出對(duì)象,可以從response輸出,直接向用戶(hù)提供下載175. OutputStreamout=newFileOutputStream(D:/testOne
27、.xls);176. /開(kāi)始時(shí)間177. Longl=System.currentTimeMillis();178. /注意179. ExcelExport2ex=newExcelExport2();180. /181. ex.exportExcel(測(cè)試,Student.class,list,out);182. out.close();183. /結(jié)束時(shí)間184. Longs=System.currentTimeMillis();185. System.out.println(excel導(dǎo)出成功);186. System.out.println(總共耗時(shí):+(s-l);187. 188. 1
28、89. 190. 191. 192. 4.通用導(dǎo)入:javaview plaincopy1. packagemon.excel.parser;2. 3. 4. importjava.io.File;5. importjava.io.FileInputStream;6. importjava.lang.reflect.Field;7. importjava.lang.reflect.Method;8. importjava.lang.reflect.Type;9. importjava.text.SimpleDateFormat;10. importjava.util.ArrayList;11.
29、 importjava.util.Collection;12. importjava.util.HashMap;13. importjava.util.Iterator;14. importjava.util.List;15. importjava.util.Map;16. 17. 18. importorg.apache.poi.hssf.usermodel.HSSFSheet;19. importorg.apache.poi.hssf.usermodel.HSSFWorkbook;20. importorg.apache.poi.ss.usermodel.Cell;21. importor
30、g.apache.poi.ss.usermodel.Row;22. 23. 24. importcom.huateng.test.pojo.Student;25. 26. 27. publicclassExcelImport228. 29. publicCollectionimportExcel(Filefile,ClasspojoClass,String.pattern)30. 31. Collectiondist=newArrayList();32. try33. /得到目標(biāo)目標(biāo)類(lèi)的所有的字段列表34. Fieldfiled=pojoClass.getDeclaredFields();35
31、. /將所有標(biāo)有Annotation的字段,也就是允許導(dǎo)入數(shù)據(jù)的字段,放入到一個(gè)map中36. MapfieldSetMap=newHashMap();37. 38. MapfieldSetConvertMap=newHashMap();39. 40. 41. /循環(huán)讀取所有字段42. for(inti=0;ifiled.length;i+)43. Fieldf=filedi;44. 45. /得到單個(gè)字段上的Annotation46. Excelexcel=f.getAnnotation(Excel.class);47. 48. /如果標(biāo)識(shí)了Annotationd的話(huà)49. if(excel
32、!=null)50. /構(gòu)造設(shè)置了Annotation的字段的Setter方法51. Stringfieldname=f.getName();52. StringsetMethodName=set53. +fieldname.substring(0,1).toUpperCase()54. +fieldname.substring(1);55. /構(gòu)造調(diào)用的method,56. MethodsetMethod=pojoClass.getMethod(setMethodName,57. newClassf.getType();58. /將這個(gè)method以Annotaion的名字為key來(lái)存入。5
33、9. 60. /對(duì)于重名將導(dǎo)致覆蓋失敗,對(duì)于此處的限制需要61. fieldSetMap.put(excel.exportName(),setMethod);62. 63. if(excel.importConvertSign()=1)64. 65. StringBuffersetConvertMethodName=newStringBuffer(set);66. setConvertMethodName.append(fieldname.substring(0,1)67. .toUpperCase();68. setConvertMethodName.append(fieldname.sub
34、string(1);69. setConvertMethodName.append(Convert);70. MethodgetConvertMethod=pojoClass.getMethod(setConvertMethodName.toString(),71. newClassString.class);72. fieldSetConvertMap.put(excel.exportName(),getConvertMethod);73. 74. 75. 76. 77. 78. 79. 80. 81. 82. /將傳入的File構(gòu)造為FileInputStream;83. FileInpu
35、tStreamin=newFileInputStream(file);84. /得到工作表85. HSSFWorkbookbook=newHSSFWorkbook(in);86. /得到第一頁(yè)87. HSSFSheetsheet=book.getSheetAt(0);88. /得到第一面的所有行89. Iteratorrow=sheet.rowIterator();90. 91. 92. /得到第一行,也就是標(biāo)題行93. Rowtitle=row.next();94. /得到第一行的所有列95. IteratorcellTitle=title.cellIterator();96. /將標(biāo)題的文
36、字內(nèi)容放入到一個(gè)map中。97. Maptitlemap=newHashMap();98. /從標(biāo)題第一列開(kāi)始99. inti=0;100. /循環(huán)標(biāo)題所有的列101. while(cellTitle.hasNext()102. Cellcell=cellTitle.next();103. Stringvalue=cell.getStringCellValue();104. titlemap.put(i,value);105. i=i+1;106. 107. 108. 109. /用來(lái)格式化日期的DateFormat110. SimpleDateFormatsf;111. if(pattern
37、.length1)112. 113. sf=newSimpleDateFormat(yyyy-MM-dd);114. 115. else116. sf=newSimpleDateFormat(pattern0);117. 118. 119. while(row.hasNext()120. /標(biāo)題下的第一行121. Rowrown=row.next();122. 123. /行的所有列124. Iteratorcellbody=rown.cellIterator();125. 126. /得到傳入類(lèi)的實(shí)例127. ObjecttObject=pojoClass.newInstance();128
38、. 129. intk=0;130. /遍歷一行的列131. while(cellbody.hasNext()132. Cellcell=cellbody.next();133. /這里得到此列的對(duì)應(yīng)的標(biāo)題134. StringtitleString=(String)titlemap.get(k);135. /如果這一列的標(biāo)題和類(lèi)中的某一列的Annotation相同,那么則調(diào)用此類(lèi)的的set方法,進(jìn)行設(shè)值136. if(fieldSetMap.containsKey(titleString)137. 138. MethodsetMethod=(Method)fieldSetMap.get(ti
39、tleString);139. /得到setter方法的參數(shù)140. Typets=setMethod.getGenericParameterTypes();141. /只要一個(gè)參數(shù)142. Stringxclass=ts0.toString();143. /判斷參數(shù)類(lèi)型144. System.out.println(類(lèi)型:+xclass);145. 146. if(fieldSetConvertMap.containsKey(titleString)147. 148. 149. fieldSetConvertMap.get(titleString).invoke(tObject,150. cell.getStringCellValue();151. 152. 153. else154. if(xclass.equals(classjava.lang.String)155. setMethod
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 護(hù)理技能提升與臨床實(shí)踐創(chuàng)新
- 2025-2030年中國(guó)磨砂膠行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 2025-2030年中國(guó)電影行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 2025-2030年中國(guó)電動(dòng)汽車(chē)用太陽(yáng)能電池板及充電器行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 2025-2030年中國(guó)生鮮牛乳行業(yè)發(fā)展趨勢(shì)與前景展望戰(zhàn)略研究報(bào)告
- 2025-2030年中國(guó)環(huán)??咕瑝|行業(yè)市場(chǎng)發(fā)展分析及發(fā)展趨勢(shì)與投資研究報(bào)告
- 2025-2030年中國(guó)狗窩行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 2025-2030年中國(guó)物流園區(qū)行業(yè)現(xiàn)狀供需分析及市場(chǎng)深度研究發(fā)展前景及規(guī)劃可行性分析研究報(bào)告
- 2025-2030年中國(guó)牙鉆機(jī)行業(yè)發(fā)展分析及投資風(fēng)險(xiǎn)預(yù)測(cè)研究報(bào)告
- 2025-2030年中國(guó)熱空氣消毒器行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 母嬰阻斷知識(shí)培訓(xùn)課件
- TGXTC 0008-2024 鷹嘴桃流膠病防治技術(shù)規(guī)程
- 冶煉煙氣制酸工藝設(shè)計(jì)規(guī)范
- 小學(xué)學(xué)校規(guī)范教材和教輔資料征訂管理暫行辦法
- JT-T-1178.2-2019營(yíng)運(yùn)貨車(chē)安全技術(shù)條件第2部分:牽引車(chē)輛與掛車(chē)
- 海洋環(huán)境下船用太陽(yáng)能光伏系統(tǒng)特性研究
- 廣東省廣州中考近5年中考真題高頻詞502
- 2024年成都影視城文化傳媒有限公司招聘筆試沖刺題(帶答案解析)
- 剪叉式升降工作平臺(tái)作業(yè)專(zhuān)項(xiàng)施工方案24
- 壓力容器安全監(jiān)察規(guī)程
- 2021年上海市普通高中學(xué)業(yè)水平合格性考試歷史試卷
評(píng)論
0/150
提交評(píng)論