`
javatome
  • 浏览: 824658 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Struts 2导出EXCEL

 
阅读更多
  1. importjava.io.IOException;
  2. importjava.io.OutputStream;
  3. importjava.util.List;
  4. importjavax.servlet.http.HttpServletResponse;
  5. importorg.apache.poi.hssf.usermodel.HSSFCell;
  6. importorg.apache.poi.hssf.usermodel.HSSFCellStyle;
  7. importorg.apache.poi.hssf.usermodel.HSSFDataFormat;
  8. importorg.apache.poi.hssf.usermodel.HSSFFont;
  9. importorg.apache.poi.hssf.usermodel.HSSFRow;
  10. importorg.apache.poi.hssf.usermodel.HSSFSheet;
  11. importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. importorg.apache.struts2.ServletActionContext;
  13. importcom.neusoft.dcd.css.common.BaseAction;
  14. importcom.neusoft.dcd.css.model.management.user.UserBaseInfo;
  15. importcom.neusoft.dcd.css.model.tsc.Tsc;
  16. importcom.neusoft.dcd.css.service.tsc.TscService;
  17. importcom.opensymphony.xwork2.Preparable;
  18. publicclassExportExcel{
  19. privatestaticfinallongserialVersionUID=1L;
  20. privateTscServicecssTscService;
  21. publicvoidprepare()throwsException{
  22. }
  23. /**
  24. *绘出Excel
  25. *
  26. *@paramactionMapping
  27. *@paramactionForm
  28. *@paramrequest
  29. *@paramresponse
  30. *@return
  31. *@throwsException
  32. */
  33. publicStringinit()throwsException{
  34. UserBaseInfouserinfo=(UserBaseInfo)ServletActionContext
  35. .getRequest().getSession().getAttribute("UserBaseInfo");
  36. if(userinfo!=null){
  37. HttpServletResponseresponse=ServletActionContext.getResponse();
  38. ListdataList=cssTscService.queryForProTsclb(userinfo.getUserId());
  39. HSSFWorkbookworkbook=exportExcel(dataList);
  40. if(workbook!=null){
  41. this.printExcel(workbook,response,"ExcelTsc.xls");
  42. }
  43. }
  44. returnSUCCESS;
  45. }
  46. //导出Excel
  47. privatevoidprintExcel(HSSFWorkbookworkbook,HttpServletResponseresponse,Stringstring)throwsIOException{
  48. OutputStreamout=response.getOutputStream();
  49. response.setHeader("Content-disposition","attachment;filename="+"TSC.xls");
  50. response.setContentType("application/msexcel;charset=UTF-8");
  51. workbook.write(out);
  52. out.flush();
  53. out.close();
  54. }
  55. publicHSSFWorkbookexportExcel(ListdataList)throwsException
  56. {
  57. HSSFWorkbookworkbook=null;
  58. try
  59. {
  60. //这里的数据即时你要从后台取得的数据
  61. //创建工作簿实例
  62. workbook=newHSSFWorkbook();
  63. //创建工作表实例
  64. HSSFSheetsheet=workbook.createSheet("TscExcel");
  65. //设置列宽
  66. this.setSheetColumnWidth(sheet);
  67. //获取样式
  68. HSSFCellStylestyle=this.createTitleStyle(workbook);
  69. //
  70. if(dataList!=null&&dataList.size()>0)
  71. {
  72. //创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
  73. HSSFRowrow=sheet.createRow((short)0);//建立新行
  74. this.createCell(row,0,style,HSSFCell.CELL_TYPE_STRING,
  75. this.getText("css.tsc.asktime"));
  76. this.createCell(row,1,style,HSSFCell.CELL_TYPE_STRING,
  77. this.getText("css.tsc.productname"));
  78. this.createCell(row,2,style,HSSFCell.CELL_TYPE_STRING,
  79. this.getText("css.tsc.passsort"));
  80. this.createCell(row,3,style,HSSFCell.CELL_TYPE_STRING,
  81. this.getText("css.tsc.askusername"));
  82. this.createCell(row,4,style,HSSFCell.CELL_TYPE_STRING,
  83. this.getText("css.tsc.dtype"));
  84. this.createCell(row,5,style,HSSFCell.CELL_TYPE_STRING,
  85. this.getText("css.tsc.askone"));
  86. this.createCell(row,6,style,HSSFCell.CELL_TYPE_STRING,
  87. this.getText("css.tsc.department"));
  88. this.createCell(row,7,style,HSSFCell.CELL_TYPE_STRING,
  89. this.getText("css.tsc.requesttime"));
  90. this.createCell(row,8,style,HSSFCell.CELL_TYPE_STRING,
  91. this.getText("css.tsc.answertime"));
  92. this.createCell(row,9,style,HSSFCell.CELL_TYPE_STRING,
  93. this.getText("css.tsc.requesttow"));
  94. //给excel填充数据
  95. for(inti=0;i<dataList.size();i++)
  96. {
  97. //将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦
  98. Tscmodel=(Tsc)dataList.get(i);
  99. HSSFRowrow1=sheet.createRow((short)(i+1));//建立新行
  100. if(model.getAsktime()!=null)
  101. this.createCell(row1,0,style,HSSFCell.CELL_TYPE_STRING,
  102. model.getAsktime());
  103. if(model.getProductname()!=null)
  104. this.createCell(row1,1,style,HSSFCell.CELL_TYPE_STRING,
  105. model.getProductname());
  106. if(model.getPasssort()!=null)
  107. this.createCell(row1,2,style,HSSFCell.CELL_TYPE_STRING,
  108. model.getPasssort());
  109. if(model.getAskusername()!=null)
  110. this.createCell(row1,3,style,HSSFCell.CELL_TYPE_STRING,
  111. model.getAskusername());
  112. if(model.getDtype()!=null)
  113. this.createCell(row1,4,style,HSSFCell.CELL_TYPE_STRING,
  114. model.getDtype());
  115. if(model.getAskone()!=null)
  116. this.createCell(row1,5,style,HSSFCell.CELL_TYPE_STRING,
  117. model.getAskone());
  118. if(model.getDepartment()!=null)
  119. this.createCell(row1,6,style,HSSFCell.CELL_TYPE_STRING,
  120. model.getDepartment());
  121. if(model.getRequesttime()!=null)
  122. this.createCell(row1,7,style,HSSFCell.CELL_TYPE_STRING,
  123. model.getRequesttime());
  124. if(model.getAnswertime()!=null)
  125. this.createCell(row1,8,style,HSSFCell.CELL_TYPE_STRING,
  126. model.getAnswertime());
  127. if(model.getRequesttow()!=null)
  128. this.createCell(row1,9,style,HSSFCell.CELL_TYPE_STRING,
  129. model.getRequesttow());
  130. }
  131. }
  132. else{
  133. this.createCell(sheet.createRow(0),0,style,
  134. HSSFCell.CELL_TYPE_STRING,"查无资料");
  135. }
  136. }catch(Exceptione)
  137. {
  138. e.printStackTrace();
  139. }
  140. returnworkbook;
  141. }
  142. privatevoidsetSheetColumnWidth(HSSFSheetsheet)
  143. {
  144. //根据你数据里面的记录有多少列,就设置多少列
  145. sheet.setColumnWidth((short)0,(short)3000);
  146. sheet.setColumnWidth((short)1,(short)3000);
  147. sheet.setColumnWidth((short)2,(short)3000);
  148. sheet.setColumnWidth((short)3,(short)3000);
  149. sheet.setColumnWidth((short)4,(short)5000);
  150. sheet.setColumnWidth((short)5,(short)5000);
  151. sheet.setColumnWidth((short)6,(short)5000);
  152. sheet.setColumnWidth((short)7,(short)5000);
  153. sheet.setColumnWidth((short)8,(short)5000);
  154. sheet.setColumnWidth((short)9,(short)5000);
  155. }
  156. //设置excel的title样式
  157. privateHSSFCellStylecreateTitleStyle(HSSFWorkbookwb){
  158. HSSFFontboldFont=wb.createFont();
  159. boldFont.setFontHeight((short)200);
  160. HSSFCellStylestyle=wb.createCellStyle();
  161. style.setFont(boldFont);
  162. style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
  163. returnstyle;
  164. }
  165. //创建Excel单元格
  166. privatevoidcreateCell(HSSFRowrow,intcolumn,HSSFCellStylestyle,
  167. intcellType,Objectvalue){
  168. HSSFCellcell=row.createCell((short)column);
  169. cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  170. if(style!=null){
  171. cell.setCellStyle(style);
  172. }
  173. switch(cellType){
  174. caseHSSFCell.CELL_TYPE_BLANK:{
  175. }
  176. break;
  177. caseHSSFCell.CELL_TYPE_STRING:{
  178. cell.setCellValue(value.toString());
  179. }
  180. break;
  181. caseHSSFCell.CELL_TYPE_NUMERIC:{
  182. cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  183. //DecimalFormatformat=newDecimalFormat("###,##0.00");
  184. //cell.setCellValue(Float.parseFloat(value.toString()));
  185. cell.setCellValue(Double.parseDouble(value.toString()));
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. publicvoidsetCssTscService(TscServicecssTscService){
  193. this.cssTscService=cssTscService;
  194. }
  195. }

  1. importjava.io.IOException;
  2. importjava.io.OutputStream;
  3. importjava.util.List;
  4. importjavax.servlet.http.HttpServletResponse;
  5. importorg.apache.poi.hssf.usermodel.HSSFCell;
  6. importorg.apache.poi.hssf.usermodel.HSSFCellStyle;
  7. importorg.apache.poi.hssf.usermodel.HSSFDataFormat;
  8. importorg.apache.poi.hssf.usermodel.HSSFFont;
  9. importorg.apache.poi.hssf.usermodel.HSSFRow;
  10. importorg.apache.poi.hssf.usermodel.HSSFSheet;
  11. importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. importorg.apache.struts2.ServletActionContext;
  13. importcom.neusoft.dcd.css.common.BaseAction;
  14. importcom.neusoft.dcd.css.model.management.user.UserBaseInfo;
  15. importcom.neusoft.dcd.css.model.tsc.Tsc;
  16. importcom.neusoft.dcd.css.service.tsc.TscService;
  17. importcom.opensymphony.xwork2.Preparable;
  18. publicclassExportExcel{
  19. privatestaticfinallongserialVersionUID=1L;
  20. privateTscServicecssTscService;
  21. publicvoidprepare()throwsException{
  22. }
  23. /**
  24. *绘出Excel
  25. *
  26. *@paramactionMapping
  27. *@paramactionForm
  28. *@paramrequest
  29. *@paramresponse
  30. *@return
  31. *@throwsException
  32. */
  33. publicStringinit()throwsException{
  34. UserBaseInfouserinfo=(UserBaseInfo)ServletActionContext
  35. .getRequest().getSession().getAttribute("UserBaseInfo");
  36. if(userinfo!=null){
  37. HttpServletResponseresponse=ServletActionContext.getResponse();
  38. ListdataList=cssTscService.queryForProTsclb(userinfo.getUserId());
  39. HSSFWorkbookworkbook=exportExcel(dataList);
  40. if(workbook!=null){
  41. this.printExcel(workbook,response,"ExcelTsc.xls");
  42. }
  43. }
  44. returnSUCCESS;
  45. }
  46. //导出Excel
  47. privatevoidprintExcel(HSSFWorkbookworkbook,HttpServletResponseresponse,Stringstring)throwsIOException{
  48. OutputStreamout=response.getOutputStream();
  49. response.setHeader("Content-disposition","attachment;filename="+"TSC.xls");
  50. response.setContentType("application/msexcel;charset=UTF-8");
  51. workbook.write(out);
  52. out.flush();
  53. out.close();
  54. }
  55. publicHSSFWorkbookexportExcel(ListdataList)throwsException
  56. {
  57. HSSFWorkbookworkbook=null;
  58. try
  59. {
  60. //这里的数据即时你要从后台取得的数据
  61. //创建工作簿实例
  62. workbook=newHSSFWorkbook();
  63. //创建工作表实例
  64. HSSFSheetsheet=workbook.createSheet("TscExcel");
  65. //设置列宽
  66. this.setSheetColumnWidth(sheet);
  67. //获取样式
  68. HSSFCellStylestyle=this.createTitleStyle(workbook);
  69. //
  70. if(dataList!=null&&dataList.size()>0)
  71. {
  72. //创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
  73. HSSFRowrow=sheet.createRow((short)0);//建立新行
  74. this.createCell(row,0,style,HSSFCell.CELL_TYPE_STRING,
  75. this.getText("css.tsc.asktime"));
  76. this.createCell(row,1,style,HSSFCell.CELL_TYPE_STRING,
  77. this.getText("css.tsc.productname"));
  78. this.createCell(row,2,style,HSSFCell.CELL_TYPE_STRING,
  79. this.getText("css.tsc.passsort"));
  80. this.createCell(row,3,style,HSSFCell.CELL_TYPE_STRING,
  81. this.getText("css.tsc.askusername"));
  82. this.createCell(row,4,style,HSSFCell.CELL_TYPE_STRING,
  83. this.getText("css.tsc.dtype"));
  84. this.createCell(row,5,style,HSSFCell.CELL_TYPE_STRING,
  85. this.getText("css.tsc.askone"));
  86. this.createCell(row,6,style,HSSFCell.CELL_TYPE_STRING,
  87. this.getText("css.tsc.department"));
  88. this.createCell(row,7,style,HSSFCell.CELL_TYPE_STRING,
  89. this.getText("css.tsc.requesttime"));
  90. this.createCell(row,8,style,HSSFCell.CELL_TYPE_STRING,
  91. this.getText("css.tsc.answertime"));
  92. this.createCell(row,9,style,HSSFCell.CELL_TYPE_STRING,
  93. this.getText("css.tsc.requesttow"));
  94. //给excel填充数据
  95. for(inti=0;i<dataList.size();i++)
  96. {
  97. //将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦
  98. Tscmodel=(Tsc)dataList.get(i);
  99. HSSFRowrow1=sheet.createRow((short)(i+1));//建立新行
  100. if(model.getAsktime()!=null)
  101. this.createCell(row1,0,style,HSSFCell.CELL_TYPE_STRING,
  102. model.getAsktime());
  103. if(model.getProductname()!=null)
  104. this.createCell(row1,1,style,HSSFCell.CELL_TYPE_STRING,
  105. model.getProductname());
  106. if(model.getPasssort()!=null)
  107. this.createCell(row1,2,style,HSSFCell.CELL_TYPE_STRING,
  108. model.getPasssort());
  109. if(model.getAskusername()!=null)
  110. this.createCell(row1,3,style,HSSFCell.CELL_TYPE_STRING,
  111. model.getAskusername());
  112. if(model.getDtype()!=null)
  113. this.createCell(row1,4,style,HSSFCell.CELL_TYPE_STRING,
  114. model.getDtype());
  115. if(model.getAskone()!=null)
  116. this.createCell(row1,5,style,HSSFCell.CELL_TYPE_STRING,
  117. model.getAskone());
  118. if(model.getDepartment()!=null)
  119. this.createCell(row1,6,style,HSSFCell.CELL_TYPE_STRING,
  120. model.getDepartment());
  121. if(model.getRequesttime()!=null)
  122. this.createCell(row1,7,style,HSSFCell.CELL_TYPE_STRING,
  123. model.getRequesttime());
  124. if(model.getAnswertime()!=null)
  125. this.createCell(row1,8,style,HSSFCell.CELL_TYPE_STRING,
  126. model.getAnswertime());
  127. if(model.getRequesttow()!=null)
  128. this.createCell(row1,9,style,HSSFCell.CELL_TYPE_STRING,
  129. model.getRequesttow());
  130. }
  131. }
  132. else{
  133. this.createCell(sheet.createRow(0),0,style,
  134. HSSFCell.CELL_TYPE_STRING,"查无资料");
  135. }
  136. }catch(Exceptione)
  137. {
  138. e.printStackTrace();
  139. }
  140. returnworkbook;
  141. }
  142. privatevoidsetSheetColumnWidth(HSSFSheetsheet)
  143. {
  144. //根据你数据里面的记录有多少列,就设置多少列
  145. sheet.setColumnWidth((short)0,(short)3000);
  146. sheet.setColumnWidth((short)1,(short)3000);
  147. sheet.setColumnWidth((short)2,(short)3000);
  148. sheet.setColumnWidth((short)3,(short)3000);
  149. sheet.setColumnWidth((short)4,(short)5000);
  150. sheet.setColumnWidth((short)5,(short)5000);
  151. sheet.setColumnWidth((short)6,(short)5000);
  152. sheet.setColumnWidth((short)7,(short)5000);
  153. sheet.setColumnWidth((short)8,(short)5000);
  154. sheet.setColumnWidth((short)9,(short)5000);
  155. }
  156. //设置excel的title样式
  157. privateHSSFCellStylecreateTitleStyle(HSSFWorkbookwb){
  158. HSSFFontboldFont=wb.createFont();
  159. boldFont.setFontHeight((short)200);
  160. HSSFCellStylestyle=wb.createCellStyle();
  161. style.setFont(boldFont);
  162. style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
  163. returnstyle;
  164. }
  165. //创建Excel单元格
  166. privatevoidcreateCell(HSSFRowrow,intcolumn,HSSFCellStylestyle,
  167. intcellType,Objectvalue){
  168. HSSFCellcell=row.createCell((short)column);
  169. cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  170. if(style!=null){
  171. cell.setCellStyle(style);
  172. }
  173. switch(cellType){
  174. caseHSSFCell.CELL_TYPE_BLANK:{
  175. }
  176. break;
  177. caseHSSFCell.CELL_TYPE_STRING:{
  178. cell.setCellValue(value.toString());
  179. }
  180. break;
  181. caseHSSFCell.CELL_TYPE_NUMERIC:{
  182. cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  183. //DecimalFormatformat=newDecimalFormat("###,##0.00");
  184. //cell.setCellValue(Float.parseFloat(value.toString()));
  185. cell.setCellValue(Double.parseDouble(value.toString()));
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. publicvoidsetCssTscService(TscServicecssTscService){
  193. this.cssTscService=cssTscService;
  194. }
  195. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics