diff --git a/backend/lpg-common/pom.xml b/backend/lpg-common/pom.xml
index ef998c5..c73dea6 100644
--- a/backend/lpg-common/pom.xml
+++ b/backend/lpg-common/pom.xml
@@ -38,6 +38,34 @@
8.2.1
compile
+
+
+ org.apache.poi
+ poi
+ 4.1.2
+
+
+ org.apache.poi
+ poi-ooxml
+ 4.1.2
+
+
+
+
+ cn.afterturn
+ easypoi-base
+ 4.1.0
+
+
+ cn.afterturn
+ easypoi-web
+ 4.1.0
+
+
+ cn.afterturn
+ easypoi-annotation
+ 4.1.0
+
@@ -47,6 +75,7 @@
xls
+ xlsx
diff --git a/backend/lpg-common/src/main/java/com/hotent/lpg/common/util/DataExportUtils.java b/backend/lpg-common/src/main/java/com/hotent/lpg/common/util/DataExportUtils.java
new file mode 100644
index 0000000..a999868
--- /dev/null
+++ b/backend/lpg-common/src/main/java/com/hotent/lpg/common/util/DataExportUtils.java
@@ -0,0 +1,260 @@
+package com.hotent.lpg.common.util;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.map.MapUtil;
+import cn.hutool.poi.excel.ExcelUtil;
+import cn.hutool.poi.excel.ExcelWriter;
+import com.alibaba.fastjson.JSONObject;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+import org.apache.poi.ss.usermodel.VerticalAlignment;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.Size;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.net.URLEncoder;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+@Slf4j
+@UtilityClass
+public class DataExportUtils {
+
+ public void getExcel(Class> clazz, HttpServletResponse response, List> data) {
+
+ Field[] entityFields = clazz.getDeclaredFields();
+ ExcelWriter writer = ExcelUtil.getWriter(true);
+ List headers = CollUtil.newArrayList();
+ StringBuilder comment = new StringBuilder();
+ comment.append("提示(请勿删除此提示):");
+ for (Field field : entityFields) {
+ ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
+ Size size = field.getAnnotation(Size.class);
+ NotBlank notBlank = field.getAnnotation(NotBlank.class);
+ if (null != api) {
+ headers.add(api.value());
+ }
+ String sizeValue = Optional.ofNullable(size).map(Size::message).orElse("");
+ String notBlankValue = Optional.ofNullable(notBlank).map(NotBlank::message).orElse("");
+ comment.append("*").append(sizeValue).append(",").append(notBlankValue).append("\n");
+ }
+ comment.append("使用模板导入时请删除示例数据");
+ for (int i = 0; i < headers.size(); i++) {
+ writer.setColumnWidth(i, 22);
+ }
+ List> rows = CollUtil.newArrayList();
+ rows.add(headers);
+ writer.merge(headers.size() - 1, comment).setRowHeight(0, headers.size() * 25);
+
+ //设置注释单元格格式:水平左对齐,垂直居中,自动换行
+ CellStyle style = writer.createCellStyle();
+ style.setAlignment(HorizontalAlignment.LEFT);
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
+ style.setWrapText(true);
+ writer.setStyle(style, 0, 0);
+
+ writer.write(rows);
+ writer.write(data);
+ try {
+ final OutputStream output = response.getOutputStream();
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+ response.setHeader("Content-disposition",
+ "attachment;filename=" +
+ URLEncoder.encode(clazz.getSimpleName() + ".xlsx", "UTF-8"));
+ writer.flush(output);
+ writer.close();
+ IoUtil.close(output);
+ } catch (IOException e) {
+ log.error("转化流失败", e);
+ }
+ }
+
+ public void bottleBatchImportTemplate(Class> clazz, HttpServletResponse response, List> data) {
+
+ Field[] entityFields = clazz.getDeclaredFields();
+ ExcelWriter writer = ExcelUtil.getWriter(true);
+ List headers = CollUtil.newArrayList();
+ StringBuilder comment = new StringBuilder();
+ comment.append("气瓶档案信息").append("\n");
+ String sizeValue = "0";
+ String notBlankValue = "0";
+ for (Field field : entityFields) {
+ ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
+ Size size = field.getAnnotation(Size.class);
+ NotBlank notBlank = field.getAnnotation(NotBlank.class);
+ if (null != api) {
+ headers.add(api.value());
+ }
+ sizeValue = Optional.ofNullable(size).map(Size::message).orElse("");
+ notBlankValue = Optional.ofNullable(notBlank).map(NotBlank::message).orElse("");
+ continue;
+ }
+ for (int i = 0; i < headers.size(); i++) {
+ writer.setColumnWidth(i, 22);
+ }
+ List> rows = CollUtil.newArrayList();
+ rows.add(headers);
+ writer.merge(headers.size() - 1, comment).setRowHeight(0, 80);
+
+ //设置注释单元格格式:水平左对齐,垂直居中,自动换行
+ CellStyle style = writer.createCellStyle();
+ style.setAlignment(HorizontalAlignment.LEFT);
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
+ style.setWrapText(true);
+ writer.setStyle(style, 0, 0);
+
+ writer.write(rows);
+ writer.write(data);
+ try {
+ final OutputStream output = response.getOutputStream();
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+ response.setHeader("Content-disposition",
+ "attachment;filename=" +
+ URLEncoder.encode(clazz.getSimpleName() + ".xlsx", "UTF-8"));
+ writer.flush(output);
+ writer.close();
+ IoUtil.close(output);
+ } catch (IOException e) {
+ log.error("转化流失败", e);
+ }
+ }
+
+ public void getExcelVersion(Class> clazz, HttpServletResponse response, List> data) {
+
+ Field[] entityFields = clazz.getDeclaredFields();
+ ExcelWriter writer = ExcelUtil.getWriter(true);
+ List headers = CollUtil.newArrayList();
+ StringBuilder comment = new StringBuilder();
+ for (Field field : entityFields) {
+ ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
+ Size size = field.getAnnotation(Size.class);
+ NotBlank notBlank = field.getAnnotation(NotBlank.class);
+ if (null != api) {
+ headers.add(api.value());
+ }
+ String sizeValue = Optional.ofNullable(size).map(Size::message).orElse("");
+ String notBlankValue = Optional.ofNullable(notBlank).map(NotBlank::message).orElse("");
+ }
+ for (int i = 0; i < headers.size(); i++) {
+ writer.setColumnWidth(i, 22);
+ }
+ List> rows = CollUtil.newArrayList();
+ rows.add(headers);
+// writer.merge(headers.size() - 1, comment).setRowHeight(0, headers.size() * 25);
+
+ //设置注释单元格格式:水平左对齐,垂直居中,自动换行
+ CellStyle style = writer.createCellStyle();
+ style.setAlignment(HorizontalAlignment.LEFT);
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
+ style.setWrapText(true);
+ writer.setStyle(style, 0, 0);
+
+ writer.write(rows);
+ writer.write(data);
+ try {
+ final OutputStream output = response.getOutputStream();
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+ response.setHeader("Content-disposition",
+ "attachment;filename=" +
+ URLEncoder.encode(clazz.getSimpleName() + ".xlsx", "UTF-8"));
+ writer.flush(output);
+ writer.close();
+ IoUtil.close(output);
+ } catch (IOException e) {
+ log.error("转化流失败", e);
+ }
+ }
+
+ public void writeToExcel(List> list, HttpServletResponse response) {
+ if (list == null || list.isEmpty()) {
+ log.error("列表为空,生成表格失败");
+ return;
+ }
+
+ Class> itemClass = list.get(0).getClass();
+ ApiModel apiModel = itemClass.getAnnotation(ApiModel.class);
+ String fileName = Optional.ofNullable(apiModel).map(ApiModel::value).orElse("导出数据");
+
+ Field[] entityFields = itemClass.getDeclaredFields();
+
+ //创建xlsx格式的writer
+ ExcelWriter writer = ExcelUtil.getWriter(true);
+
+ setHeaderAlias(writer, entityFields);
+
+ writer.write(list);
+
+ //自适应每一列宽度
+ for (int i = 0; i < entityFields.length; i++) {
+ // 调整每一列宽度
+ writer.autoSizeColumn(i, false);
+ }
+
+ try {
+ // 获取输出流
+ final OutputStream output = response.getOutputStream();
+ response.setHeader("Content-disposition",
+ "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+ writer.flush(output);
+ writer.close();
+ IoUtil.close(output);
+ } catch (IOException e) {
+ log.error("转化为流失败", e);
+ }
+
+ }
+
+ public void writeToTxt(Object o, HttpServletResponse response) {
+ if (o == null) {
+ log.error("数据为空,生成文件失败");
+ return;
+ }
+
+ Class> itemClass = o.getClass();
+ ApiModel apiModel = itemClass.getAnnotation(ApiModel.class);
+ String fileName = Optional.ofNullable(apiModel).map(ApiModel::value).orElse("导出数据") + ".txt";
+
+ try {
+ ByteArrayInputStream inputStringStream = new ByteArrayInputStream(JSONObject.toJSONString(o).getBytes());
+ final OutputStream output = response.getOutputStream();
+ response.setHeader("Content-disposition",
+ "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
+ response.setContentType("text/plain");
+ int len;
+ byte[] buffer = new byte[1024 * 10];
+ while ((len = inputStringStream.read(buffer)) != -1) {
+ output.write(buffer, 0, len);
+ }
+ output.flush();
+ IoUtil.close(inputStringStream);
+ IoUtil.close(output);
+ } catch (IOException e) {
+ log.error("转化为流失败", e);
+ }
+ }
+
+ private static void setHeaderAlias(ExcelWriter writer, Field[]... fieldsList) {
+ Map headerAlias = MapUtil.newHashMap(true);
+ for (Field[] fields : fieldsList) {
+ for (Field field : fields) {
+ ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
+ if (null != api) {
+ headerAlias.put(field.getName(), api.value());
+ }
+ }
+ }
+ writer.setHeaderAlias(headerAlias);
+ }
+
+}
diff --git a/backend/lpg-common/src/main/java/com/hotent/lpg/common/util/ExcelUtils.java b/backend/lpg-common/src/main/java/com/hotent/lpg/common/util/ExcelUtils.java
index f07ded9..9c500a9 100644
--- a/backend/lpg-common/src/main/java/com/hotent/lpg/common/util/ExcelUtils.java
+++ b/backend/lpg-common/src/main/java/com/hotent/lpg/common/util/ExcelUtils.java
@@ -12,34 +12,34 @@ import java.util.Date;
import java.util.List;
public class ExcelUtils {
- private final static String excel2003L =".xls"; //2003- 版本的excel
- private final static String excel2007U =".xlsx"; //2007+ 版本的excel
+ private final static String excel2003L = ".xls"; //2003- 版本的excel
+ private final static String excel2007U = ".xlsx"; //2007+ 版本的excel
- public static List> getListByExcel(InputStream in, String fileName) throws Exception{
+ public static List> getListByExcel(InputStream in, String fileName) throws Exception {
List> list = null;
-
//创建Excel工作薄
- Workbook work = getWorkbook(in,fileName);
- if(null == work){
+ Workbook work = getWorkbook(in, fileName);
+ if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null; //页数
Row row = null; //行数
Cell cell = null; //列数
-
list = new ArrayList>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
//取某个sheet
sheet = work.getSheetAt(i);
- if(sheet== null){continue;}
-
+ if (sheet == null) {
+ continue;
+ }
//遍历当前sheet中的所有行
- for (int j = sheet.getFirstRowNum()+1; j <= sheet.getLastRowNum(); j++) {
+ for (int j = sheet.getFirstRowNum() + 1; j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
- if(row == null){continue;}
-
+ if (row == null) {
+ continue;
+ }
//遍历所有的列
List