@@ -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<Sw Map> 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<Sw Map> 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<Sw Map> 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<Sw Map> 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<Sw Map> 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<Sw Map> 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<Sw Map> 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, Sw Map 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<Sw Map> 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;
Sw Map 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);
}
}
}
}