1. public ActionResult excelPrint() {
  2. HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
  3. HSSFSheet sheet = 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. HSSFCellStyle sheetStyle = 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 (int i = 0; i <= 14; i++) {
  24. sheet.setDefaultColumnStyle((short) i, sheetStyle);
  25. }
  26. // 设置字体
  27. HSSFFont headfont = workbook.createFont();
  28. headfont.setFontName("黑体");
  29. headfont.setFontHeightInPoints((short) 22);// 字体大小
  30. headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
  31. // 另一个样式
  32. HSSFCellStyle headstyle = 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. HSSFFont columnHeadFont = workbook.createFont();
  40. columnHeadFont.setFontName("宋体");
  41. columnHeadFont.setFontHeightInPoints((short) 10);
  42. columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  43. // 列头的样式
  44. HSSFCellStyle columnHeadStyle = 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. HSSFFont font = workbook.createFont();
  59. font.setFontName("宋体");
  60. font.setFontHeightInPoints((short) 10);
  61. // 普通单元格样式
  62. HSSFCellStyle style = 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. HSSFCellStyle centerstyle = 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. HSSFRow row0 = sheet.createRow(0);
  90. // 设置行高
  91. row0.setHeight((short) 900);
  92. // 创建第一列
  93. HSSFCell cell0 = row0.createCell(0);
  94. cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));
  95. cell0.setCellStyle(headstyle);
  96. CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
  97. sheet.addMergedRegion(range);
  98. // 创建第二行
  99. HSSFRow row1 = sheet.createRow(1);
  100. HSSFCell cell1 = row1.createCell(0);
  101. cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日       前次会议时间:2009年8月24日"));
  102. cell1.setCellStyle(centerstyle);
  103. // 合并单元格
  104. range = new CellRangeAddress(1, 2, 0, 7);
  105. sheet.addMergedRegion(range);
  106. // 第三行
  107. HSSFRow row2 = sheet.createRow(3);
  108. row2.setHeight((short) 750);
  109. HSSFCell cell = row2.createCell(0);
  110. cell.setCellValue(new HSSFRichTextString("责任者"));
  111. cell.setCellStyle(columnHeadStyle);
  112. cell = row2.createCell(1);
  113. cell.setCellValue(new HSSFRichTextString("成熟度排序"));
  114. cell.setCellStyle(columnHeadStyle);
  115. cell = row2.createCell(2);
  116. cell.setCellValue(new HSSFRichTextString("事项"));
  117. cell.setCellStyle(columnHeadStyle);
  118. cell = row2.createCell(3);
  119. cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));
  120. cell.setCellStyle(columnHeadStyle);
  121. cell = row2.createCell(4);
  122. cell.setCellValue(new HSSFRichTextString("上周工作进展"));
  123. cell.setCellStyle(columnHeadStyle);
  124. cell = row2.createCell(5);
  125. cell.setCellValue(new HSSFRichTextString("本周工作计划"));
  126. cell.setCellStyle(columnHeadStyle);
  127. cell = row2.createCell(6);
  128. cell.setCellValue(new HSSFRichTextString("问题和建议"));
  129. cell.setCellStyle(columnHeadStyle);
  130. cell = row2.createCell(7);
  131. cell.setCellValue(new HSSFRichTextString("备 注"));
  132. cell.setCellStyle(columnHeadStyle);
  133. // 访问数据库,得到数据集
  134. List deitelVOList = getEntityManager().queryDeitelVOList();
  135. int m = 4;
  136. int k = 4;
  137. for (int i = 0; i < deitelVOList.size(); i++) {
  138. DeitelVO vo = deitelVOList.get(i);
  139. String dname = vo.getDname();
  140. List workList = vo.getWorkInfoList();
  141. HSSFRow row = sheet.createRow(m);
  142. cell = row.createCell(0);
  143. cell.setCellValue(new HSSFRichTextString(dname));
  144. cell.setCellStyle(centerstyle);
  145. // 合并单元格
  146. range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0);
  147. sheet.addMergedRegion(range);
  148. m = m + workList.size();
  149. for (int j = 0; j < workList.size(); j++) {
  150. Workinfo w = workList.get(j);
  151. // 遍历数据集创建Excel的行
  152. row = sheet.getRow(k + j);
  153. if (null == row) {
  154. row = sheet.createRow(k + j);
  155. }
  156. cell = row.createCell(1);
  157. cell.setCellValue(w.getWnumber());
  158. cell.setCellStyle(centerstyle);
  159. cell = row.createCell(2);
  160. cell.setCellValue(new HSSFRichTextString(w.getWitem()));
  161. cell.setCellStyle(style);
  162. cell = row.createCell(3);
  163. cell.setCellValue(new HSSFRichTextString(w.getWmeting()));
  164. cell.setCellStyle(style);
  165. cell = row.createCell(4);
  166. cell.setCellValue(new HSSFRichTextString(w.getWbweek()));
  167. cell.setCellStyle(style);
  168. cell = row.createCell(5);
  169. cell.setCellValue(new HSSFRichTextString(w.getWtweek()));
  170. cell.setCellStyle(style);
  171. cell = row.createCell(6);
  172. cell.setCellValue(new HSSFRichTextString(w.getWproblem()));
  173. cell.setCellStyle(style);
  174. cell = row.createCell(7);
  175. cell.setCellValue(new HSSFRichTextString(w.getWremark()));
  176. cell.setCellStyle(style);
  177. }
  178. k = k + workList.size();
  179. }
  180. // 列尾
  181. int footRownumber = sheet.getLastRowNum();
  182. HSSFRow footRow = sheet.createRow(footRownumber + 1);
  183. HSSFCell footRowcell = footRow.createCell(0);
  184. footRowcell.setCellValue(new HSSFRichTextString("                    审  定:XXX      审  核:XXX     汇  总:XX"));
  185. footRowcell.setCellStyle(centerstyle);
  186. range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);
  187. sheet.addMergedRegion(range);
  188. HttpServletResponse response = getResponse();
  189. HttpServletRequest request = getRequest();
  190. String filename = "未命名.xls";//设置下载时客户端Excel的名称
  191. // 请见:http://zmx.javaeye.com/blog/622529
  192. filename = Util.encodeFilename(filename, request);
  193. response.setContentType("application/vnd.ms-excel");
  194. response.setHeader("Content-disposition", "attachment;filename=" + filename);
  195. OutputStream ouputStream = response.getOutputStream();
  196. workbook.write(ouputStream);
  197. ouputStream.flush();
  198. ouputStream.close();
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. }
  202. return null;
  203. }

