HSSFWorkBooK用法 ---Excel表的导出和设置

  1. publicActionResultexcelPrint(){
  2. HSSFWorkbookworkbook=newHSSFWorkbook();//创建一个Excel文件
  3. HSSFSheetsheet=workbook.createSheet();//创建一个Excel的Sheet
  4. sheet.createFreezePane(1,3);//冻结
  5. //设置列宽
  6. sheet.setColumnWidth(0,1000);
  7. sheet.setColumnWidth(1,3500);
  8. sheet.setColumnWidth(2,3500);
  9. sheet.setColumnWidth(3,6500);
  10. sheet.setColumnWidth(4,6500);
  11. sheet.setColumnWidth(5,6500);
  12. sheet.setColumnWidth(6,6500);
  13. sheet.setColumnWidth(7,2500);
  14. //Sheet样式
  15. HSSFCellStylesheetStyle=workbook.createCellStyle();
  16. //背景色的设定
  17. sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
  18. //前景色的设定
  19. sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
  20. //填充模式
  21. sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
  22. //设置列的样式
  23. for(inti=0;i<=14;i++){
  24. sheet.setDefaultColumnStyle((short)i,sheetStyle);
  25. }
  26. //设置字体
  27. HSSFFontheadfont=workbook.createFont();
  28. headfont.setFontName("黑体");
  29. headfont.setFontHeightInPoints((short)22);//字体大小
  30. headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
  31. //另一个样式
  32. HSSFCellStyleheadstyle=workbook.createCellStyle();
  33. headstyle.setFont(headfont);
  34. headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
  35. headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
  36. headstyle.setLocked(true);
  37. headstyle.setWrapText(true);//自动换行
  38. //另一个字体样式
  39. HSSFFontcolumnHeadFont=workbook.createFont();
  40. columnHeadFont.setFontName("宋体");
  41. columnHeadFont.setFontHeightInPoints((short)10);
  42. columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  43. //列头的样式
  44. HSSFCellStylecolumnHeadStyle=workbook.createCellStyle();
  45. columnHeadStyle.setFont(columnHeadFont);
  46. columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
  47. columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
  48. columnHeadStyle.setLocked(true);
  49. columnHeadStyle.setWrapText(true);
  50. columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);//左边框的颜色
  51. columnHeadStyle.setBorderLeft((short)1);//边框的大小
  52. columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);//右边框的颜色
  53. columnHeadStyle.setBorderRight((short)1);//边框的大小
  54. columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置单元格的边框为粗体
  55. columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);//设置单元格的边框颜色
  56. //设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
  57. columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
  58. HSSFFontfont=workbook.createFont();
  59. font.setFontName("宋体");
  60. font.setFontHeightInPoints((short)10);
  61. //普通单元格样式
  62. HSSFCellStylestyle=workbook.createCellStyle();
  63. style.setFont(font);
  64. style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左右居中
  65. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);//上下居中
  66. style.setWrapText(true);
  67. style.setLeftBorderColor(HSSFColor.BLACK.index);
  68. style.setBorderLeft((short)1);
  69. style.setRightBorderColor(HSSFColor.BLACK.index);
  70. style.setBorderRight((short)1);
  71. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置单元格的边框为粗体
  72. style.setBottomBorderColor(HSSFColor.BLACK.index);//设置单元格的边框颜色.
  73. style.setFillForegroundColor(HSSFColor.WHITE.index);//设置单元格的背景颜色.
  74. //另一个样式
  75. HSSFCellStylecenterstyle=workbook.createCellStyle();
  76. centerstyle.setFont(font);
  77. centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
  78. centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
  79. centerstyle.setWrapText(true);
  80. centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
  81. centerstyle.setBorderLeft((short)1);
  82. centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
  83. centerstyle.setBorderRight((short)1);
  84. centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置单元格的边框为粗体
  85. centerstyle.setBottomBorderColor(HSSFColor.BLACK.index);//设置单元格的边框颜色.
  86. centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);//设置单元格的背景颜色.
  87. try{
  88. //创建第一行
  89. HSSFRowrow0=sheet.createRow(0);
  90. //设置行高
  91. row0.setHeight((short)900);
  92. //创建第一列
  93. HSSFCellcell0=row0.createCell(0);
  94. cell0.setCellValue(newHSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));
  95. cell0.setCellStyle(headstyle);
  96. /**
  97. *合并单元格
  98. *第一个参数:第一个单元格的行数(从0开始)
  99. *第二个参数:第二个单元格的行数(从0开始)
  100. *第三个参数:第一个单元格的列数(从0开始)
  101. *第四个参数:第二个单元格的列数(从0开始)
  102. */
  103. CellRangeAddressrange=newCellRangeAddress(0,0,0,7);
  104. sheet.addMergedRegion(range);
  105. //创建第二行
  106. HSSFRowrow1=sheet.createRow(1);
  107. HSSFCellcell1=row1.createCell(0);
  108. cell1.setCellValue(newHSSFRichTextString("本次会议时间:2009年8月31日前次会议时间:2009年8月24日"));
  109. cell1.setCellStyle(centerstyle);
  110. //合并单元格
  111. range=newCellRangeAddress(1,2,0,7);
  112. sheet.addMergedRegion(range);
  113. //第三行
  114. HSSFRowrow2=sheet.createRow(3);
  115. row2.setHeight((short)750);
  116. HSSFCellcell=row2.createCell(0);
  117. cell.setCellValue(newHSSFRichTextString("责任者"));
  118. cell.setCellStyle(columnHeadStyle);
  119. cell=row2.createCell(1);
  120. cell.setCellValue(newHSSFRichTextString("成熟度排序"));
  121. cell.setCellStyle(columnHeadStyle);
  122. cell=row2.createCell(2);
  123. cell.setCellValue(newHSSFRichTextString("事项"));
  124. cell.setCellStyle(columnHeadStyle);
  125. cell=row2.createCell(3);
  126. cell.setCellValue(newHSSFRichTextString("前次会议要求/n/新项目的项目概要"));
  127. cell.setCellStyle(columnHeadStyle);
  128. cell=row2.createCell(4);
  129. cell.setCellValue(newHSSFRichTextString("上周工作进展"));
  130. cell.setCellStyle(columnHeadStyle);
  131. cell=row2.createCell(5);
  132. cell.setCellValue(newHSSFRichTextString("本周工作计划"));
  133. cell.setCellStyle(columnHeadStyle);
  134. cell=row2.createCell(6);
  135. cell.setCellValue(newHSSFRichTextString("问题和建议"));
  136. cell.setCellStyle(columnHeadStyle);
  137. cell=row2.createCell(7);
  138. cell.setCellValue(newHSSFRichTextString("备注"));
  139. cell.setCellStyle(columnHeadStyle);
  140. //访问数据库,得到数据集
  141. List<DeitelVO>deitelVOList=getEntityManager().queryDeitelVOList();
  142. intm=4;
  143. intk=4;
  144. for(inti=0;i<deitelVOList.size();i++){
  145. DeitelVOvo=deitelVOList.get(i);
  146. Stringdname=vo.getDname();
  147. List<Workinfo>workList=vo.getWorkInfoList();
  148. HSSFRowrow=sheet.createRow(m);
  149. cell=row.createCell(0);
  150. cell.setCellValue(newHSSFRichTextString(dname));
  151. cell.setCellStyle(centerstyle);
  152. //合并单元格
  153. range=newCellRangeAddress(m,m+workList.size()-1,0,0);
  154. sheet.addMergedRegion(range);
  155. m=m+workList.size();
  156. for(intj=0;j<workList.size();j++){
  157. Workinfow=workList.get(j);
  158. //遍历数据集创建Excel的行
  159. row=sheet.getRow(k+j);
  160. if(null==row){
  161. row=sheet.createRow(k+j);
  162. }
  163. cell=row.createCell(1);
  164. cell.setCellValue(w.getWnumber());
  165. cell.setCellStyle(centerstyle);
  166. cell=row.createCell(2);
  167. cell.setCellValue(newHSSFRichTextString(w.getWitem()));
  168. cell.setCellStyle(style);
  169. cell=row.createCell(3);
  170. cell.setCellValue(newHSSFRichTextString(w.getWmeting()));
  171. cell.setCellStyle(style);
  172. cell=row.createCell(4);
  173. cell.setCellValue(newHSSFRichTextString(w.getWbweek()));
  174. cell.setCellStyle(style);
  175. cell=row.createCell(5);
  176. cell.setCellValue(newHSSFRichTextString(w.getWtweek()));
  177. cell.setCellStyle(style);
  178. cell=row.createCell(6);
  179. cell.setCellValue(newHSSFRichTextString(w.getWproblem()));
  180. cell.setCellStyle(style);
  181. cell=row.createCell(7);
  182. cell.setCellValue(newHSSFRichTextString(w.getWremark()));
  183. cell.setCellStyle(style);
  184. }
  185. k=k+workList.size();
  186. }
  187. //列尾
  188. intfootRownumber=sheet.getLastRowNum();
  189. HSSFRowfootRow=sheet.createRow(footRownumber+1);
  190. HSSFCellfootRowcell=footRow.createCell(0);
  191. footRowcell.setCellValue(newHSSFRichTextString("审定:XXX审核:XXX汇总:XX"));
  192. footRowcell.setCellStyle(centerstyle);
  193. range=newCellRangeAddress(footRownumber+1,footRownumber+1,0,7);
  194. sheet.addMergedRegion(range);
  195. HttpServletResponseresponse=getResponse();
  196. HttpServletRequestrequest=getRequest();
  197. Stringfilename="未命名.xls";//设置下载时客户端Excel的名称
  198. //请见:http://zmx.javaeye.com/blog/622529
  199. filename=Util.encodeFilename(filename,request);
  200. response.setContentType("application/vnd.ms-excel");
  201. response.setHeader("Content-disposition","attachment;filename="+filename);
  202. OutputStreamouputStream=response.getOutputStream();
  203. workbook.write(ouputStream);
  204. ouputStream.flush();
  205. ouputStream.close();
  206. }catch(Exceptione){
  207. e.printStackTrace();
  208. }
  209. returnnull;
  210. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值