From b92fd92933fce8e97fef05207596217bb746cd4d Mon Sep 17 00:00:00 2001
From: 1821349743@qq.com <1821349743@qq.com>
Date: 星期一, 17 四月 2023 12:06:33 +0800
Subject: [PATCH] update export excel
---
src/main/java/com/product/module/data/utli/CustomMergeStrategy.java | 98 +++++++
src/main/java/com/product/module/data/controller/SystemDataExportController.java | 106 +++----
src/main/java/com/product/module/data/service/SystemDataExportService.java | 517 +++++++++++++++++++++------------------
3 files changed, 413 insertions(+), 308 deletions(-)
diff --git a/src/main/java/com/product/module/data/controller/SystemDataExportController.java b/src/main/java/com/product/module/data/controller/SystemDataExportController.java
index 9f009d2..db6e1cc 100644
--- a/src/main/java/com/product/module/data/controller/SystemDataExportController.java
+++ b/src/main/java/com/product/module/data/controller/SystemDataExportController.java
@@ -32,71 +32,49 @@
public class SystemDataExportController extends AbstractBaseController {
- @Autowired
- SystemDataExportService systemDataExportService;
+ @Autowired
+ SystemDataExportService systemDataExportService;
- @PostMapping("general-list/{version}")
- @ApiVersion(1)
- public String generalListDataExport(HttpServletRequest request){
- try{
- FieldSetEntity fse = null;
- Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
- if (bean != null) {
- RequestParameterEntity reqp = (RequestParameterEntity) bean;
- fse = reqp.getFormData();
- }
- if (bean == null || fse == null) {
- SpringMVCContextHolder.getSystemLogger().error(CmnCode.SYSTEM_FORM_NODATA.getValue(), CmnCode.SYSTEM_FORM_NODATA.getText());
- return this.error(CmnCode.SYSTEM_FORM_NODATA.getValue(), CmnCode.SYSTEM_FORM_NODATA.getText());
- }
- // 鍒ゆ柇鍙傛暟鏄惁姝e父
- if (StringUtils.isEmpty(fse.getString(CmnConst.FILE_NAME))) {
- SpringMVCContextHolder.getSystemLogger().error(CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getValue(), CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getText());
- return this.error(CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getValue(), CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getText());
- }
- if (StringUtils.isEmpty(fse.getString(CmnConst.UPLOAD_API_URL))) {
- SpringMVCContextHolder.getSystemLogger().error(CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getValue(), CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getText());
- return this.error(CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getValue(), CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getText());
- }
- if(BaseUtil.dataTableIsEmpty(fse.getSubDataTable(CmnConst.EXPORT_PARAM))){
- SpringMVCContextHolder.getSystemLogger().error(CmnCode.EXPORT_PARAMS_NOT_EMPTY.getValue(), CmnCode.EXPORT_PARAMS_NOT_EMPTY.getText());
- return this.error(CmnCode.EXPORT_PARAMS_NOT_EMPTY.getValue(), CmnCode.EXPORT_PARAMS_NOT_EMPTY.getText());
- }
- systemDataExportService.generalListDataExport(fse.clones());
- return OK();
- }catch (BaseException e){
- e.printStackTrace();
- SpringMVCContextHolder.getSystemLogger().error(e);
- return error(e);
- }
- catch (Exception e){
- e.printStackTrace();
- SpringMVCContextHolder.getSystemLogger().error(e);
- return error(CmnCode.EXPORT_GENERAL_LIST_DATA_IMPORT_FIAL.getValue(),CmnCode.EXPORT_GENERAL_LIST_DATA_IMPORT_FIAL.getText()+e.getMessage());
- }
- }
- @PostMapping("report/{version}")
- @ApiVersion(1)
- public String reportDataExport(HttpServletRequest request){
- try{
- FieldSetEntity fse = null;
- Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
- if (bean != null) {
- RequestParameterEntity reqp = (RequestParameterEntity) bean;
- return OK_Add(systemDataExportService.reportDataExport(reqp));
- }
- return OK();
- }catch (BaseException e){
- e.printStackTrace();
- SpringMVCContextHolder.getSystemLogger().error(e);
- return error(e);
- }
- catch (Exception e){
- e.printStackTrace();
- SpringMVCContextHolder.getSystemLogger().error(e);
- return error(CmnCode.EXPORT_GENERAL_LIST_DATA_IMPORT_FIAL.getValue(),CmnCode.EXPORT_GENERAL_LIST_DATA_IMPORT_FIAL.getText()+e.getMessage());
- }
- }
+ @PostMapping("general-list/{version}")
+ @ApiVersion(1)
+ public String generalListDataExport(HttpServletRequest request) {
+ try {
+ FieldSetEntity fse = null;
+ Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
+ if (bean != null) {
+ RequestParameterEntity reqp = (RequestParameterEntity) bean;
+ fse = reqp.getFormData();
+ }
+ if (bean == null || fse == null) {
+ SpringMVCContextHolder.getSystemLogger().error(CmnCode.SYSTEM_FORM_NODATA.getValue(), CmnCode.SYSTEM_FORM_NODATA.getText());
+ return this.error(CmnCode.SYSTEM_FORM_NODATA.getValue(), CmnCode.SYSTEM_FORM_NODATA.getText());
+ }
+ // 鍒ゆ柇鍙傛暟鏄惁姝e父
+ if (StringUtils.isEmpty(fse.getString(CmnConst.FILE_NAME))) {
+ SpringMVCContextHolder.getSystemLogger().error(CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getValue(), CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getText());
+ return this.error(CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getValue(), CmnCode.EXPORT_FILE_NAME_NOT_EMPTY.getText());
+ }
+ if (StringUtils.isEmpty(fse.getString(CmnConst.UPLOAD_API_URL))) {
+ SpringMVCContextHolder.getSystemLogger().error(CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getValue(), CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getText());
+ return this.error(CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getValue(), CmnCode.EXPORT_UPLOAD_URL_NOT_EMPTY.getText());
+ }
+ if (BaseUtil.dataTableIsEmpty(fse.getSubDataTable(CmnConst.EXPORT_PARAM))) {
+ SpringMVCContextHolder.getSystemLogger().error(CmnCode.EXPORT_PARAMS_NOT_EMPTY.getValue(), CmnCode.EXPORT_PARAMS_NOT_EMPTY.getText());
+ return this.error(CmnCode.EXPORT_PARAMS_NOT_EMPTY.getValue(), CmnCode.EXPORT_PARAMS_NOT_EMPTY.getText());
+ }
+ systemDataExportService.generalListDataExport(fse.clones());
+ return OK();
+ } catch (BaseException e) {
+ e.printStackTrace();
+ SpringMVCContextHolder.getSystemLogger().error(e);
+ return error(e);
+ } catch (Exception e) {
+ e.printStackTrace();
+ SpringMVCContextHolder.getSystemLogger().error(e);
+ return error(CmnCode.EXPORT_GENERAL_LIST_DATA_IMPORT_FIAL.getValue(), CmnCode.EXPORT_GENERAL_LIST_DATA_IMPORT_FIAL.getText() + e.getMessage());
+ }
+ }
+
}
diff --git a/src/main/java/com/product/module/data/service/SystemDataExportService.java b/src/main/java/com/product/module/data/service/SystemDataExportService.java
index f68f537..f35abaa 100644
--- a/src/main/java/com/product/module/data/service/SystemDataExportService.java
+++ b/src/main/java/com/product/module/data/service/SystemDataExportService.java
@@ -22,6 +22,7 @@
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.*;
@@ -35,261 +36,289 @@
*/
@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());
- }
- //灏唚orkbook杞崲涓哄瓧鑺傛祦
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- //灏唀xcel杈撳嚭鍒板瓧鑺傛祦
- 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());
+ }
+ //灏唚orkbook杞崲涓哄瓧鑺傛祦
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ //灏唀xcel杈撳嚭鍒板瓧鑺傛祦
+ 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鍙互闃叉涓枃涔辩爜 褰撶劧鍜宔asyexcel娌℃湁鍏崇郴
- // 杩欓噷闇�瑕佽缃笉鍏抽棴娴�
- 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鍙互闃叉涓枃涔辩爜 褰撶劧鍜宔asyexcel娌℃湁鍏崇郴
+ 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鍙互闃叉涓枃涔辩爜 褰撶劧鍜宔asyexcel娌℃湁鍏崇郴
+ // 杩欓噷闇�瑕佽缃笉鍏抽棴娴�
+ 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;
+ }
}
diff --git a/src/main/java/com/product/module/data/utli/CustomMergeStrategy.java b/src/main/java/com/product/module/data/utli/CustomMergeStrategy.java
new file mode 100644
index 0000000..084a682
--- /dev/null
+++ b/src/main/java/com/product/module/data/utli/CustomMergeStrategy.java
@@ -0,0 +1,98 @@
+package com.product.module.data.utli;
+
+import cn.hutool.core.collection.CollUtil;
+import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.write.merge.AbstractMergeStrategy;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class CustomMergeStrategy extends AbstractMergeStrategy {
+
+ /**
+ * 鍒嗙粍锛屾瘡鍑犺鍚堝苟涓�娆�
+ */
+ private List<List<Integer>> mergeColDataGroupCountList;
+
+ /**
+ * 鐩爣鍚堝苟鍒梚ndex
+ */
+ private List<Integer> targetColumnIndex;
+ /**
+ * 闇�瑕佸紑濮嬪悎骞跺崟鍏冩牸鐨勯琛宨ndex
+ */
+ private Integer rowIndex;
+
+ /**
+ * mergeColDataList涓哄緟鍚堝苟鐩爣鍒楃殑鍊�
+ */
+ public CustomMergeStrategy(List<List<String>> mergeColDataList, List<Integer> targetColumnIndex) {
+ this.mergeColDataGroupCountList = getGroupCountList(mergeColDataList);
+ this.targetColumnIndex = targetColumnIndex;
+ }
+
+
+ @Override
+ protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
+
+ if (null == rowIndex) {
+ rowIndex = cell.getRowIndex();
+ }
+ // 浠呬粠棣栬浠ュ強鐩爣鍒楃殑鍗曞厓鏍煎紑濮嬪悎骞讹紝蹇界暐鍏朵粬
+ if (cell.getRowIndex() == rowIndex && targetColumnIndex.contains(cell.getColumnIndex())) {
+ //鎵惧埌瀵瑰簲鐨勯渶瑕佸悎骞剁殑鍒�
+ AtomicInteger i = new AtomicInteger(0);
+ Optional<Integer> first = targetColumnIndex.stream().filter(col -> {
+ i.getAndIncrement();
+ return col == cell.getColumnIndex();
+ }).findFirst();
+ mergeGroupColumn(sheet, first.get());
+ }
+ }
+
+ private void mergeGroupColumn(Sheet sheet, Integer index) {
+ int rowCount = rowIndex;
+ for (Integer count : mergeColDataGroupCountList.get(index)) {
+ if (count == 1) {
+ rowCount += count;
+ continue;
+ }
+ // 鍚堝苟鍗曞厓鏍�
+ CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCount, rowCount + count - 1,
+ targetColumnIndex.get(index), targetColumnIndex.get(index));
+ sheet.addMergedRegionUnsafe(cellRangeAddress);
+ rowCount += count;
+ }
+ }
+
+ /**
+ * 璇ユ柟娉曞皢鐩爣鍒楁牴鎹�兼槸鍚︾浉鍚岃繛缁彲鍚堝苟锛屽瓨鍌ㄥ彲鍚堝苟鐨勮鏁�
+ */
+ private List<List<Integer>> getGroupCountList(List<List<String>> exportDataList) {
+ if (CollUtil.isEmpty(exportDataList)) {
+ return new ArrayList<>();
+ }
+ List<List<Integer>> groupCountListList = new ArrayList<>();
+ exportDataList.forEach(dataList->{
+ List<Integer> groupCountList = new ArrayList<>();
+ int count = 1;
+ for (int i = 1; i < dataList.size(); i++) {
+ if (dataList.get(i).equals(dataList.get(i - 1))) {
+ count++;
+ } else {
+ groupCountList.add(count);
+ count = 1;
+ }
+ }
+ // 澶勭悊瀹屾渶鍚庝竴鏉″悗
+ groupCountList.add(count);
+ groupCountListList.add(groupCountList);
+ });
+ return groupCountListList;
+ }
+}
--
Gitblit v1.9.2