HSSFWorkbook的更多相关文章

  1. HSSFWorkBooK用法

    public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFShe ...

  2. POI Workbook接口和HSSFWorkbook对象和XSSFWorkbook对象操作相应excel版本

    由于HSSFWorkbook只能操作excel2003一下版本,XSSFWorkbook只能操作excel2007以上版本,所以利用Workbook接口创建对应的对象操作excel来处理兼容性 @Te ...

  3. 数据以Excel形式导出导服务器,再将文件读取到客户端另存 以HSSFWorkbook方式实现

    public void exportExcel(List<P2pInfo> repayXist,HttpServletRequest request,HttpServletResponse ...

  4. HSSFWorkbook和XSSFWorkbook的区别

    HSSFWorkbook读取97-2003格式 ,XSSFWorkbook读取2007-2013格式 /** * 读取97-2003格式 * @param filePath 文件路径 * @throw ...

  5. 自己写的POIUtil,主要解决从不同的HSSFWorkbook复制sheet以及根据单元格插入图片等

    复制sheet的原始代码网上找的,但是小问题很多,然后自己动手改了一下: 根据单元格信息动态插入图片,如果单元格有文字,图片的位置会在文字之后,如果同样的位置已有图片则会往下插入. import or ...

  6. HSSFWorkbook 导出excel java

    public String exportExcelList(){ //创建webbook,对应一个excel文件 HSSFWorkbook wb = new HSSFWorkbook(); //在we ...

  7. HSSFWorkbook操作excel读写

    //exlel读操作 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Ite ...

  8. 关于poi导出excel三种方式HSSFWorkbook,SXSSFWorkbook,csv的总结

    poi导出excel最常用的是第一种方式HSSFWorkbook,不过这种方式数据量大的话会产生内存溢出问题,SXSSFWorkbook是一种大数据量导出格式,csv是另一种excel导出的一种轻快的 ...

  9. 关于poi导出excel方式HSSFWorkbook(xls).XSSFWorkbook(xlsx).SXSSFWorkbook.csv的总结

    1.HSSFWorkbook(xls) import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermo ...

  10. 项目总结06:Java Excel文件导入功能HSSFWorkbook(xls)和 XSSFWorkbook (xlsx)

    项目中碰到Excel文件打入数据功能Mark一下 package com.blue.pem.client.util; import java.io.File; import java.io.FileI ...

随机推荐

  1. Python tldextract模块准确获取域名和后缀

    Python tldextract 模块 - 功能说明 tldextract准确地从URL的域名和子域名分离通用顶级域名或国家顶级域名. 例如,http://www.google.com,你只想取出连 ...

  2. Repeated Substrings(UVAlive 6869)

    题意:求出现过两次以上的不同子串有多少种. /* 用后缀数组求出height[]数组,然后扫一遍, 发现height[i]-height[i-1]>=0,就ans+=height[i]-heig ...

  3. Spring定义的五种事务隔离级别

    在Spring中定义了5中不同的事务隔离级别. 1. ISOLATION_DEFAULT(一般情况下使用这种配置既可) 这是一个PlatfromTransactionManager默认的隔离级别,使用 ...

  4. es6总结(四)--对象

  5. iOS8下定位问题解决

    项目是以前iOS7的,升级iOS8后,无法成功获取用户位置.后来才发现iOS8 使用定位需要在infoplist文件中加2个key,然后manager需要加一个方法,指定定位授权机制   在plist ...

  6. bootstrapTable 应用小例(收索)

    <script src="/plugins/My97DatePicker/WdatePicker.js"></script> <!-- Content ...

  7. AC日记——软件包管理器 洛谷 P2416

    题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个 ...

  8. IDEA重新打jar包时报错MANIFEST.MF already exists in VFS

    报错原因:曾经打过jar包了,把之前的包删掉无用,VFS:虚拟文件系统.即使删掉之前的包,信息依然会在此处.故删掉MANIFEST文件夹,重新打包即可解决.

  9. (4)DataTable

    引用 using System.Data; 创建DataTable DataTable dt = new DataTable(); //指定表明,当把这个table添加到dataset时你就可以用da ...

  10. luogu P1510 精卫填海

    题目描述 [版权说明] 本题为改编题. [问题描述] 发鸠之山,其上多柘木.有鸟焉,其状如乌,文首,白喙,赤足,名曰精卫,其名自詨.是炎帝之少女,名曰女娃.女娃游于东海,溺而不返,故为精卫.常衔西山之 ...