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

2013年02月21日 ⁄ 综合 ⁄ 共 9248字 ⁄ 字号    ⁄ 评论关闭
  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. /**
  97. * 合并单元格
  98. *    第一个参数:第一个单元格的行数(从0开始)
  99. *    第二个参数:第二个单元格的行数(从0开始)
  100. *    第三个参数:第一个单元格的列数(从0开始)
  101. *    第四个参数:第二个单元格的列数(从0开始)
  102. */
  103. CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
  104. sheet.addMergedRegion(range);
  105. // 创建第二行
  106. HSSFRow row1 = sheet.createRow(1);
  107. HSSFCell cell1 = row1.createCell(0);
  108. cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日       前次会议时间:2009年8月24日"));
  109. cell1.setCellStyle(centerstyle);
  110. // 合并单元格
  111. range = new CellRangeAddress(1, 2, 0, 7);
  112. sheet.addMergedRegion(range);
  113. // 第三行
  114. HSSFRow row2 = sheet.createRow(3);
  115. row2.setHeight((short) 750);
  116. HSSFCell cell = row2.createCell(0);
  117. cell.setCellValue(new HSSFRichTextString("责任者"));
  118. cell.setCellStyle(columnHeadStyle);
  119. cell = row2.createCell(1);
  120. cell.setCellValue(new HSSFRichTextString("成熟度排序"));
  121. cell.setCellStyle(columnHeadStyle);
  122. cell = row2.createCell(2);
  123. cell.setCellValue(new HSSFRichTextString("事项"));
  124. cell.setCellStyle(columnHeadStyle);
  125. cell = row2.createCell(3);
  126. cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));
  127. cell.setCellStyle(columnHeadStyle);
  128. cell = row2.createCell(4);
  129. cell.setCellValue(new HSSFRichTextString("上周工作进展"));
  130. cell.setCellStyle(columnHeadStyle);
  131. cell = row2.createCell(5);
  132. cell.setCellValue(new HSSFRichTextString("本周工作计划"));
  133. cell.setCellStyle(columnHeadStyle);
  134. cell = row2.createCell(6);
  135. cell.setCellValue(new HSSFRichTextString("问题和建议"));
  136. cell.setCellStyle(columnHeadStyle);
  137. cell = row2.createCell(7);
  138. cell.setCellValue(new HSSFRichTextString("备 注"));
  139. cell.setCellStyle(columnHeadStyle);
  140. // 访问数据库,得到数据集
  141. List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();
  142. int m = 4;
  143. int k = 4;
  144. for (int i = 0; i < deitelVOList.size(); i++) {
  145. DeitelVO vo = deitelVOList.get(i);
  146. String dname = vo.getDname();
  147. List<Workinfo> workList = vo.getWorkInfoList();
  148. HSSFRow row = sheet.createRow(m);
  149. cell = row.createCell(0);
  150. cell.setCellValue(new HSSFRichTextString(dname));
  151. cell.setCellStyle(centerstyle);
  152. // 合并单元格
  153. range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0);
  154. sheet.addMergedRegion(range);
  155. m = m + workList.size();
  156. for (int j = 0; j < workList.size(); j++) {
  157. Workinfo w = 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(new HSSFRichTextString(w.getWitem()));
  168. cell.setCellStyle(style);
  169. cell = row.createCell(3);
  170. cell.setCellValue(new HSSFRichTextString(w.getWmeting()));
  171. cell.setCellStyle(style);
  172. cell = row.createCell(4);
  173. cell.setCellValue(new HSSFRichTextString(w.getWbweek()));
  174. cell.setCellStyle(style);
  175. cell = row.createCell(5);
  176. cell.setCellValue(new HSSFRichTextString(w.getWtweek()));
  177. cell.setCellStyle(style);
  178. cell = row.createCell(6);
  179. cell.setCellValue(new HSSFRichTextString(w.getWproblem()));
  180. cell.setCellStyle(style);
  181. cell = row.createCell(7);
  182. cell.setCellValue(new HSSFRichTextString(w.getWremark()));
  183. cell.setCellStyle(style);
  184. }
  185. k = k + workList.size();
  186. }
  187. // 列尾
  188. int footRownumber = sheet.getLastRowNum();
  189. HSSFRow footRow = sheet.createRow(footRownumber + 1);
  190. HSSFCell footRowcell = footRow.createCell(0);
  191. footRowcell.setCellValue(new HSSFRichTextString("                    审  定:XXX      审  核:XXX     汇  总:XX"));
  192. footRowcell.setCellStyle(centerstyle);
  193. range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);
  194. sheet.addMergedRegion(range);
  195. HttpServletResponse response = getResponse();
  196. HttpServletRequest request = getRequest();
  197. String filename = "未命名.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. OutputStream ouputStream = response.getOutputStream();
  203. workbook.write(ouputStream);
  204. ouputStream.flush();
  205. ouputStream.close();
  206. catch (Exception e) {
  207. e.printStackTrace();
  208. }
  209. return null;
  210. }

