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

论坛 期权论坛 脚本     
匿名技术用户   2021-1-2 18:43   11   0
1 工程所需jar包如下:
commons-codec-1.5.jar
commons-logging-1.1.jar
log4j-1.2.13.jar
junit-3.8.1.jar
poi-3.9-20121203.jar

2 Code:
Java代码
  1. /**
  2. * 导出设备信息Excel
  3. * @param form 和 HTTP 请求相关的表格对象
  4. * @param resources 信息资源对象
  5. * @param locale 本地化对象
  6. * @param session HTTP 会话对象
  7. * @param request HTTP 请求对象
  8. * @param response HTTP 响应对象
  9. * @return
  10. */
  11. public String exportExcel(DynaBean form, MessageResources resources,
  12. Locale locale, HttpSession session, HttpServletRequest request,
  13. HttpServletResponse response) throws Exception{
  14. int iLanguage = (locale.getLanguage().indexOf("en")>=0)?0:1;
  15. response.reset();
  16. response.setContentType("application/vnd.ms-excel");
  17. response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(resources.getMessage(locale, "device.details")+".xls","UTF-8"));
  18. OutputStream sos = response.getOutputStream();
  19. List<DeviceVO> deviceList = dao.getAllDevice();
  20. HSSFWorkbook wb = new HSSFWorkbook();
  21. Map<String, CellStyle> styles = createStyles(wb);
  22. // 创建sheet页
  23. Sheet sheet = wb.createSheet("Sheet");
  24. PrintSetup printSetup = sheet.getPrintSetup();
  25. printSetup.setLandscape(true);
  26. sheet.setFitToPage(true);
  27. sheet.setHorizontallyCenter(true);
  28. /**
  29. *合并单元格的行或者列
  30. */
  31. sheet.addMergedRegion(CellRangeAddress.valueOf("$F$1:$H$1"));
  32. sheet.addMergedRegion(CellRangeAddress.valueOf("$M$1:$P$1"));
  33. sheet.addMergedRegion(CellRangeAddress.valueOf("$Q$1:$S$1"));
  34. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$A$2"));
  35. sheet.addMergedRegion(CellRangeAddress.valueOf("$B$1:$B$2"));
  36. sheet.addMergedRegion(CellRangeAddress.valueOf("$C$1:$C$2"));
  37. sheet.addMergedRegion(CellRangeAddress.valueOf("$D$1:$D$2"));
  38. sheet.addMergedRegion(CellRangeAddress.valueOf("$E$1:$E$2"));
  39. sheet.addMergedRegion(CellRangeAddress.valueOf("$I$1:$I$2"));
  40. sheet.addMergedRegion(CellRangeAddress.valueOf("$J$1:$J$2"));
  41. sheet.addMergedRegion(CellRangeAddress.valueOf("$K$1:$K$2"));
  42. sheet.addMergedRegion(CellRangeAddress.valueOf("$L$1:$L$2"));
  43. sheet.addMergedRegion(CellRangeAddress.valueOf("$T$1:$T$2"));
  44. sheet.addMergedRegion(CellRangeAddress.valueOf("$U$1:$U$2"));
  45. sheet.addMergedRegion(CellRangeAddress.valueOf("$V$1:$V$2"));
  46. sheet.addMergedRegion(CellRangeAddress.valueOf("$W$1:$W$2"));
  47. sheet.addMergedRegion(CellRangeAddress.valueOf("$X$1:$X$2"));
  48. sheet.addMergedRegion(CellRangeAddress.valueOf("$Y$1:$Y$2"));
  49. sheet.addMergedRegion(CellRangeAddress.valueOf("$Z$1:$Z$2"));
  50. sheet.addMergedRegion(CellRangeAddress.valueOf("$AA$1:$AA$2"));
  51. sheet.addMergedRegion(CellRangeAddress.valueOf("$AB$1:$AB$2"));
  52. // 创建表头
  53. Row headerRow = sheet.createRow(0);
  54. headerRow.setHeightInPoints(30);
  55. Cell headerCell;
  56. headerCell = headerRow.createCell(0);
  57. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.number")); //设备编号
  58. headerCell.setCellStyle(styles.get("header"));
  59. headerCell = headerRow.createCell(1);
  60. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.qrcode")); //设备二维码
  61. headerCell.setCellStyle(styles.get("header"));
  62. headerCell = headerRow.createCell(2);
  63. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customerbase")); //客户群
  64. headerCell.setCellStyle(styles.get("header"));
  65. headerCell = headerRow.createCell(3);
  66. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customertype")); //客户类别
  67. headerCell.setCellStyle(styles.get("header"));
  68. headerCell = headerRow.createCell(4);
  69. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customername")); //客户名称
  70. headerCell.setCellStyle(styles.get("header"));
  71. headerCell = headerRow.createCell(5);
  72. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.area")); //设备区域
  73. headerCell.setCellStyle(styles.get("header"));
  74. headerCell = headerRow.createCell(8);
  75. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.itemname")); //所属项目名称
  76. headerCell.setCellStyle(styles.get("header"));
  77. headerCell = headerRow.createCell(9);
  78. headerCell.setCellValue(resources.getMessage(locale, "device.category")); //设备类别
  79. headerCell.setCellStyle(styles.get("header"));
  80. headerCell = headerRow.createCell(10);
  81. headerCell.setCellValue(resources.getMessage(locale, "device.name")); //设备名称
  82. headerCell.setCellStyle(styles.get("header"));
  83. headerCell = headerRow.createCell(11);
  84. headerCell.setCellValue(resources.getMessage(locale, "device.no")); //设备信息编号
  85. headerCell.setCellStyle(styles.get("header"));
  86. headerCell = headerRow.createCell(12);
  87. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.baseinfomation")); //设备基本信息
  88. headerCell.setCellStyle(styles.get("header"));
  89. headerCell = headerRow.createCell(16);
  90. headerCell.setCellValue(resources.getMessage(locale, "device.location")); //设备位置
  91. headerCell.setCellStyle(styles.get("header"));
  92. headerCell = headerRow.createCell(19);
  93. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.enabledate")); //设备启用日期
  94. headerCell.setCellStyle(styles.get("header"));
  95. headerCell = headerRow.createCell(20);
  96. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.backendload")); //后端负载
  97. headerCell.setCellStyle(styles.get("header"));
  98. headerCell = headerRow.createCell(21);
  99. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.operationtips")); //操作提示
  100. headerCell.setCellStyle(styles.get("header"));
  101. headerCell = headerRow.createCell(22);
  102. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.maintenancepeople")); //维护责任人
  103. headerCell.setCellStyle(styles.get("header"));
  104. headerCell = headerRow.createCell(23);
  105. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.usetime")); //巡检时间
  106. headerCell.setCellStyle(styles.get("header"));
  107. headerCell = headerRow.createCell(24);
  108. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.intervaltime")); //巡检下台设备时间间隔
  109. headerCell.setCellStyle(styles.get("header"));
  110. headerCell = headerRow.createCell(25);
  111. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.patrolpath")); //巡检路径
  112. headerCell.setCellStyle(styles.get("header"));
  113. headerCell = headerRow.createCell(26);
  114. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.enable")); //是否启用巡检路径
  115. headerCell.setCellStyle(styles.get("header"));
  116. headerCell = headerRow.createCell(27);
  117. headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.other")); //其他信息
  118. headerCell.setCellStyle(styles.get("header"));
  119. Row headerRowRegion = sheet.createRow(1);
  120. headerRowRegion.setHeightInPoints(15);
  121. Cell headerCellRegion;
  122. headerCellRegion = headerRowRegion.createCell(5);
  123. headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.province")); //省份
  124. headerCellRegion.setCellStyle(styles.get("header"));
  125. headerCellRegion = headerRowRegion.createCell(6);
  126. headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.region")); //市
  127. headerCellRegion.setCellStyle(styles.get("header"));
  128. headerCellRegion = headerRowRegion.createCell(7);
  129. headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.county")); //县/区
  130. headerCellRegion.setCellStyle(styles.get("header"));
  131. headerCellRegion = headerRowRegion.createCell(12);
  132. headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.brand")); //品牌
  133. headerCellRegion.setCellStyle(styles.get("header"));
  134. headerCellRegion = headerRowRegion.createCell(13);
  135. headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.model")); //型号
  136. headerCellRegion.setCellStyle(styles.get("header"));
  137. headerCellRegion = headerRowRegion.createCell(14);
  138. headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.capacity")); //容量
  139. headerCellRegion.setCellStyle(styles.get("header"));
  140. headerCellRegion = headerRowRegion.createCell(15);
  141. headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.systemtype")); //系统类型
  142. headerCellRegion.setCellStyle(styles.get("header"));
  143. headerCellRegion = headerRowRegion.createCell(16);
  144. headerCellRegion.setCellValue(resources.getMessage(locale, "device.build")); //楼号
  145. headerCellRegion.setCellStyle(styles.get("header"));
  146. headerCellRegion = headerRowRegion.createCell(17);
  147. headerCellRegion.setCellValue(resources.getMessage(locale, "device.floor")); //楼层
  148. headerCellRegion.setCellStyle(styles.get("header"));
  149. headerCellRegion = headerRowRegion.createCell(18);
  150. headerCellRegion.setCellValue(resources.getMessage(locale, "device.room")); //房间号
  151. headerCellRegion.setCellStyle(styles.get("header"));
  152. for(int i=0;i<deviceList.size();i++){
  153. DeviceVO device = deviceList.get(i);
  154. Cell rowCell;
  155. Row cellRow = sheet.createRow(i + 2);
  156. rowCell = cellRow.createCell(0);
  157. rowCell.setCellValue(device.getDeviceId());
  158. rowCell.setCellStyle(styles.get("cell"));
  159. rowCell = cellRow.createCell(1);
  160. rowCell.setCellValue(device.getQrcode());
  161. rowCell.setCellStyle(styles.get("cell"));
  162. rowCell = cellRow.createCell(2);
  163. //判断是否是中文
  164. if(iLanguage==1){
  165. rowCell.setCellValue(device.getItemId().getCustomerBase().substring(device.getItemId().getCustomerBase().indexOf(":")+1,device.getItemId().getCustomerBase().length()));
  166. }else if(iLanguage==0){ //判断是否是英文
  167. rowCell.setCellValue(device.getItemId().getCustomerBase().substring(0,device.getItemId().getCustomerBase().indexOf(":")));
  168. }
  169. rowCell.setCellStyle(styles.get("cell"));
  170. rowCell = cellRow.createCell(3);
  171. //判断是否是中文
  172. if(iLanguage==1){
  173. rowCell.setCellValue(device.getItemId().getCustomerType().substring(device.getItemId().getCustomerType().indexOf(":")+1,device.getItemId().getCustomerType().length()));
  174. }else if(iLanguage==0){ //判断是否是英文
  175. rowCell.setCellValue(device.getItemId().getCustomerType().substring(0,device.getItemId().getCustomerType().indexOf(":")));
  176. }
  177. rowCell.setCellStyle(styles.get("cell"));
  178. rowCell = cellRow.createCell(4);
  179. rowCell.setCellValue(device.getItemId().getCustomerName());
  180. rowCell.setCellStyle(styles.get("cell"));
  181. rowCell = cellRow.createCell(5);
  182. rowCell.setCellValue(device.getItemId().getProvince());
  183. rowCell.setCellStyle(styles.get("cell"));
  184. rowCell = cellRow.createCell(6);
  185. rowCell.setCellValue(device.getItemId().getCity());
  186. rowCell.setCellStyle(styles.get("cell"));
  187. rowCell = cellRow.createCell(7);
  188. rowCell.setCellValue(device.getItemId().getCounty());
  189. rowCell.setCellStyle(styles.get("cell"));
  190. rowCell = cellRow.createCell(8);
  191. //判断是否是中文
  192. if(iLanguage==1){
  193. rowCell.setCellValue(device.getItemId().getItemName());
  194. }else if(iLanguage==0){ //判断是否是英文
  195. rowCell.setCellValue(device.getItemId().getForShort());
  196. }
  197. rowCell.setCellStyle(styles.get("cell"));
  198. rowCell = cellRow.createCell(9);
  199. //判断是否是中文
  200. if(iLanguage==1){
  201. rowCell.setCellValue(device.getZequipId().getZequipGroup().getNameLoc());
  202. }else if(iLanguage==0){ //判断是否是英文
  203. rowCell.setCellValue(device.getZequipId().getZequipGroup().getNameEn());
  204. }
  205. rowCell.setCellStyle(styles.get("cell"));
  206. rowCell = cellRow.createCell(10);
  207. rowCell.setCellValue(device.getDeviceName());
  208. rowCell.setCellStyle(styles.get("cell"));
  209. rowCell = cellRow.createCell(11);
  210. rowCell.setCellValue(device.getDeviceNo());
  211. rowCell.setCellStyle(styles.get("cell"));
  212. rowCell = cellRow.createCell(12);
  213. rowCell.setCellValue(device.getDeviceBrand());
  214. rowCell.setCellStyle(styles.get("cell"));
  215. rowCell = cellRow.createCell(13);
  216. rowCell.setCellValue(device.getEquipDriveId().getModel());
  217. rowCell.setCellStyle(styles.get("cell"));
  218. rowCell = cellRow.createCell(14);
  219. rowCell.setCellValue(device.getDeviceCapacity());
  220. rowCell.setCellStyle(styles.get("cell"));
  221. rowCell = cellRow.createCell(15);
  222. rowCell.setCellValue(device.getSystemType());
  223. rowCell.setCellStyle(styles.get("cell"));
  224. rowCell = cellRow.createCell(16);
  225. rowCell.setCellValue(device.getStairsNo());
  226. rowCell.setCellStyle(styles.get("cell"));
  227. rowCell = cellRow.createCell(17);
  228. rowCell.setCellValue(device.getFloor());
  229. rowCell.setCellStyle(styles.get("cell"));
  230. rowCell = cellRow.createCell(18);
  231. rowCell.setCellValue(device.getRoomNo());
  232. rowCell.setCellStyle(styles.get("cell"));
  233. rowCell = cellRow.createCell(19);
  234. rowCell.setCellValue(device.getEnableDate()!=null ? new SimpleDateFormat("yyyy-MM-dd").format(device.getEnableDate()) : "");
  235. rowCell.setCellStyle(styles.get("cell"));
  236. rowCell = cellRow.createCell(20);
  237. rowCell.setCellValue(device.getBackendLoad());
  238. rowCell.setCellStyle(styles.get("cell"));
  239. rowCell = cellRow.createCell(21);
  240. rowCell.setCellValue(device.getOperationTips());
  241. rowCell.setCellStyle(styles.get("cell"));
  242. rowCell = cellRow.createCell(22);
  243. rowCell.setCellValue(device.getPersonId().getUserName());
  244. rowCell.setCellStyle(styles.get("cell"));
  245. rowCell = cellRow.createCell(23);
  246. rowCell.setCellValue(device.getInspecTime()+resources.getMessage(locale, "device.export.excel.minute"));
  247. rowCell.setCellStyle(styles.get("cell"));
  248. rowCell = cellRow.createCell(24);
  249. rowCell.setCellValue(device.getIntervalTime()+resources.getMessage(locale, "device.export.excel.minute"));
  250. rowCell.setCellStyle(styles.get("cell"));
  251. rowCell = cellRow.createCell(25);
  252. rowCell.setCellValue(device.getDevicePath());
  253. rowCell.setCellStyle(styles.get("cell"));
  254. rowCell = cellRow.createCell(26);
  255. rowCell.setCellValue(device.getValidityNr() == 0 ? resources.getMessage(locale, "mmc.soft.person.disabled") : resources.getMessage(locale, "mmc.soft.person.enable"));
  256. rowCell.setCellStyle(styles.get("cell"));
  257. rowCell = cellRow.createCell(27);
  258. rowCell.setCellValue(device.getOtherInfo() != null ? device.getOtherInfo() : "");
  259. rowCell.setCellStyle(styles.get("cell"));
  260. }
  261. wb.write(sos);
  262. sos.flush();
  263. sos.close();
  264. return null;
  265. }
  266. //excel样式
  267. private Map<String, CellStyle> createStyles(Workbook wb)
  268. {
  269. Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
  270. CellStyle style;
  271. Font titleFont = wb.createFont();
  272. titleFont.setFontHeightInPoints((short) 18);
  273. titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
  274. style = wb.createCellStyle();
  275. style.setAlignment(CellStyle.ALIGN_CENTER);
  276. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  277. style.setFont(titleFont);
  278. styles.put("title", style);
  279. style = wb.createCellStyle();
  280. style.setAlignment(CellStyle.ALIGN_CENTER);
  281. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  282. style.setWrapText(true);
  283. styles.put("header", style);
  284. style = wb.createCellStyle();
  285. style.setAlignment(CellStyle.ALIGN_CENTER);
  286. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  287. style.setWrapText(true);
  288. styles.put("cell", style);
  289. style = wb.createCellStyle();
  290. style.setAlignment(CellStyle.ALIGN_CENTER);
  291. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  292. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  293. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  294. style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
  295. styles.put("formula", style);
  296. style = wb.createCellStyle();
  297. style.setAlignment(CellStyle.ALIGN_CENTER);
  298. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  299. style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
  300. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  301. style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
  302. styles.put("formula_2", style);
  303. return styles;
  304. }
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP