Browse Source

导出excel

4.0
lip 2 years ago
parent
commit
d2c33685de
6 changed files with 45 additions and 25 deletions
  1. +2
    -0
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/dynPage/DynPageService.java
  2. +1
    -0
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/model/common/ModelService.java
  3. +8
    -0
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/AbstractCompService.java
  4. +10
    -1
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/AbstractListHandler.java
  5. +23
    -23
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/ExcelExportUtil.java
  6. +1
    -1
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/StringUtil.java

+ 2
- 0
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/dynPage/DynPageService.java View File

@@ -24,6 +24,8 @@ public class DynPageService extends AbstractCompService {
return new DynPageSaveHandler(); return new DynPageSaveHandler();
case TYPE_DEL: case TYPE_DEL:
return new DynPageDelHandler(); return new DynPageDelHandler();
case TYPE_LIST:
return new DynPageListHandler();
} }
return null; return null;
} }


+ 1
- 0
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/model/common/ModelService.java View File

@@ -21,6 +21,7 @@ public class ModelService extends DynPageService {
@Override @Override
protected AbstractHandler createHandler(String type) { protected AbstractHandler createHandler(String type) {
switch (type) { switch (type) {
case TYPE_LIST:
case TYPE_MODEL_LIST: case TYPE_MODEL_LIST:
return new ModelListHandler(); return new ModelListHandler();
case TYPE_MODEL_LOAD: case TYPE_MODEL_LOAD:


+ 8
- 0
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/AbstractCompService.java View File

@@ -68,6 +68,14 @@ public abstract class AbstractCompService {
return pageHandler(params, us, TYPE_LIST, handler -> ((AbstractListHandler) handler).getTotal()); return pageHandler(params, us, TYPE_LIST, handler -> ((AbstractListHandler) handler).getTotal());
} }


/**
* 导出excel
* @return R:{ file: fileName@@filePath }
*/
public R exportExcel(@SwBody SwMap params, UserSession us) {
return pageHandler(params, us, TYPE_LIST, handler -> ((AbstractListHandler) handler).exportExcel());
}

//combo数据 //combo数据
public R combo(@SwBody SwMap params, UserSession us) { public R combo(@SwBody SwMap params, UserSession us) {
return pageHandler(params, us, TYPE_COMBO, handler -> ((DefaultComboHandler) handler).data()); return pageHandler(params, us, TYPE_COMBO, handler -> ((DefaultComboHandler) handler).data());


+ 10
- 1
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/AbstractListHandler.java View File

@@ -6,6 +6,7 @@ import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap; import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine; import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.mvc.service.list.FooterField; import cc.smtweb.framework.core.mvc.service.list.FooterField;
import cc.smtweb.framework.core.util.ExcelExportUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;


@@ -59,7 +60,15 @@ public abstract class AbstractListHandler extends AbstractHandler {
* @return R:{ file: fileName@@filePath } * @return R:{ file: fileName@@filePath }
*/ */
public R exportExcel() { public R exportExcel() {
return R.success(buildListData());
SwMap model = params.readMap("model");
SwListData swListData = this.buildListData();
String fileName = "";
try {
fileName = ExcelExportUtil.exportToExcel(swListData.getRows(), model, (short) 9);
} catch (Exception e) {
throw new RuntimeException(e);
}
return R.success(fileName);
} }


public SwListData buildListData() { public SwListData buildListData() {


+ 23
- 23
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/ExcelExportUtil.java View File

@@ -10,6 +10,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil; import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder; import sun.misc.BASE64Decoder;


import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@@ -22,6 +23,7 @@ import java.util.*;
/** /**
* Excel导出工具类 * Excel导出工具类
*/ */
@Component
public class ExcelExportUtil { public class ExcelExportUtil {
public static Integer MAX_EXCEL_COUNT = 60000; public static Integer MAX_EXCEL_COUNT = 60000;
private static Map<Workbook, Map<String, CellStyle>> cacheMaps = null; private static Map<Workbook, Map<String, CellStyle>> cacheMaps = null;
@@ -135,34 +137,32 @@ public class ExcelExportUtil {
* @param modelJson 前端传回的导出模型 * @param modelJson 前端传回的导出模型
* @throws Exception * @throws Exception
*/ */
public static String exportToExcel(List<Map<String, String>> data, String modelJson) throws Exception {
public static String exportToExcel(List<SwMap> data, String modelJson) throws Exception {
return exportToExcel(data, modelJson, ""); return exportToExcel(data, modelJson, "");
} }


public static String exportToExcel(List<Map<String, String>> data, String modelJson, Map<String, String> mapMergeField) throws Exception {
public static String exportToExcel(List<SwMap> data, String modelJson, Map<String, String> mapMergeField) throws Exception {
SwMap model = new SwMap(); SwMap model = new SwMap();
model.putAll(JsonUtil.parseMap(modelJson)); model.putAll(JsonUtil.parseMap(modelJson));
return exportToExcel(data, model, mapMergeField, null, (short) 9); return exportToExcel(data, model, mapMergeField, null, (short) 9);
} }


public static String exportToExcel(List<Map<String, String>> data, SwMap model, short defFontSize) throws Exception {
public static String exportToExcel(List<SwMap> data, SwMap model, short defFontSize) throws Exception {
return exportToExcel(data, model, null, defFontSize); return exportToExcel(data, model, null, defFontSize);
} }


public static String exportToExcel(List<Map<String, String>> data, String modelJson, String fileName) throws Exception {
public static String exportToExcel(List<SwMap> data, String modelJson, String fileName) throws Exception {
SwMap model = new SwMap(); SwMap model = new SwMap();
model.putAll(JsonUtil.parseMap(modelJson)); model.putAll(JsonUtil.parseMap(modelJson));
return exportToExcel(data, model, fileName, (short) 9); return exportToExcel(data, model, fileName, (short) 9);
} }


public static String exportToExcel(List<Map<String, String>> data, SwMap model, String fileName, short defFontSize) throws Exception {
public static String exportToExcel(List<SwMap> data, SwMap model, String fileName, short defFontSize) throws Exception {
return exportToExcel(data, model, null, fileName, defFontSize); return exportToExcel(data, model, null, fileName, defFontSize);
} }


public static String exportToExcel(List<Map<String, String>> data, SwMap model, Map<String, String> mapMergeField, String fileName, short defFontSize) throws Exception {
// File tempFile = File.createTempFile("xls_", ".xls", new File(System.getProperty("java.io.tmpdir")));
// TODO: 2022/9/27 路径作测试用
File tempFile = File.createTempFile("xls_", ".xls", new File("D:\\jjkj\\"));
public static String exportToExcel(List<SwMap> data, SwMap model, Map<String, String> mapMergeField, String fileName, short defFontSize) throws Exception {
File tempFile = File.createTempFile("xls_", ".xls", new File(System.getProperty("java.io.tmpdir")));
try (FileOutputStream fs = new FileOutputStream(tempFile)) { try (FileOutputStream fs = new FileOutputStream(tempFile)) {
exportToExcel(fs, data, model, mapMergeField, defFontSize); exportToExcel(fs, data, model, mapMergeField, defFontSize);
fs.flush(); fs.flush();
@@ -183,7 +183,7 @@ public class ExcelExportUtil {
return len; return len;
} }


public static void exportToExcel(OutputStream os, List<Map<String, String>> data, SwMap model, Map<String, String> mapMergeField, short defFontSize) throws Exception {
public static void exportToExcel(OutputStream os, List<SwMap> data, SwMap model, Map<String, String> mapMergeField, short defFontSize) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
int detSize = data.size(); int detSize = data.size();
if (detSize == 0) { if (detSize == 0) {
@@ -194,7 +194,7 @@ public class ExcelExportUtil {
sheetCount = sheetCount != 0 ? detSize / MAX_EXCEL_COUNT + 1 : detSize / MAX_EXCEL_COUNT; sheetCount = sheetCount != 0 ? detSize / MAX_EXCEL_COUNT + 1 : detSize / MAX_EXCEL_COUNT;
List<String> listSheetName = new ArrayList<>(); List<String> listSheetName = new ArrayList<>();
for (int i = 0; i < sheetCount; i++) { for (int i = 0; i < sheetCount; i++) {
List<Map<String, String>> listDetailPage = new ArrayList<>();
List<SwMap> listDetailPage = new ArrayList<>();
int endIndex = (i + 1) * MAX_EXCEL_COUNT; int endIndex = (i + 1) * MAX_EXCEL_COUNT;
if (endIndex > detSize) { if (endIndex > detSize) {
endIndex = detSize; endIndex = detSize;
@@ -218,14 +218,14 @@ public class ExcelExportUtil {
clearCellStyleCache(wb); clearCellStyleCache(wb);
} }


private static String getMergeFieldValue(String keyField, Map<String, String> curRow, Map<String, String> mapMergeField) {
private static String getMergeFieldValue(String keyField, SwMap curRow, Map<String, String> mapMergeField) {
if (mapMergeField == null || !mapMergeField.containsKey(keyField)) { if (mapMergeField == null || !mapMergeField.containsKey(keyField)) {
return StringUtil.checkNull(curRow.get(keyField));
return StringUtil.checkNull(String.valueOf(curRow.get(keyField)));
} }
return StringUtil.checkNull(curRow.get(mapMergeField.get(keyField)));
return StringUtil.checkNull(String.valueOf(curRow.get(mapMergeField.get(keyField))));
} }


private static void exportToExcelEx(Sheet sheet, HSSFWorkbook wb, List<Map<String, String>> data, SwMap model, Map<String, String> mapMergeField, short defFontSize) throws Exception {
private static void exportToExcelEx(Sheet sheet, HSSFWorkbook wb, List<SwMap> data, SwMap model, Map<String, String> mapMergeField, short defFontSize) throws Exception {
CellStyle colTitleStyle; CellStyle colTitleStyle;
if (model.containsKey("colTitleBold") && model.readString("colTitleBold").equals("1")) { if (model.containsKey("colTitleBold") && model.readString("colTitleBold").equals("1")) {
colTitleStyle = createCellStyle(wb, defFontSize, true, true, HorizontalAlignment.CENTER); colTitleStyle = createCellStyle(wb, defFontSize, true, true, HorizontalAlignment.CENTER);
@@ -309,7 +309,7 @@ public class ExcelExportUtil {


int curRowIndex; int curRowIndex;
Row row; Row row;
Map<String, String> curRow;
SwMap curRow;
AutoMergeCols amcs = new AutoMergeCols(); AutoMergeCols amcs = new AutoMergeCols();
AutoMergeColInfo mci; AutoMergeColInfo mci;
boolean isIgnoreZero = model.readBool("ignoreZero"); boolean isIgnoreZero = model.readBool("ignoreZero");
@@ -320,8 +320,8 @@ public class ExcelExportUtil {
boolean isEnd = curRowIndex == data.size() + rowIndex - 1; boolean isEnd = curRowIndex == data.size() + rowIndex - 1;
//写序号列 //写序号列
String rowNum = ""; String rowNum = "";
int indention = curRow.containsKey("indention") ? StringUtil.getIntIgnoreErr(curRow.get("indention")) : 0;
if (StringUtil.isEmpty(curRow.get("__sys_footer"))) rowNum = String.valueOf(i + 1);
int indention = curRow.containsKey("indention") ? StringUtil.getIntIgnoreErr(String.valueOf(curRow.get("indention"))) : 0;
if (StringUtil.isEmpty(String.valueOf(curRow.get("__sys_footer")))) rowNum = String.valueOf(i + 1);
if (!model.containsKey("needSeqCol")) { if (!model.containsKey("needSeqCol")) {
writeCell(row, 0, dataStyle_number, rowNum, ColType.TEXT, isIgnoreZero); writeCell(row, 0, dataStyle_number, rowNum, ColType.TEXT, isIgnoreZero);
} }
@@ -331,7 +331,7 @@ public class ExcelExportUtil {
col.putAll(cm); col.putAll(cm);
// //
String keyField = col.readString("field"); String keyField = col.readString("field");
String key = StringUtil.checkNull(curRow.get(keyField));
String key = StringUtil.checkNull(String.valueOf(curRow.get(keyField)));
String value = getMergeFieldValue(keyField, curRow, mapMergeField); String value = getMergeFieldValue(keyField, curRow, mapMergeField);
CellStyle cs; CellStyle cs;
ColType ct = ColType.getColType(col.readString("colType")); ColType ct = ColType.getColType(col.readString("colType"));
@@ -617,8 +617,7 @@ public class ExcelExportUtil {




InputStream iso = null; InputStream iso = null;
// File tempFile = File.createTempFile("xls_", ".xls", new File(System.getProperty("java.io.tmpdir")));
File tempFile = File.createTempFile("xls_", ".xls", new File("D:\\jjkj\\"));
File tempFile = File.createTempFile("xls_", ".xls", new File(System.getProperty("java.io.tmpdir")));
FileOutputStream fs = new FileOutputStream(tempFile); FileOutputStream fs = new FileOutputStream(tempFile);
try { try {
iso = Files.newInputStream(Paths.get(modelFile)); iso = Files.newInputStream(Paths.get(modelFile));
@@ -1073,9 +1072,10 @@ public class ExcelExportUtil {


String model = "{\"header\":[{\"title\":\"姓名\",\"colspan\":1,\"rowspan\":1,\"row\":0,\"col\":0,\"isLeaf\":true},{\"title\":\"人数\",\"colspan\":1,\"rowspan\":1,\"row\":0,\"col\":1,\"isLeaf\":true},{\"title\":\"低保金\",\"colspan\":1,\"rowspan\":1,\"row\":0,\"col\":2,\"isLeaf\":true}],\"columns\":[{\"field\":\"F1\",\"align\":\"center\",\"title\":\"姓名1\",\"chname\":\"姓名1\"},{\"field\":\"F2\",\"align\":\"center\",\"title\":\"人数1\",\"chname\":\"人数1\"},{\"field\":\"F3\",\"align\":\"center\",\"title\":\"低保金1\",\"chname\":\"低保金1\"}],\"headerRowCount\":1,\"title\":\"收款台列表\",\"rownum_\":0}"; String model = "{\"header\":[{\"title\":\"姓名\",\"colspan\":1,\"rowspan\":1,\"row\":0,\"col\":0,\"isLeaf\":true},{\"title\":\"人数\",\"colspan\":1,\"rowspan\":1,\"row\":0,\"col\":1,\"isLeaf\":true},{\"title\":\"低保金\",\"colspan\":1,\"rowspan\":1,\"row\":0,\"col\":2,\"isLeaf\":true}],\"columns\":[{\"field\":\"F1\",\"align\":\"center\",\"title\":\"姓名1\",\"chname\":\"姓名1\"},{\"field\":\"F2\",\"align\":\"center\",\"title\":\"人数1\",\"chname\":\"人数1\"},{\"field\":\"F3\",\"align\":\"center\",\"title\":\"低保金1\",\"chname\":\"低保金1\"}],\"headerRowCount\":1,\"title\":\"收款台列表\",\"rownum_\":0}";


String ss = exportToExcel(rows, model);

// String ss = exportToExcel(rows, model);
// String ss = exportToExcelModel(map, listSonD, mapSonDetail, "D:\\jjkj\\test.xlsx", "a"); // String ss = exportToExcelModel(map, listSonD, mapSonDetail, "D:\\jjkj\\test.xlsx", "a");
System.out.println(ss);
// System.out.println(ss);


} }
} }

+ 1
- 1
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/StringUtil.java View File

@@ -21,7 +21,7 @@ public class StringUtil {
private static Collator chineseCollator = Collator.getInstance(Locale.CHINA); private static Collator chineseCollator = Collator.getInstance(Locale.CHINA);


public static boolean isEmpty(String str) { public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0 || "-".equals(str);
return str == null || str.trim().length() == 0 || "-".equals(str)||"null".equals(str);
} }


public static boolean isNotEmpty(String cs) { public static boolean isNotEmpty(String cs) {


Loading…
Cancel
Save