|
@@ -18,6 +18,7 @@ import org.dromara.common.core.utils.StringUtils;
|
|
|
import org.dromara.common.core.utils.file.FileUtils;
|
|
import org.dromara.common.core.utils.file.FileUtils;
|
|
|
import org.dromara.common.excel.convert.ExcelBigNumberConvert;
|
|
import org.dromara.common.excel.convert.ExcelBigNumberConvert;
|
|
|
import org.dromara.common.excel.core.*;
|
|
import org.dromara.common.excel.core.*;
|
|
|
|
|
+import org.dromara.common.excel.handler.DataWriteHandler;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
@@ -191,6 +192,7 @@ public class ExcelUtil {
|
|
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
// 大数值自动转换 防止失真
|
|
// 大数值自动转换 防止失真
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
|
|
+ .registerWriteHandler(new DataWriteHandler(list.get(0).getClass()))
|
|
|
.sheet(sheetName);
|
|
.sheet(sheetName);
|
|
|
if (merge) {
|
|
if (merge) {
|
|
|
// 合并处理器
|
|
// 合并处理器
|
|
@@ -211,7 +213,7 @@ public class ExcelUtil {
|
|
|
* @param data 模板需要的数据
|
|
* @param data 模板需要的数据
|
|
|
* @param response 响应体
|
|
* @param response 响应体
|
|
|
*/
|
|
*/
|
|
|
- public static void exportTemplate(List<Object> data, String filename, String templatePath, HttpServletResponse response) {
|
|
|
|
|
|
|
+ public static <T> void exportTemplate(List<T> data, String filename, String templatePath, HttpServletResponse response) {
|
|
|
try {
|
|
try {
|
|
|
resetResponse(filename, response);
|
|
resetResponse(filename, response);
|
|
|
ServletOutputStream os = response.getOutputStream();
|
|
ServletOutputStream os = response.getOutputStream();
|
|
@@ -230,20 +232,21 @@ public class ExcelUtil {
|
|
|
* @param data 模板需要的数据
|
|
* @param data 模板需要的数据
|
|
|
* @param os 输出流
|
|
* @param os 输出流
|
|
|
*/
|
|
*/
|
|
|
- public static void exportTemplate(List<Object> data, String templatePath, OutputStream os) {
|
|
|
|
|
|
|
+ public static <T> void exportTemplate(List<T> data, String templatePath, OutputStream os) {
|
|
|
|
|
+ if (CollUtil.isEmpty(data)) {
|
|
|
|
|
+ throw new IllegalArgumentException("数据为空");
|
|
|
|
|
+ }
|
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
|
.withTemplate(templateResource.getStream())
|
|
.withTemplate(templateResource.getStream())
|
|
|
.autoCloseStream(false)
|
|
.autoCloseStream(false)
|
|
|
// 大数值自动转换 防止失真
|
|
// 大数值自动转换 防止失真
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
|
|
+ .registerWriteHandler(new DataWriteHandler(data.get(0).getClass()))
|
|
|
.build();
|
|
.build();
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
|
- if (CollUtil.isEmpty(data)) {
|
|
|
|
|
- throw new IllegalArgumentException("数据为空");
|
|
|
|
|
- }
|
|
|
|
|
// 单表多数据导出 模板格式为 {.属性}
|
|
// 单表多数据导出 模板格式为 {.属性}
|
|
|
- for (Object d : data) {
|
|
|
|
|
|
|
+ for (T d : data) {
|
|
|
excelWriter.fill(d, writeSheet);
|
|
excelWriter.fill(d, writeSheet);
|
|
|
}
|
|
}
|
|
|
excelWriter.finish();
|
|
excelWriter.finish();
|
|
@@ -299,6 +302,9 @@ public class ExcelUtil {
|
|
|
* @param os 输出流
|
|
* @param os 输出流
|
|
|
*/
|
|
*/
|
|
|
public static void exportTemplateMultiList(Map<String, Object> data, String templatePath, OutputStream os) {
|
|
public static void exportTemplateMultiList(Map<String, Object> data, String templatePath, OutputStream os) {
|
|
|
|
|
+ if (CollUtil.isEmpty(data)) {
|
|
|
|
|
+ throw new IllegalArgumentException("数据为空");
|
|
|
|
|
+ }
|
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
|
.withTemplate(templateResource.getStream())
|
|
.withTemplate(templateResource.getStream())
|
|
@@ -307,9 +313,6 @@ public class ExcelUtil {
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
.build();
|
|
.build();
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
|
- if (CollUtil.isEmpty(data)) {
|
|
|
|
|
- throw new IllegalArgumentException("数据为空");
|
|
|
|
|
- }
|
|
|
|
|
for (Map.Entry<String, Object> map : data.entrySet()) {
|
|
for (Map.Entry<String, Object> map : data.entrySet()) {
|
|
|
// 设置列表后续还有数据
|
|
// 设置列表后续还有数据
|
|
|
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
@@ -333,6 +336,9 @@ public class ExcelUtil {
|
|
|
* @param os 输出流
|
|
* @param os 输出流
|
|
|
*/
|
|
*/
|
|
|
public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) {
|
|
public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) {
|
|
|
|
|
+ if (CollUtil.isEmpty(data)) {
|
|
|
|
|
+ throw new IllegalArgumentException("数据为空");
|
|
|
|
|
+ }
|
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
|
.withTemplate(templateResource.getStream())
|
|
.withTemplate(templateResource.getStream())
|
|
@@ -340,9 +346,6 @@ public class ExcelUtil {
|
|
|
// 大数值自动转换 防止失真
|
|
// 大数值自动转换 防止失真
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
.build();
|
|
.build();
|
|
|
- if (CollUtil.isEmpty(data)) {
|
|
|
|
|
- throw new IllegalArgumentException("数据为空");
|
|
|
|
|
- }
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet(i).build();
|
|
WriteSheet writeSheet = EasyExcel.writerSheet(i).build();
|
|
|
for (Map.Entry<String, Object> map : data.get(i).entrySet()) {
|
|
for (Map.Entry<String, Object> map : data.get(i).entrySet()) {
|