1. /**
    2. * 导出错误信息
    3. *
    4. * @Title exportErrMsgQuery
    5. * @Description 【错误信息导出功能】
    6. * @Date 2018年6月5日
    7. * @Author wayne
    8. */
    9. @RequestMapping(value = "/exportErrMsgQuery", method = RequestMethod.GET)
    10. public void exportErrMsgQuery(OrderChangeRequestSearchEntity param, Principal principal, HttpServletRequest request, HttpServletResponse response) {
    11. logger.info("----- 错误信息导出方法开始 -----");
    12. long loggerStart = System.currentTimeMillis();
    13. try (HSSFWorkbook workbook = new HSSFWorkbook()) {
    14. //根据条件使用分页方法查询所有需导出的数据 暂定默认5000条
    15. ExecuteResult<Pagination<JSONObject>> searchResult = getPage(5000, 1, param, principal);
    16. if (!searchResult.isSuccess()) {
    17. logger.error("导出错误信息数据时,查询数据出错:" + searchResult);
    18. return;
    19. }
    20. // 时间格式化
    21. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    22. List<JSONObject> resultList = searchResult.getData().getRecords();
    23. // 声明一个工作薄
    24. // 生成一个表格
    25. HSSFSheet sheet = workbook.createSheet("错误信息信息");
    26. // 设置表格默认列宽度为15个字节
    27. sheet.setDefaultColumnWidth((short) 15);
    28. // 生成一个样式
    29. HSSFCellStyle style = workbook.createCellStyle();
    30. // 生成一个字体
    31. HSSFFont font = workbook.createFont();
    32. font.setFontHeightInPoints((short) 12);
    33. // 把字体应用到当前的样式
    34. style.setFont(font);
    35. // 生成并设置另一个样式
    36. HSSFCellStyle style2 = workbook.createCellStyle();
    37. // 生成另一个字体
    38. HSSFFont font2 = workbook.createFont();
    39. // 把字体应用到当前的样式
    40. style2.setFont(font2);
    41. // 产生表格标题行
    42. HSSFRow row = sheet.createRow(0);
    43. String[] title = {"工贸名称", "网点编码", "服务商名称", "工单编号", "手机号码", "错误街道", "正确街道", "错误服务区域", "正确服务区域",
    44. "错误POI名称", "正确POI名称", "错误POI坐标(经度)", "错误POI坐标(纬度)", "正确POI坐标(经度)", "正确POI坐标(纬度)",
    45. "提报时间", "创建时间", "错误类型", "网点申请描述", "系统返回描述", "审核结果", "系统状态", "错误服务类型", "正确服务类型",
    46. "错误产品大类", "正确产品大类", "错误产品型号", "正确产品型号", "错误详细地址", "是否重新派工", "正确详细地址", "新工单号", "责任人",
    47. "坐席编号", "坐席名称", "是否强结", "审核状态", "审批记录", "提报记录", "审核意见", "审核时间", "审核人", "审核来源"};
    48. for (short j = 0; j < title.length; j++) {
    49. HSSFCell cell = row.createCell(j);
    50. cell.setCellStyle(style);
    51. HSSFRichTextString text = new HSSFRichTextString(title[j]);
    52. cell.setCellValue(text);
    53. }
    54. if (CollectionUtils.isNotEmpty(resultList)) {
    55. for (int i = 0; i < resultList.size(); i++) {
    56. row = sheet.createRow(i + 1);
    57. JSONObject obj = resultList.get(i);
    58. int colNumber = -1;
    59. //工贸名称
    60. if (colNumber++ > -2 && StringUtils.isNotBlank(obj.getString("hsicrmRegionname"))) {
    61. row.createCell(colNumber).setCellValue(obj.getString("hsicrmRegionname"));
    62. }
    63. //网点编码
    64. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmServicestationcode"))) {
    65. row.createCell(colNumber).setCellValue(obj.getString("hsicrmServicestationcode"));
    66. }
    67. //服务商名称
    68. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmServicestationname"))) {
    69. row.createCell(colNumber).setCellValue(obj.getString("hsicrmServicestationname"));
    70. }
    71. //工单编号
    72. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmWorkorderid"))) {
    73. row.createCell(colNumber).setCellValue(obj.getString("hsicrmWorkorderid"));
    74. }
    75. // 手机号码
    76. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmMobilenumber"))) {
    77. row.createCell(colNumber).setCellValue(obj.getString("hsicrmMobilenumber"));
    78. }
    79. // 街道
    80. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPrevioustownname"))) {
    81. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPrevioustownname"));
    82. }
    83. // 正确街道
    84. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmTownname"))) {
    85. row.createCell(colNumber).setCellValue(obj.getString("hsicrmTownname"));
    86. }
    87. // 服务区域
    88. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviouspoiregion"))) {
    89. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviouspoiregion"));
    90. }
    91. // 正确服务区域
    92. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPoiregion"))) {
    93. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPoiregion"));
    94. }
    95. // POI名称
    96. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviouspoiname"))) {
    97. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviouspoiname"));
    98. }
    99. // 正确POI名称
    100. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPoiname"))) {
    101. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPoiname"));
    102. }
    103. // POI坐标(经度)
    104. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviouslongitude"))) {
    105. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviouslongitude"));
    106. }
    107. // POI坐标(纬度)
    108. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviouslatitude"))) {
    109. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviouslatitude"));
    110. }
    111. // 正确POI坐标(经度)
    112. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmLongitude"))) {
    113. row.createCell(colNumber).setCellValue(obj.getString("hsicrmLongitude"));
    114. }
    115. // 正确POI坐标(纬度)
    116. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmLatitude"))) {
    117. row.createCell(colNumber).setCellValue(obj.getString("hsicrmLatitude"));
    118. }
    119. //提报时间
    120. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmSubmittime"))) {
    121. row.createCell(colNumber).setCellValue(dateFormat.format(obj.getDate("hsicrmSubmittime")));
    122. }
    123. //创建时间
    124. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmCreateddate"))) {
    125. row.createCell(colNumber).setCellValue(dateFormat.format(obj.getDate("hsicrmCreateddate")));
    126. }
    127. //错误类型
    128. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmErrortypename"))) {
    129. row.createCell(colNumber).setCellValue(obj.getString("hsicrmErrortypename"));
    130. }
    131. // 网点申请描述
    132. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmDesc"))) {
    133. row.createCell(colNumber).setCellValue(obj.getString("hsicrmDesc"));
    134. }
    135. // 系统返回描述审核结果
    136. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmSystemreturnedmessage"))) {
    137. row.createCell(colNumber).setCellValue(obj.getString("hsicrmSystemreturnedmessage"));
    138. }
    139. // 审核结果
    140. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmOperateresult"))) {
    141. row.createCell(colNumber).setCellValue(StringUtils.equals("100000000", obj.getString("hsicrmOperateresult")) ? "通过" : "不通过");
    142. }
    143. // 系统状态
    144. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmJobstate"))) {
    145. row.createCell(colNumber).setCellValue(StringUtils.equals("100000000", obj.getString("hsicrmJobstate")) ? "未处理" : (StringUtils.equals("100000001", obj.getString("hsicrmJobstate")) ? "成功" : (StringUtils.equals("100000002", obj.getString("hsicrmJobstate")) ? "失败" : "未处理")));
    146. }
    147. // 错误服务类型
    148. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviousrequireservicetypename"))) {
    149. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviousrequireservicetypename"));
    150. }
    151. // 正确服务类型
    152. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmRequireservicetypename"))) {
    153. row.createCell(colNumber).setCellValue(obj.getString("hsicrmRequireservicetypename"));
    154. }
    155. // 错误产品大类
    156. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviousproductcategoryname"))) {
    157. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviousproductcategoryname"));
    158. }
    159. // 正确产品大类
    160. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmProductcategoryname"))) {
    161. row.createCell(colNumber).setCellValue(obj.getString("hsicrmProductcategoryname"));
    162. }
    163. // 错误产品型号
    164. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviousproductmodelname"))) {
    165. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviousproductmodelname"));
    166. }
    167. // 正确产品型号
    168. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmProductmodelname"))) {
    169. row.createCell(colNumber).setCellValue(obj.getString("hsicrmProductmodelname"));
    170. }
    171. // 错误详细地址
    172. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmPreviousconsumeraddr"))) {
    173. row.createCell(colNumber).setCellValue(obj.getString("hsicrmPreviousconsumeraddr"));
    174. }
    175. // 是否重新派工
    176. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmNeeddispatching"))) {
    177. row.createCell(colNumber).setCellValue(obj.getBoolean("hsicrmNeeddispatching") ? "是" : "否");
    178. }
    179. // 正确详细地址
    180. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmConsumeraddr"))) {
    181. row.createCell(colNumber).setCellValue(obj.getString("hsicrmConsumeraddr"));
    182. }
    183. // 新工单号
    184. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmNewworkorderid"))) {
    185. row.createCell(colNumber).setCellValue(obj.getString("hsicrmNewworkorderid"));
    186. }
    187. // 责任人
    188. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmResponsibleperson"))) {
    189. row.createCell(colNumber).setCellValue(obj.getString("hsicrmResponsibleperson"));
    190. }
    191. // 坐席编号
    192. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmSeatsno"))) {
    193. row.createCell(colNumber).setCellValue(obj.getString("hsicrmSeatsno"));
    194. }
    195. // 坐席名称
    196. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmSeatsname"))) {
    197. row.createCell(colNumber).setCellValue(obj.getString("hsicrmSeatsname"));
    198. }
    199. // 是否强结
    200. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmNeedforcecloseworkorder"))) {
    201. row.createCell(colNumber).setCellValue(obj.getBoolean("hsicrmNeedforcecloseworkorder") ? "是" : "否");
    202. }
    203. // 审核状态
    204. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmStatusname"))) {
    205. row.createCell(colNumber).setCellValue(obj.getString("hsicrmStatusname"));
    206. }
    207. // 审批记录
    208. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmApprovallog"))) {
    209. row.createCell(colNumber).setCellValue(obj.getString("hsicrmApprovallog"));
    210. }
    211. // 提报记录
    212. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmSubmitlog"))) {
    213. row.createCell(colNumber).setCellValue(obj.getString("hsicrmSubmitlog"));
    214. }
    215. // 审核意见
    216. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmOperatecomment"))) {
    217. row.createCell(colNumber).setCellValue(obj.getString("hsicrmOperatecomment"));
    218. }
    219. // 审核时间
    220. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmOperatetime"))) {
    221. row.createCell(colNumber).setCellValue(dateFormat.format(obj.getDate("hsicrmOperatetime")));
    222. }
    223. // 审核人
    224. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmOperateby"))) {
    225. row.createCell(colNumber).setCellValue(obj.getString("hsicrmOperateby"));
    226. }
    227. // 审核来源
    228. if (colNumber++ > -2 && StringUtils.isNoneBlank(obj.getString("hsicrmOperatesource"))) {
    229. row.createCell(colNumber).setCellValue(obj.getString("hsicrmOperatesource"));
    230. }
    231. }
    232. }
    233. response.setContentType("application/vnd.ms-excel");
    234. response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("错误信息", "UTF8") + ".xls");
    235. OutputStream ouputStream = response.getOutputStream();
    236. workbook.write(ouputStream);
    237. ouputStream.flush();
    238. ouputStream.close();
    239. } catch (Exception e) {
    240. logger.error("导出错误信息数据失败:" + e.getMessage());
    241. }
    242. long loggerEnd = System.currentTimeMillis();
    243. logger.warn("----- 错误信息导出方法耗时:" + (loggerEnd - loggerStart) + "ms");
    244. logger.info("----- 错误信息导出方法结束 -----");
    245. }

    依赖
    <**dependency**><br /> <**groupId**>org.apache.poi</**groupId**><br /> <**artifactId**>poi-ooxml</**artifactId**><br /> <**version**>3.16</**version**><br /></**dependency**>