1. /**
  2. * 导出复杂excel 合并单元格 (HSSFWorkbook)
  3. */
  4. @GetMapping("/testHSSFWorkbook.do")
  5. public void testExport1(HttpServletResponse response)
  6. throws Exception
  7. {
  8. /** 第一步,创建一个Workbook,对应一个Excel文件 */
  9. HSSFWorkbook wb = new HSSFWorkbook();
  10.  
  11. /** 第二步,在Workbook中添加一个sheet,对应Excel文件中的sheet */
  12. HSSFSheet sheet = wb.createSheet("南京市城镇社会保险参保人员花名册");
  13.  
  14. /** 第三步,设置样式以及字体样式*/
  15. HSSFCellStyle titleStyle = createTitleCellStyle(wb);
  16. HSSFCellStyle headerStyle = createHeadCellStyle(wb);
  17. HSSFCellStyle contentStyle = createContentCellStyle(wb);
  18.  
  19. /** 第四步,创建标题 ,合并标题单元格 */
  20. // 行号
  21. int rowNum = 0;
  22. // 总列数
  23. int totalColumn = 10;
  24. // 创建第一页的第一行,索引从0开始
  25. HSSFRow row0 = sheet.createRow(rowNum++);
  26. row0.setHeight((short) 800);// 设置行高
  27.  
  28. String title = "南京市城镇社会保险参保人员花名册";
  29. HSSFCell c00 = row0.createCell(0);
  30. c00.setCellValue(title);
  31. c00.setCellStyle(titleStyle);
  32. // 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始)
  33. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, totalColumn));//标题合并单元格操作,11+1为总列数
  34.  
  35. // 第二行
  36. HSSFRow row1 = sheet.createRow(rowNum++);
  37. row1.setHeight((short) 500);
  38. String[] row_first = {"单位名称(公章):", "", "", "", "", "", "", "", "劳动和社保保障证号(单位代码):", "", ""};
  39. for (int i = 0; i < row_first.length; i++)
  40. {
  41. HSSFCell tempCell = row1.createCell(i);
  42. tempCell.setCellStyle(headerStyle);
  43. tempCell.setCellValue(row_first[i]);
  44. }
  45.  
  46. //第三行
  47. HSSFRow row2 = sheet.createRow(rowNum++);
  48. row2.setHeight((short) 700);
  49. String[] row_second = {"序号", "社会保障卡号(个人代码)", "姓名", "性别", "身份证号码", "民族", "户口性质", "参加工作时间",
  50. "进本单位参保时间", "月缴费基数(元)", "备注"};
  51. for (int i = 0; i < row_second.length; i++)
  52. {
  53. HSSFCell tempCell = row2.createCell(i);
  54. tempCell.setCellValue(row_second[i]);
  55. tempCell.setCellStyle(headerStyle);
  56. }
  57.  
  58. //循环每一行数据
  59. List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); //查询出来的数据
  60. Map<String,Object> map = new HashMap<String,Object>();
  61. map.put("name", "1");
  62. map.put("r1", "111");
  63. map.put("r2", "222");
  64. map.put("r3", "333");
  65. map.put("r4", "444");
  66. map.put("r5", "555");
  67. map.put("r6", "666");
  68. dataList.add(map);
  69. dataList.add(map);//加多一条list
  70.  
  71. for (Map<String, Object> excelData : dataList)
  72. {
  73. HSSFRow tempRow = sheet.createRow(rowNum++);
  74. tempRow.setHeight((short) 500);
  75. // 循环单元格填入数据
  76. for (int j = 0; j < 7; j++)
  77. {
  78. HSSFCell tempCell = tempRow.createCell(j);
  79. tempCell.setCellStyle(contentStyle);
  80. String tempValue;
  81. if (j == 0)
  82. {
  83. // 乡镇、街道名称
  84. tempValue = excelData.get("name").toString();
  85. }
  86. else if (j == 1)
  87. {
  88. // 登记数(人)
  89. tempValue = excelData.get("r1").toString();
  90. }
  91. else if (j == 2)
  92. {
  93. // 办证总数(人)
  94. tempValue = excelData.get("r2").toString();
  95. }
  96. else if (j == 3)
  97. {
  98. // 办证率(%)
  99. tempValue = excelData.get("r3").toString();
  100. }
  101. else if (j == 4)
  102. {
  103. // 登记户数(户)
  104. tempValue = excelData.get("r4").toString();
  105. }
  106. else if (j == 5)
  107. {
  108. // 签订数(份)
  109. tempValue = excelData.get("r5").toString();
  110. }
  111. else
  112. {
  113. // 备注
  114. tempValue = excelData.get("r6").toString();
  115. }
  116. tempCell.setCellValue(tempValue);
  117. }
  118. }
  119.  
  120. // 尾行备注1
  121. HSSFRow foot1 = sheet.createRow(rowNum++);
  122. foot1.setHeight((short) 300);
  123. String[] row_foot1 = {"备注: 1、“参加工作时间”:指首次参加工作时间。", "", "", "", "", "", "",
  124. "", "5、机关事业单位人员应注明是否参加养老保险。", "", ""};
  125. for (int i = 0; i < row_foot1.length; i++)
  126. {
  127. HSSFCell tempCell = foot1.createCell(i);
  128. tempCell.setCellValue(row_foot1[i]);
  129. }
  130. int remarkRowNum1 = dataList.size() + 3;
  131. // 合并
  132. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum1, remarkRowNum1, 0, 2));//备注1
  133. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum1, remarkRowNum1, 8, 10));//备注5
  134.  
  135. // 尾行备注2
  136. HSSFRow foot2 = sheet.createRow(rowNum++);
  137. foot2.setHeight((short) 300);
  138. String[] row_foot2 = {" 2、“进本单位参保时间”:指在本单位缴费起始时间。", "", "", "", "", "", "",
  139. "", "6、本表一式两份,社会保险经办机构、缴费单位各留一份", "", ""};
  140. for (int i = 0; i < row_foot2.length; i++)
  141. {
  142. HSSFCell tempCell = foot2.createCell(i);
  143. tempCell.setCellValue(row_foot2[i]);
  144. }
  145. int remarkRowNum2 = dataList.size() + 4;
  146. // 合并
  147. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum2, remarkRowNum2, 0, 2));//备注1
  148. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum2, remarkRowNum2, 8, 10));//备注5
  149.  
  150. // 尾行备注3
  151. HSSFRow foot3 = sheet.createRow(rowNum++);
  152. foot3.setHeight((short) 300);
  153. String[] row_foot3 = {" 3、“月缴费基数”:指月平均收入总和。", "", "", "", "", "", "",
  154. "", "7、社会保险政策咨询电话:12333", "", ""};
  155. for (int i = 0; i < row_foot3.length; i++)
  156. {
  157. HSSFCell tempCell = foot3.createCell(i);
  158. tempCell.setCellValue(row_foot3[i]);
  159. }
  160. int remarkRowNum3 = dataList.size() + 5;
  161. // 合并
  162. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum3, remarkRowNum3, 0, 2));//备注1
  163. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum3, remarkRowNum3, 8, 10));//备注5
  164.  
  165. // 尾行备注4
  166. HSSFRow foot4 = sheet.createRow(rowNum++);
  167. foot4.setHeight((short) 300);
  168. String[] row_foot4 = {" 4、跨社会保险结算年度办理补缴时须出具劳动合同原件、工资报表原件等相关资料。",
  169. "", "", "", "", "", "", "", "8、南京人力资源和社会保障网址:www.njhrss.gov.cn", "", ""};
  170. for (int i = 0; i < row_foot4.length; i++)
  171. {
  172. HSSFCell tempCell = foot4.createCell(i);
  173. tempCell.setCellValue(row_foot4[i]);
  174. }
  175. int remarkRowNum4 = dataList.size() + 6;
  176. // 合并
  177. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum4, remarkRowNum4, 0, 2));//备注1
  178. sheet.addMergedRegion(new CellRangeAddress(remarkRowNum4, remarkRowNum4, 8, 10));//备注5
  179.  
  180. // 最后一行
  181. HSSFRow foot = sheet.createRow(rowNum++);
  182. foot.setHeight((short) 300);
  183. String[] row_foot = {"单位负责人:", "", "填报人:", "", "联系电话:", "", "", "", "", "填报日期: 年 月 日", ""};
  184. for (int i = 0; i < row_foot.length; i++)
  185. {
  186. HSSFCell tempCell = foot.createCell(i);
  187. tempCell.setCellValue(row_foot[i]);
  188. }
  189.  
  190. for (int i = 0; i <= totalColumn; i++)
  191. {
  192. sheet.autoSizeColumn((short)i,true); //调整列宽
  193. }
  194.  
  195. //导出
  196. String fileName = "南京市城镇社会保险参保人员花名册.xls";
  197. try
  198. {
  199. response.setCharacterEncoding("UTF-8");
  200. response.setHeader("content-Type", "application/vnd.ms-excel");
  201. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  202.  
  203. OutputStream stream = response.getOutputStream();
  204. if (null != wb && null != stream)
  205. {
  206. wb.write(stream);// 将数据写出去
  207. wb.close();
  208. stream.close();
  209. }
  210. }
  211. catch (Exception e)
  212. {
  213. e.printStackTrace();
  214. }
  215. }

