| | |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | |
| | | */ |
| | | @Service |
| | | public class SystemDataExportService extends AbstractBaseService { |
| | | @Autowired |
| | | SystemApiToMethods systemApiToMethods; |
| | | @Autowired |
| | | SystemApiToMethods systemApiToMethods; |
| | | |
| | | @Override |
| | | public BaseDao getBaseDao() { |
| | | return super.getBaseDao(); |
| | | } |
| | | @Override |
| | | public BaseDao getBaseDao() { |
| | | return super.getBaseDao(); |
| | | } |
| | | |
| | | /** |
| | | * æ°æ®æ¥è¡¨å¯¼åº |
| | | * |
| | | * @param reqp |
| | | * @return è¿åBase64 EXCEL.XLSX |
| | | * @throws BaseException |
| | | * @throws IOException |
| | | */ |
| | | public String reportDataExport(RequestParameterEntity reqp) throws BaseException { |
| | | public void reportDataExport(FieldSetEntity fse) throws BaseException { |
| | | //æ¥è¡¨uuid |
| | | String uuid = fse.getUUID(); |
| | | //æ¯å¦å½å页 |
| | | boolean exportCurrentPage = fse.getBoolean("exportCurrentPage"); |
| | | //å½åé¡µå· |
| | | int pageIndex = fse.getInteger(CmnConst.CPAGE); |
| | | |
| | | FieldSetEntity formData = reqp.getFormData(); |
| | | //æ»åæ° |
| | | Integer totalColumn = formData.getInteger("totalColumn"); |
| | | //æ»è¡æ° |
| | | Integer totalRows = formData.getInteger("totalRows"); |
| | | //头é¨åºåè¡æ° |
| | | Integer headCount = formData.getInteger("headCount"); |
| | | Map<String, File> files = reqp.getFiles(); |
| | | //åºå®æä»¶å |
| | | File portExcel = files.get("portExcel.xlsx"); |
| | | try (InputStream inputStream = new FileInputStream(portExcel); XSSFWorkbook wb = new XSSFWorkbook(inputStream)) { |
| | | XSSFSheet sheet = wb.getSheetAt(0); |
| | | //è·å头鍿 ·å¼ |
| | | XSSFCellStyle headStyle = getReportStyle(wb, IndexedColors.BLACK.getIndex(), true, (short) 15, new java.awt.Color(191, 191, 191)); |
| | | //è·åæ°æ®åºæ ·å¼ |
| | | XSSFCellStyle dataStyle = getReportStyle(wb, IndexedColors.BLACK.getIndex(), false, (short) 13, new java.awt.Color(255, 255, 255)); |
| | | //ææååæå®½ |
| | | Map<Integer, Integer> m = new HashMap<>(); |
| | | for (int i = 0; i < totalRows; i++) { |
| | | XSSFRow row = sheet.getRow(i); |
| | | XSSFCellStyle style = null; |
| | | if (i < headCount) { |
| | | //头é¨åºåæ ·å¼ |
| | | style = headStyle; |
| | | } else { |
| | | //æ°æ®åºæ ·å¼ |
| | | style = dataStyle; |
| | | } |
| | | //é忝ä¸ä¸ªåå
æ ¼ |
| | | for (int j = 0; j < totalColumn; j++) { |
| | | //è·ååå
æ ¼å¯¹è±¡ |
| | | XSSFCell cell = row.getCell(j); |
| | | if (cell == null) { |
| | | //å¡«å
空çåå
æ ¼ |
| | | cell = row.createCell(j, CellType.STRING); |
| | | } |
| | | //设置åå
æ ¼æ ·å¼ |
| | | cell.setCellStyle(style); |
| | | //è·ååå
æ ¼å¼ |
| | | String val = cell.getStringCellValue(); |
| | | if (val != null) { |
| | | //æ ¹æ®åè计ç®å¤§è´å®½åº¦ |
| | | int i2 = val.getBytes().length * 256; |
| | | //宽度ä¸è½è¶
è¿ 256*256 |
| | | if (i2 > 256 * 256) { |
| | | i2 = 256 * 255; |
| | | } |
| | | if (m.get(j) == null) { |
| | | m.put(j, i2); |
| | | } else { |
| | | //å½åè¡ç忝妿¯å
¶ä»è¡çå宽 |
| | | Integer width = m.get(j); |
| | | if (width < i2) { |
| | | m.put(j, i2); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | //设置æ¯åç宽度 |
| | | for (Map.Entry<Integer, Integer> v : m.entrySet()) { |
| | | sheet.setColumnWidth(v.getKey(), v.getValue()); |
| | | } |
| | | //å°workbook转æ¢ä¸ºåèæµ |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | //å°excelè¾åºå°åèæµ |
| | | wb.write(byteArrayOutputStream); |
| | | //å°åèæµè½¬ä¸ºåè |
| | | byte[] bytes = byteArrayOutputStream.toByteArray(); |
| | | //å°åè转æ¢ä¸ºBase64 |
| | | String encode = Base64.getEncoder().encodeToString(bytes); |
| | | //å é¤ä¼ å
¥çæä»¶ |
| | | if (portExcel != null && portExcel.exists()) { |
| | | portExcel.delete(); |
| | | } |
| | | //è¿åBase64å符串 æ¼æ¥XLSXæä»¶æ ¼å¼åç¼ |
| | | return "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + encode; |
| | | } catch (Exception e) { |
| | | throw new BaseException(CmnCode.EXPORT_REPORT_DATA_FIAL.getValue(),CmnCode.EXPORT_REPORT_DATA_FIAL.getValue()+e.getMessage()); |
| | | } |
| | | } |
| | | /** |
| | | * æ°æ®æ¥è¡¨å¯¼åº |
| | | * |
| | | * @param reqp |
| | | * @return è¿åBase64 EXCEL.XLSX |
| | | * @throws BaseException |
| | | * @throws IOException |
| | | */ |
| | | public String reportDataExport(RequestParameterEntity reqp) throws BaseException { |
| | | |
| | | /** |
| | | * åå
æ ¼æ ·å¼ |
| | | * |
| | | * @param workbook |
| | | * @param color åä½é¢è² |
| | | * @param bold æ¯å¦å ç² |
| | | * @param fontSize åä½å¤§å° |
| | | * @param bgc èæ¯è² |
| | | * @return |
| | | */ |
| | | public XSSFCellStyle getReportStyle(XSSFWorkbook workbook, short color, boolean bold, short fontSize, java.awt.Color bgc) { |
| | | XSSFCellStyle cellStyle = workbook.createCellStyle(); |
| | | //å·¦å³å±
ä¸ |
| | | cellStyle.setAlignment(HorizontalAlignment.CENTER); |
| | | //ä¸ä¸å±
ä¸ |
| | | cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | // ä¸è¾¹æ¡ |
| | | cellStyle.setBorderBottom(BorderStyle.THIN); |
| | | // å·¦è¾¹æ¡ |
| | | cellStyle.setBorderLeft(BorderStyle.THIN); |
| | | // ä¸è¾¹æ¡ |
| | | cellStyle.setBorderTop(BorderStyle.THIN); |
| | | // å³è¾¹æ¡ |
| | | cellStyle.setBorderRight(BorderStyle.THIN); |
| | | //è®¾ç½®èæ¯è² |
| | | FieldSetEntity formData = reqp.getFormData(); |
| | | //æ»åæ° |
| | | Integer totalColumn = formData.getInteger("totalColumn"); |
| | | //æ»è¡æ° |
| | | Integer totalRows = formData.getInteger("totalRows"); |
| | | //头é¨åºåè¡æ° |
| | | Integer headCount = formData.getInteger("headCount"); |
| | | Map<String, File> files = reqp.getFiles(); |
| | | //åºå®æä»¶å |
| | | File portExcel = files.get("portExcel.xlsx"); |
| | | try (InputStream inputStream = new FileInputStream(portExcel); XSSFWorkbook wb = new XSSFWorkbook(inputStream)) { |
| | | XSSFSheet sheet = wb.getSheetAt(0); |
| | | //è·å头鍿 ·å¼ |
| | | XSSFCellStyle headStyle = getReportStyle(wb, IndexedColors.BLACK.getIndex(), true, (short) 15, new java.awt.Color(191, 191, 191)); |
| | | //è·åæ°æ®åºæ ·å¼ |
| | | XSSFCellStyle dataStyle = getReportStyle(wb, IndexedColors.BLACK.getIndex(), false, (short) 13, new java.awt.Color(255, 255, 255)); |
| | | //ææååæå®½ |
| | | Map<Integer, Integer> m = new HashMap<>(); |
| | | for (int i = 0; i < totalRows; i++) { |
| | | XSSFRow row = sheet.getRow(i); |
| | | XSSFCellStyle style = null; |
| | | if (i < headCount) { |
| | | //头é¨åºåæ ·å¼ |
| | | style = headStyle; |
| | | } else { |
| | | //æ°æ®åºæ ·å¼ |
| | | style = dataStyle; |
| | | } |
| | | //é忝ä¸ä¸ªåå
æ ¼ |
| | | for (int j = 0; j < totalColumn; j++) { |
| | | //è·ååå
æ ¼å¯¹è±¡ |
| | | XSSFCell cell = row.getCell(j); |
| | | if (cell == null) { |
| | | //å¡«å
空çåå
æ ¼ |
| | | cell = row.createCell(j, CellType.STRING); |
| | | } |
| | | //设置åå
æ ¼æ ·å¼ |
| | | cell.setCellStyle(style); |
| | | //è·ååå
æ ¼å¼ |
| | | String val = cell.getStringCellValue(); |
| | | if (val != null) { |
| | | //æ ¹æ®åè计ç®å¤§è´å®½åº¦ |
| | | int i2 = val.getBytes().length * 256; |
| | | //宽度ä¸è½è¶
è¿ 256*256 |
| | | if (i2 > 256 * 256) { |
| | | i2 = 256 * 255; |
| | | } |
| | | if (m.get(j) == null) { |
| | | m.put(j, i2); |
| | | } else { |
| | | //å½åè¡ç忝妿¯å
¶ä»è¡çå宽 |
| | | Integer width = m.get(j); |
| | | if (width < i2) { |
| | | m.put(j, i2); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | //设置æ¯åç宽度 |
| | | for (Map.Entry<Integer, Integer> v : m.entrySet()) { |
| | | sheet.setColumnWidth(v.getKey(), v.getValue()); |
| | | } |
| | | //å°workbook转æ¢ä¸ºåèæµ |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | //å°excelè¾åºå°åèæµ |
| | | wb.write(byteArrayOutputStream); |
| | | //å°åèæµè½¬ä¸ºåè |
| | | byte[] bytes = byteArrayOutputStream.toByteArray(); |
| | | //å°åè转æ¢ä¸ºBase64 |
| | | String encode = Base64.getEncoder().encodeToString(bytes); |
| | | //å é¤ä¼ å
¥çæä»¶ |
| | | if (portExcel != null && portExcel.exists()) { |
| | | portExcel.delete(); |
| | | } |
| | | //è¿åBase64å符串 æ¼æ¥XLSXæä»¶æ ¼å¼åç¼ |
| | | return "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + encode; |
| | | } catch (Exception e) { |
| | | throw new BaseException(CmnCode.EXPORT_REPORT_DATA_FIAL.getValue(), CmnCode.EXPORT_REPORT_DATA_FIAL.getValue() + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * åå
æ ¼æ ·å¼ |
| | | * |
| | | * @param workbook |
| | | * @param color åä½é¢è² |
| | | * @param bold æ¯å¦å ç² |
| | | * @param fontSize åä½å¤§å° |
| | | * @param bgc èæ¯è² |
| | | * @return |
| | | */ |
| | | public XSSFCellStyle getReportStyle(XSSFWorkbook workbook, short color, boolean bold, short fontSize, java.awt.Color bgc) { |
| | | XSSFCellStyle cellStyle = workbook.createCellStyle(); |
| | | //å·¦å³å±
ä¸ |
| | | cellStyle.setAlignment(HorizontalAlignment.CENTER); |
| | | //ä¸ä¸å±
ä¸ |
| | | cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | // ä¸è¾¹æ¡ |
| | | cellStyle.setBorderBottom(BorderStyle.THIN); |
| | | // å·¦è¾¹æ¡ |
| | | cellStyle.setBorderLeft(BorderStyle.THIN); |
| | | // ä¸è¾¹æ¡ |
| | | cellStyle.setBorderTop(BorderStyle.THIN); |
| | | // å³è¾¹æ¡ |
| | | cellStyle.setBorderRight(BorderStyle.THIN); |
| | | //è®¾ç½®èæ¯è² |
| | | // cellStyle.setFillForegroundColor(new XSSFColor()); |
| | | //å¡«å
æ¨¡å¼ |
| | | cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| | | XSSFFont font = workbook.createFont(); |
| | | //åä½é¢è² |
| | | font.setColor(color); |
| | | //å ç² |
| | | font.setBold(bold); |
| | | //åä½å¤§å° |
| | | font.setFontHeightInPoints(fontSize); |
| | | //å使 ·å¼ |
| | | font.setFontName("微软é
é»"); |
| | | //èªå¨æ¢è¡ |
| | | cellStyle.setWrapText(true); |
| | | cellStyle.setFont(font); |
| | | return cellStyle; |
| | | } |
| | | //å¡«å
æ¨¡å¼ |
| | | cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| | | XSSFFont font = workbook.createFont(); |
| | | //åä½é¢è² |
| | | font.setColor(color); |
| | | //å ç² |
| | | font.setBold(bold); |
| | | //åä½å¤§å° |
| | | font.setFontHeightInPoints(fontSize); |
| | | //å使 ·å¼ |
| | | font.setFontName("微软é
é»"); |
| | | //èªå¨æ¢è¡ |
| | | cellStyle.setWrapText(true); |
| | | cellStyle.setFont(font); |
| | | return cellStyle; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * éç¨åè¡¨å¯¼åº |
| | | * |
| | | * @param fse |
| | | * @throws BaseException |
| | | */ |
| | | public void generalListDataExport(FieldSetEntity fse) throws BaseException { |
| | | //å¯¼åºæä»¶åç§° |
| | | String file_name = fse.getString(CmnConst.FILE_NAME); |
| | | //æ°æ®æ¥å£ |
| | | String upload_api_url = fse.getString(CmnConst.UPLOAD_API_URL); |
| | | //æ°æ®æ¥å£æéåæ° |
| | | FieldSetEntity export_param = fse.getSubDataTable(CmnConst.EXPORT_PARAM).getFieldSetEntity(0); |
| | | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
| | | RequestParameterEntity requestParameterEntity = (RequestParameterEntity) request.getAttribute(CoreConst.API_POST_REQUEST_DATA); |
| | | FieldSetEntity fs = new FieldSetEntity(); |
| | | fs.setTableName(fse.getString(CmnConst.TABLE_NAME)); |
| | | //å¼å§é¡µ |
| | | int start_cpage = export_param.getInteger(CmnConst.START_CPAGE); |
| | | //ç»æé¡µ |
| | | int end_cpage = export_param.getInteger(CmnConst.END_CPAGE); |
| | | int pagesize = export_param.getInteger(CmnConst.PAGESIZE); |
| | | export_param.setValue(CmnConst.CPAGE, ((start_cpage) * pagesize) / pagesize); |
| | | export_param.setValue(CmnConst.PAGESIZE, pagesize * ((end_cpage - start_cpage) + 1)); |
| | | export_param.remove(CmnConst.START_CPAGE); |
| | | export_param.remove(CmnConst.END_CPAGE); |
| | | Map<Object, Object> values = export_param.getValues(); |
| | | for (Map.Entry<Object, Object> v : values.entrySet()) { |
| | | fs.setValue(v.getKey().toString(), v.getValue()); |
| | | } |
| | | requestParameterEntity.setFormData(fs); |
| | | String result = (String) systemApiToMethods.run(upload_api_url, 1); |
| | | /** |
| | | * éç¨åè¡¨å¯¼åº |
| | | * |
| | | * @param fse |
| | | * @throws BaseException |
| | | */ |
| | | public void generalListDataExport(FieldSetEntity fse) throws BaseException { |
| | | //å¯¼åºæä»¶åç§° |
| | | String file_name = fse.getString(CmnConst.FILE_NAME); |
| | | //æ°æ®æ¥å£ |
| | | String upload_api_url = fse.getString(CmnConst.UPLOAD_API_URL); |
| | | //æ°æ®æ¥å£æéåæ° |
| | | FieldSetEntity export_param = fse.getSubDataTable(CmnConst.EXPORT_PARAM).getFieldSetEntity(0); |
| | | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
| | | RequestParameterEntity requestParameterEntity = (RequestParameterEntity) request.getAttribute(CoreConst.API_POST_REQUEST_DATA); |
| | | FieldSetEntity fs = new FieldSetEntity(); |
| | | fs.setTableName(fse.getString(CmnConst.TABLE_NAME)); |
| | | //å¼å§é¡µ |
| | | int start_cpage = export_param.getInteger(CmnConst.START_CPAGE); |
| | | //ç»æé¡µ |
| | | int end_cpage = export_param.getInteger(CmnConst.END_CPAGE); |
| | | int pagesize = export_param.getInteger(CmnConst.PAGESIZE); |
| | | export_param.setValue(CmnConst.CPAGE, ((start_cpage) * pagesize) / pagesize); |
| | | export_param.setValue(CmnConst.PAGESIZE, pagesize * ((end_cpage - start_cpage) + 1)); |
| | | export_param.remove(CmnConst.START_CPAGE); |
| | | export_param.remove(CmnConst.END_CPAGE); |
| | | Map<Object, Object> values = export_param.getValues(); |
| | | for (Map.Entry<Object, Object> v : values.entrySet()) { |
| | | fs.setValue(v.getKey().toString(), v.getValue()); |
| | | } |
| | | requestParameterEntity.setFormData(fs); |
| | | String result = (String) systemApiToMethods.run(upload_api_url, 1); |
| | | |
| | | if (JSON.isValidObject(result)) { |
| | | JSONObject resultJson = JSON.parseObject(result); |
| | | if (200 == resultJson.getInteger("code")) { |
| | | //è·åæ°æ®æå |
| | | Object data = resultJson.get("data"); |
| | | if (data instanceof JSONObject) { |
| | | } else if (data instanceof JSONArray) { |
| | | DataTableEntity export_field = export_param.getSubDataTable("export_field"); |
| | | try { |
| | | writeExcel((JSONArray) data, export_field, file_name); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } else { |
| | | throw new BaseException(resultJson.getString("code"), resultJson.getString("msg")); |
| | | } |
| | | } else { |
| | | System.out.println("æªç¥çæ°æ®ç±»å"); |
| | | } |
| | | } |
| | | if (JSON.isValidObject(result)) { |
| | | JSONObject resultJson = JSON.parseObject(result); |
| | | if (200 == resultJson.getInteger("code")) { |
| | | //è·åæ°æ®æå |
| | | Object data = resultJson.get("data"); |
| | | if (data instanceof JSONObject) { |
| | | } else if (data instanceof JSONArray) { |
| | | DataTableEntity export_field = export_param.getSubDataTable("export_field"); |
| | | try { |
| | | writeExcel((JSONArray) data, export_field, file_name); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } else { |
| | | throw new BaseException(resultJson.getString("code"), resultJson.getString("msg")); |
| | | } |
| | | } else { |
| | | System.out.println("æªç¥çæ°æ®ç±»å"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è¾åºexcel |
| | | * |
| | | * @param data |
| | | * @param fieldInfo |
| | | * @param main_title |
| | | * @throws IOException |
| | | */ |
| | | public void writeExcel(JSONArray data, DataTableEntity fieldInfo, String main_title) throws IOException { |
| | | //æ é¢ |
| | | List<List<String>> headTitles = Lists.newArrayList(); |
| | | List<String> fields = Lists.newArrayList(); |
| | | //导åºçæ°æ®é |
| | | List<List<Object>> exportData = Lists.newArrayList(); |
| | | List<String> titles = Lists.newArrayList(); |
| | | List<String> titleTemplate = Lists.newArrayList(); |
| | | titleTemplate.add(main_title); |
| | | for (int i = 0; i < fieldInfo.getRows(); i++) { |
| | | titles.add(fieldInfo.getString(i, CmnConst.FIELD_DESC)); |
| | | fields.add(fieldInfo.getString(i, CmnConst.FIELD_NAME)); |
| | | } |
| | | List<List<Object>> lists = contentData(data, fields); |
| | | titles.forEach(title -> { |
| | | List<String> secondTitle = Lists.newArrayList(titleTemplate); |
| | | secondTitle.add(title); |
| | | headTitles.add(secondTitle); |
| | | }); |
| | | HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); |
| | | response.setContentType("multipart/form-data"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // è¿éURLEncoder.encodeå¯ä»¥é²æ¢ä¸æä¹±ç å½ç¶åeasyexcel没æå
³ç³» |
| | | // è¿ééè¦è®¾ç½®ä¸å
³éæµ |
| | | EasyExcel.write(response.getOutputStream()). |
| | | registerWriteHandler(new Custemhandler()).head(headTitles).sheet(main_title).doWrite(lists); |
| | | } |
| | | /** |
| | | * è¾åºexcel |
| | | * |
| | | * @param data |
| | | * @param fieldInfo |
| | | * @param main_title |
| | | * @throws IOException |
| | | */ |
| | | public void writeExcel(JSONArray data, DataTableEntity fieldInfo, String main_title) throws IOException { |
| | | //æ é¢ |
| | | List<List<String>> headTitles = Lists.newArrayList(); |
| | | List<String> fields = Lists.newArrayList(); |
| | | //导åºçæ°æ®é |
| | | List<String> titles = Lists.newArrayList(); |
| | | List<String> titleTemplate = Lists.newArrayList(); |
| | | titleTemplate.add(main_title); |
| | | for (int i = 0; i < fieldInfo.getRows(); i++) { |
| | | titles.add(fieldInfo.getString(i, CmnConst.FIELD_DESC)); |
| | | fields.add(fieldInfo.getString(i, CmnConst.FIELD_NAME)); |
| | | } |
| | | List<List<Object>> lists = contentData(data, fields); |
| | | titles.forEach(title -> { |
| | | List<String> secondTitle = Lists.newArrayList(titleTemplate); |
| | | secondTitle.add(title); |
| | | headTitles.add(secondTitle); |
| | | }); |
| | | HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); |
| | | response.setContentType("multipart/form-data"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // è¿éURLEncoder.encodeå¯ä»¥é²æ¢ä¸æä¹±ç å½ç¶åeasyexcel没æå
³ç³» |
| | | writeExcel(headTitles, lists, main_title, response); |
| | | } |
| | | |
| | | /** |
| | | * ç»è£
æ°æ® |
| | | * |
| | | * @param dataArray |
| | | * @param fields |
| | | * @return |
| | | */ |
| | | private static List<List<Object>> contentData(JSONArray dataArray, List<String> fields) { |
| | | List<List<Object>> contentList = Lists.newArrayList(); |
| | | dataArray.forEach(data -> { |
| | | JSONObject dataJson = (JSONObject) data; |
| | | List<Object> content = Lists.newArrayList(); |
| | | fields.forEach(field -> { |
| | | content.add(dataJson.get(field)); |
| | | }); |
| | | contentList.add(content); |
| | | }); |
| | | return contentList; |
| | | } |
| | | /** |
| | | * è¾åºexcel |
| | | * |
| | | * @param excelHeaders excel表头 |
| | | * @param dataRows æ°æ®è¡ |
| | | * @param sheetName sheetåç§° |
| | | * @throws IOException |
| | | */ |
| | | public void writeExcel(List<List<String>> excelHeaders, List dataRows, String sheetName, HttpServletResponse response) throws IOException { |
| | | |
| | | response.setContentType("multipart/form-data"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // è¿éURLEncoder.encodeå¯ä»¥é²æ¢ä¸æä¹±ç å½ç¶åeasyexcel没æå
³ç³» |
| | | // è¿ééè¦è®¾ç½®ä¸å
³éæµ |
| | | try (ServletOutputStream outputStream = response.getOutputStream();) { |
| | | EasyExcel.write(outputStream). |
| | | registerWriteHandler(new Custemhandler()).head(excelHeaders).sheet(sheetName).doWrite(dataRows); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * ç»è£
æ°æ® |
| | | * |
| | | * @param dataArray |
| | | * @param fields |
| | | * @return |
| | | */ |
| | | private static List<List<Object>> contentData(JSONArray dataArray, List<String> fields) { |
| | | List<List<Object>> contentList = Lists.newArrayList(); |
| | | dataArray.forEach(data -> { |
| | | JSONObject dataJson = (JSONObject) data; |
| | | List<Object> content = Lists.newArrayList(); |
| | | fields.forEach(field -> { |
| | | content.add(dataJson.get(field)); |
| | | }); |
| | | contentList.add(content); |
| | | }); |
| | | return contentList; |
| | | } |
| | | |
| | | |
| | | } |