HSSFWorkBooK用法 —Excel表的导出和设置的更多相关文章

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

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

  2. JAVA实现创建Excel表并导出(转发)

    <span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background ...

  3. 如果从excel表中导出insert-sql

    =CONCATENATE("INSERT INTO p_act_lottery(actId,status,grantWay,createTime,invalidTime,amount,pri ...

  4. 【ITOO 1】将List数据导出Excel表

    需求描述:在课表导入的时候,首先给用户提供模板(excel),然后将用户填写好的数据读取到list集合中.再进行判空处赋值处理,以及去重处理.这篇博客,主要介绍读取excel表和导出excel表的方法 ...

  5. SSM中使用POI实现excel的导入导出

    环境:导入POI对应的包 环境: Spring+SpringMVC+Mybatis POI对应的包 <dependency> <groupId>org.apache.poi&l ...

  6. ASP.NET导出excel表方法汇总

    asp.net里导出excel表方法汇总  1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...

  7. asp.net使用MVC4框架基于NPOI做导出数据到Excel表

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  8. java中的数据导出到Excel表中

    整个项目中导出数据到.Excel的源码 import java.io.BufferedOutputStream; import java.io.FileInputStream; import java ...

  9. JAVA笔记-如何将百万级数据高效的导出到Excel表单

    今天,一朋友问我使用JAVA有没有什么办法导出百万级的数据到Excel工作表. 当时我的第一个念头就是这真的是一个好疯狂的念头.然后就想假如真的有这样类似的需求,我自己应该怎么做呢? ps: 首先科普 ...

随机推荐

  1. Linux-进程间的通信-信号集函数【转】

    我们已经知道,我们可以通过信号来终止进程,也可以通过信号来在进程间进行通信,程序也可以通过指定信号的关联处理函数来改变信号的默认处理方式,也可以屏蔽某些信号,使其不能传递给进程.那么我们应该如何设定我 ...

  2. 【监控】dubbo监控中心安装

    使用dubbo的话,两个工具是不可少的: 1:dubbo的管理控制台,在之前的笔记中介绍过 2:简易控制中心monitor 简单介绍下monitor: Simple Monitor挂掉不会影响到Con ...

  3. [BZOJ2963][JLOI2011]飞行路线 分层图+spfa

    Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...

  4. 筛选出sql 查询结果中 不包含某个字符

    select * from table1 where patindex('%关键字%' , aa) = 0 select * from table1 where charindex('关键字' , a ...

  5. Spirng boot 启动的时候进行监控检查不通过停止服务与自定义健康监控节点

    基于 spring-boot-starter-actuator   • 前提条件: <dependency>   <groupId>org.springframework.bo ...

  6. python 将16进制转化为2进制

    >>> x='123abc' >>> b=bin())[:] >>> print(b)

  7. ubuntu 14.04 安装 gflags

    1.下载 git clone https://github.com/gflags/gflags 2.编译 进入源码目录(即gflags文件夹) cmake . make -j 24 sudo make ...

  8. JSON序列化反序列化

    JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...

  9. C/C++UNION中包含STRUCT

    测试环境:Win7x64,cn_visual_studio_2010_ultimate_x86_dvd_532347.iso,qt-opensource-windows-x86-msvc2010_op ...

  10. VC++异常处理

    1.测试代码: #include <stdio.h> #include <windows.h> void main() { __try { DWORD dwDemonObj = ...