导出结果如图

POI导出复杂Excel,合并单元格(1)的更多相关文章

  1. Java poi导出设置 Excel某些单元格不可编辑

    小白的总结,大神勿喷:需要转载请说明出处,如果有什么问题,欢迎留言 一.需求: 1.某一列 .某一行或某些单元格不可编辑,其他列可以编辑 二.期间遇到的问题 1.无法设置成不可编辑 2.设置为不可编辑 ...

  2. asp.net C#取Excel 合并单元格内容

    asp教程.net c#取excel 合并单元格内容读取excel数据,填充dataset// 连接字符串 string xlspath = server.mappath("~/www.11 ...

  3. 使用POI创建word表格合并单元格兼容wps

    poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...

  4. java poi导出Excel合并单元格并设置边框

    import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; i ...

  5. poi导出excel合并单元格(包括列合并、行合并)

    1 工程所需jar包如下:commons-codec-1.5.jarcommons-logging-1.1.jarlog4j-1.2.13.jarjunit-3.8.1.jarpoi-3.9-2012 ...

  6. poi excel 合并单元格

    结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,        colId, colId + c ...

  7. 前端Excel表格导入导出,包括合并单元格,表格自定义样式等

    表格数据导入 读取导入Excel表格数据这里采用的是 xlsx 插件 npm i xlsx 读取excel需要通过 XLSX.read(data, {type: type}) 方法来实现,返回一个叫W ...

  8. npoi导出excel合并单元格

    需要引用NPOI.dll程序集和Ionic.Zip.dll程序集 string[] headerRowName = { "序号", "地市", "镇街 ...

  9. NPOI之Excel——合并单元格、设置样式、输入公式、设置筛选等

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

  10. NPOI之Excel——合并单元格、设置样式、输入公式

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

随机推荐

  1. AcWing周赛43

    AcWing周赛43 题源:https://www.acwing.com/activity/content/1233/ 4314. 三元组 直接暴力做就是了,我一开始还在找规律..悲 我滴代码 #in ...

  2. 2021.08.06 P2441 角色属性树(树形结构)

    2021.08.06 P2441 角色属性树(树形结构) P2441 角色属性树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 求离x最近的祖先y且(x,y)>1. ...

  3. Dockerfile 命令详解及最佳实践

    Dockerfile 命令详解 FROM 指定基础镜像(必选) 所谓定制镜像,那一定是以一个镜像为基础,在其上进行定制.就像我们之前运行了一个 nginx 镜像的容器,再进行修改一样,基础镜像是必须指 ...

  4. Rust如何开发eBPF应用(一)?

    前言 eBPF是一项革命性的技术,可以在Linux内核中运行沙盒程序,而无需重新编译内核或加载内核模块.它能够在许多内核 hook 点安全地执行字节码,主要应用在云原生网络.安全.跟踪监控等方面. e ...

  5. HTTP:聊一聊HTTP中的强制缓存

    http响应response headers中会有一个cache-control,这个参数就是用来做强制缓存的 一.什么是强制缓存 强制缓存就是服务端告诉客户端某些资源(JS CSS IMG)需要按照 ...

  6. Python连接数据库,列表输出数据库中的某一列

    1 import pymysql 2 import pandas as pd 3 import numpy as np 4 #连接数据库,地址,端口,用户名,密码,数据库名称,数据格式 5 conn ...

  7. css3 做出顶边倾斜的 梯形 div

    效果图: <html> <head> <meta charset="utf-8"> <title>顶边倾斜的div梯形</ti ...

  8. Vert.X CompositeFuture 用法

    CompositeFuture 是一种特殊的 Future,它可以包装一个 Future 列表,从而让一组异步操作并行执行:然后协调这一组操作的结果,作为 CompositeFuture 的结果.本文 ...

  9. 数据交换格式 JSON

    1. 什么是 JSON 概念 : JSON 的英文全称是 JavaScript ObjEct Notation, 即 "JavaScript 对象表示法" . 简单来讲 : JSO ...

  10. 详谈:pNFS增强文件系统架构

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 通过 NFS(由服务器.客户机软件和两者之间的协议组成) ...