Commit 0a0b89c69d3ec0b66a3a3ec70eb26689c08f10b0

Authored by lst
1 parent e1ca62ff
Exists in dev

开发:气瓶导入模板下载

backend/lpg-common/pom.xml
@@ -38,6 +38,34 @@ @@ -38,6 +38,34 @@
38 <version>8.2.1</version> 38 <version>8.2.1</version>
39 <scope>compile</scope> 39 <scope>compile</scope>
40 </dependency> 40 </dependency>
  41 + <!--poi-->
  42 + <dependency>
  43 + <groupId>org.apache.poi</groupId>
  44 + <artifactId>poi</artifactId>
  45 + <version>4.1.2</version>
  46 + </dependency>
  47 + <dependency>
  48 + <groupId>org.apache.poi</groupId>
  49 + <artifactId>poi-ooxml</artifactId>
  50 + <version>4.1.2</version>
  51 + </dependency>
  52 +
  53 + <!--导出-->
  54 + <dependency>
  55 + <groupId>cn.afterturn</groupId>
  56 + <artifactId>easypoi-base</artifactId>
  57 + <version>4.1.0</version>
  58 + </dependency>
  59 + <dependency>
  60 + <groupId>cn.afterturn</groupId>
  61 + <artifactId>easypoi-web</artifactId>
  62 + <version>4.1.0</version>
  63 + </dependency>
  64 + <dependency>
  65 + <groupId>cn.afterturn</groupId>
  66 + <artifactId>easypoi-annotation</artifactId>
  67 + <version>4.1.0</version>
  68 + </dependency>
41 </dependencies> 69 </dependencies>
42 <build> 70 <build>
43 <plugins> 71 <plugins>
@@ -47,6 +75,7 @@ @@ -47,6 +75,7 @@
47 <configuration> 75 <configuration>
48 <nonFilteredFileExtensions> 76 <nonFilteredFileExtensions>
49 <nonFilteredFileExtension>xls</nonFilteredFileExtension> 77 <nonFilteredFileExtension>xls</nonFilteredFileExtension>
  78 + <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
50 </nonFilteredFileExtensions> 79 </nonFilteredFileExtensions>
51 </configuration> 80 </configuration>
52 </plugin> 81 </plugin>
backend/lpg-common/src/main/java/com/hotent/lpg/common/util/DataExportUtils.java 0 → 100644
@@ -0,0 +1,260 @@ @@ -0,0 +1,260 @@
  1 +package com.hotent.lpg.common.util;
  2 +
  3 +import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.io.IoUtil;
  5 +import cn.hutool.core.map.MapUtil;
  6 +import cn.hutool.poi.excel.ExcelUtil;
  7 +import cn.hutool.poi.excel.ExcelWriter;
  8 +import com.alibaba.fastjson.JSONObject;
  9 +import io.swagger.annotations.ApiModel;
  10 +import io.swagger.annotations.ApiModelProperty;
  11 +import lombok.experimental.UtilityClass;
  12 +import lombok.extern.slf4j.Slf4j;
  13 +import org.apache.poi.ss.usermodel.CellStyle;
  14 +import org.apache.poi.ss.usermodel.HorizontalAlignment;
  15 +import org.apache.poi.ss.usermodel.VerticalAlignment;
  16 +
  17 +import javax.servlet.http.HttpServletResponse;
  18 +import javax.validation.constraints.NotBlank;
  19 +import javax.validation.constraints.Size;
  20 +import java.io.ByteArrayInputStream;
  21 +import java.io.IOException;
  22 +import java.io.OutputStream;
  23 +import java.lang.reflect.Field;
  24 +import java.net.URLEncoder;
  25 +import java.util.List;
  26 +import java.util.Map;
  27 +import java.util.Optional;
  28 +
  29 +@Slf4j
  30 +@UtilityClass
  31 +public class DataExportUtils {
  32 +
  33 + public void getExcel(Class<?> clazz, HttpServletResponse response, List<?> data) {
  34 +
  35 + Field[] entityFields = clazz.getDeclaredFields();
  36 + ExcelWriter writer = ExcelUtil.getWriter(true);
  37 + List<String> headers = CollUtil.newArrayList();
  38 + StringBuilder comment = new StringBuilder();
  39 + comment.append("提示(请勿删除此提示):");
  40 + for (Field field : entityFields) {
  41 + ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
  42 + Size size = field.getAnnotation(Size.class);
  43 + NotBlank notBlank = field.getAnnotation(NotBlank.class);
  44 + if (null != api) {
  45 + headers.add(api.value());
  46 + }
  47 + String sizeValue = Optional.ofNullable(size).map(Size::message).orElse("");
  48 + String notBlankValue = Optional.ofNullable(notBlank).map(NotBlank::message).orElse("");
  49 + comment.append("*").append(sizeValue).append(",").append(notBlankValue).append("\n");
  50 + }
  51 + comment.append("使用模板导入时请删除示例数据");
  52 + for (int i = 0; i < headers.size(); i++) {
  53 + writer.setColumnWidth(i, 22);
  54 + }
  55 + List<List<String>> rows = CollUtil.newArrayList();
  56 + rows.add(headers);
  57 + writer.merge(headers.size() - 1, comment).setRowHeight(0, headers.size() * 25);
  58 +
  59 + //设置注释单元格格式:水平左对齐,垂直居中,自动换行
  60 + CellStyle style = writer.createCellStyle();
  61 + style.setAlignment(HorizontalAlignment.LEFT);
  62 + style.setVerticalAlignment(VerticalAlignment.CENTER);
  63 + style.setWrapText(true);
  64 + writer.setStyle(style, 0, 0);
  65 +
  66 + writer.write(rows);
  67 + writer.write(data);
  68 + try {
  69 + final OutputStream output = response.getOutputStream();
  70 + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  71 + response.setHeader("Content-disposition",
  72 + "attachment;filename=" +
  73 + URLEncoder.encode(clazz.getSimpleName() + ".xlsx", "UTF-8"));
  74 + writer.flush(output);
  75 + writer.close();
  76 + IoUtil.close(output);
  77 + } catch (IOException e) {
  78 + log.error("转化流失败", e);
  79 + }
  80 + }
  81 +
  82 + public void bottleBatchImportTemplate(Class<?> clazz, HttpServletResponse response, List<?> data) {
  83 +
  84 + Field[] entityFields = clazz.getDeclaredFields();
  85 + ExcelWriter writer = ExcelUtil.getWriter(true);
  86 + List<String> headers = CollUtil.newArrayList();
  87 + StringBuilder comment = new StringBuilder();
  88 + comment.append("气瓶档案信息").append("\n");
  89 + String sizeValue = "0";
  90 + String notBlankValue = "0";
  91 + for (Field field : entityFields) {
  92 + ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
  93 + Size size = field.getAnnotation(Size.class);
  94 + NotBlank notBlank = field.getAnnotation(NotBlank.class);
  95 + if (null != api) {
  96 + headers.add(api.value());
  97 + }
  98 + sizeValue = Optional.ofNullable(size).map(Size::message).orElse("");
  99 + notBlankValue = Optional.ofNullable(notBlank).map(NotBlank::message).orElse("");
  100 + continue;
  101 + }
  102 + for (int i = 0; i < headers.size(); i++) {
  103 + writer.setColumnWidth(i, 22);
  104 + }
  105 + List<List<String>> rows = CollUtil.newArrayList();
  106 + rows.add(headers);
  107 + writer.merge(headers.size() - 1, comment).setRowHeight(0, 80);
  108 +
  109 + //设置注释单元格格式:水平左对齐,垂直居中,自动换行
  110 + CellStyle style = writer.createCellStyle();
  111 + style.setAlignment(HorizontalAlignment.LEFT);
  112 + style.setVerticalAlignment(VerticalAlignment.CENTER);
  113 + style.setWrapText(true);
  114 + writer.setStyle(style, 0, 0);
  115 +
  116 + writer.write(rows);
  117 + writer.write(data);
  118 + try {
  119 + final OutputStream output = response.getOutputStream();
  120 + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  121 + response.setHeader("Content-disposition",
  122 + "attachment;filename=" +
  123 + URLEncoder.encode(clazz.getSimpleName() + ".xlsx", "UTF-8"));
  124 + writer.flush(output);
  125 + writer.close();
  126 + IoUtil.close(output);
  127 + } catch (IOException e) {
  128 + log.error("转化流失败", e);
  129 + }
  130 + }
  131 +
  132 + public void getExcelVersion(Class<?> clazz, HttpServletResponse response, List<?> data) {
  133 +
  134 + Field[] entityFields = clazz.getDeclaredFields();
  135 + ExcelWriter writer = ExcelUtil.getWriter(true);
  136 + List<String> headers = CollUtil.newArrayList();
  137 + StringBuilder comment = new StringBuilder();
  138 + for (Field field : entityFields) {
  139 + ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
  140 + Size size = field.getAnnotation(Size.class);
  141 + NotBlank notBlank = field.getAnnotation(NotBlank.class);
  142 + if (null != api) {
  143 + headers.add(api.value());
  144 + }
  145 + String sizeValue = Optional.ofNullable(size).map(Size::message).orElse("");
  146 + String notBlankValue = Optional.ofNullable(notBlank).map(NotBlank::message).orElse("");
  147 + }
  148 + for (int i = 0; i < headers.size(); i++) {
  149 + writer.setColumnWidth(i, 22);
  150 + }
  151 + List<List<String>> rows = CollUtil.newArrayList();
  152 + rows.add(headers);
  153 +// writer.merge(headers.size() - 1, comment).setRowHeight(0, headers.size() * 25);
  154 +
  155 + //设置注释单元格格式:水平左对齐,垂直居中,自动换行
  156 + CellStyle style = writer.createCellStyle();
  157 + style.setAlignment(HorizontalAlignment.LEFT);
  158 + style.setVerticalAlignment(VerticalAlignment.CENTER);
  159 + style.setWrapText(true);
  160 + writer.setStyle(style, 0, 0);
  161 +
  162 + writer.write(rows);
  163 + writer.write(data);
  164 + try {
  165 + final OutputStream output = response.getOutputStream();
  166 + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  167 + response.setHeader("Content-disposition",
  168 + "attachment;filename=" +
  169 + URLEncoder.encode(clazz.getSimpleName() + ".xlsx", "UTF-8"));
  170 + writer.flush(output);
  171 + writer.close();
  172 + IoUtil.close(output);
  173 + } catch (IOException e) {
  174 + log.error("转化流失败", e);
  175 + }
  176 + }
  177 +
  178 + public void writeToExcel(List<?> list, HttpServletResponse response) {
  179 + if (list == null || list.isEmpty()) {
  180 + log.error("列表为空,生成表格失败");
  181 + return;
  182 + }
  183 +
  184 + Class<?> itemClass = list.get(0).getClass();
  185 + ApiModel apiModel = itemClass.getAnnotation(ApiModel.class);
  186 + String fileName = Optional.ofNullable(apiModel).map(ApiModel::value).orElse("导出数据");
  187 +
  188 + Field[] entityFields = itemClass.getDeclaredFields();
  189 +
  190 + //创建xlsx格式的writer
  191 + ExcelWriter writer = ExcelUtil.getWriter(true);
  192 +
  193 + setHeaderAlias(writer, entityFields);
  194 +
  195 + writer.write(list);
  196 +
  197 + //自适应每一列宽度
  198 + for (int i = 0; i < entityFields.length; i++) {
  199 + // 调整每一列宽度
  200 + writer.autoSizeColumn(i, false);
  201 + }
  202 +
  203 + try {
  204 + // 获取输出流
  205 + final OutputStream output = response.getOutputStream();
  206 + response.setHeader("Content-disposition",
  207 + "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
  208 + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  209 + writer.flush(output);
  210 + writer.close();
  211 + IoUtil.close(output);
  212 + } catch (IOException e) {
  213 + log.error("转化为流失败", e);
  214 + }
  215 +
  216 + }
  217 +
  218 + public void writeToTxt(Object o, HttpServletResponse response) {
  219 + if (o == null) {
  220 + log.error("数据为空,生成文件失败");
  221 + return;
  222 + }
  223 +
  224 + Class<?> itemClass = o.getClass();
  225 + ApiModel apiModel = itemClass.getAnnotation(ApiModel.class);
  226 + String fileName = Optional.ofNullable(apiModel).map(ApiModel::value).orElse("导出数据") + ".txt";
  227 +
  228 + try {
  229 + ByteArrayInputStream inputStringStream = new ByteArrayInputStream(JSONObject.toJSONString(o).getBytes());
  230 + final OutputStream output = response.getOutputStream();
  231 + response.setHeader("Content-disposition",
  232 + "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  233 + response.setContentType("text/plain");
  234 + int len;
  235 + byte[] buffer = new byte[1024 * 10];
  236 + while ((len = inputStringStream.read(buffer)) != -1) {
  237 + output.write(buffer, 0, len);
  238 + }
  239 + output.flush();
  240 + IoUtil.close(inputStringStream);
  241 + IoUtil.close(output);
  242 + } catch (IOException e) {
  243 + log.error("转化为流失败", e);
  244 + }
  245 + }
  246 +
  247 + private static void setHeaderAlias(ExcelWriter writer, Field[]... fieldsList) {
  248 + Map<String, String> headerAlias = MapUtil.newHashMap(true);
  249 + for (Field[] fields : fieldsList) {
  250 + for (Field field : fields) {
  251 + ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
  252 + if (null != api) {
  253 + headerAlias.put(field.getName(), api.value());
  254 + }
  255 + }
  256 + }
  257 + writer.setHeaderAlias(headerAlias);
  258 + }
  259 +
  260 +}
backend/lpg-common/src/main/java/com/hotent/lpg/common/util/ExcelUtils.java
@@ -12,34 +12,34 @@ import java.util.Date; @@ -12,34 +12,34 @@ import java.util.Date;
12 import java.util.List; 12 import java.util.List;
13 13
14 public class ExcelUtils { 14 public class ExcelUtils {
15 - private final static String excel2003L =".xls"; //2003- 版本的excel  
16 - private final static String excel2007U =".xlsx"; //2007+ 版本的excel 15 + private final static String excel2003L = ".xls"; //2003- 版本的excel
  16 + private final static String excel2007U = ".xlsx"; //2007+ 版本的excel
17 17
18 18
19 - public static List<List<Object>> getListByExcel(InputStream in, String fileName) throws Exception{ 19 + public static List<List<Object>> getListByExcel(InputStream in, String fileName) throws Exception {
20 List<List<Object>> list = null; 20 List<List<Object>> list = null;
21 -  
22 //创建Excel工作薄 21 //创建Excel工作薄
23 - Workbook work = getWorkbook(in,fileName);  
24 - if(null == work){ 22 + Workbook work = getWorkbook(in, fileName);
  23 + if (null == work) {
25 throw new Exception("创建Excel工作薄为空!"); 24 throw new Exception("创建Excel工作薄为空!");
26 } 25 }
27 Sheet sheet = null; //页数 26 Sheet sheet = null; //页数
28 Row row = null; //行数 27 Row row = null; //行数
29 Cell cell = null; //列数 28 Cell cell = null; //列数
30 -  
31 list = new ArrayList<List<Object>>(); 29 list = new ArrayList<List<Object>>();
32 //遍历Excel中所有的sheet 30 //遍历Excel中所有的sheet
33 for (int i = 0; i < work.getNumberOfSheets(); i++) { 31 for (int i = 0; i < work.getNumberOfSheets(); i++) {
34 //取某个sheet 32 //取某个sheet
35 sheet = work.getSheetAt(i); 33 sheet = work.getSheetAt(i);
36 - if(sheet== null){continue;}  
37 - 34 + if (sheet == null) {
  35 + continue;
  36 + }
38 //遍历当前sheet中的所有行 37 //遍历当前sheet中的所有行
39 - for (int j = sheet.getFirstRowNum()+1; j <= sheet.getLastRowNum(); j++) { 38 + for (int j = sheet.getFirstRowNum() + 1; j <= sheet.getLastRowNum(); j++) {
40 row = sheet.getRow(j); 39 row = sheet.getRow(j);
41 - if(row == null){continue;}  
42 - 40 + if (row == null) {
  41 + continue;
  42 + }
43 //遍历所有的列 43 //遍历所有的列
44 List<Object> li = new ArrayList<Object>(); 44 List<Object> li = new ArrayList<Object>();
45 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { 45 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
@@ -49,35 +49,33 @@ public class ExcelUtils { @@ -49,35 +49,33 @@ public class ExcelUtils {
49 list.add(li); 49 list.add(li);
50 } 50 }
51 } 51 }
52 -  
53 return list; 52 return list;
54 -  
55 } 53 }
56 54
57 - public static List<List<Object>> getListByExcelVersion2(InputStream in, String fileName) throws Exception{ 55 + public static List<List<Object>> getListByExcelVersion2(InputStream in, String fileName) throws Exception {
58 List<List<Object>> list = null; 56 List<List<Object>> list = null;
59 -  
60 //创建Excel工作薄 57 //创建Excel工作薄
61 - Workbook work = getWorkbook(in,fileName);  
62 - if(null == work){ 58 + Workbook work = getWorkbook(in, fileName);
  59 + if (null == work) {
63 throw new Exception("创建Excel工作薄为空!"); 60 throw new Exception("创建Excel工作薄为空!");
64 } 61 }
65 Sheet sheet = null; //页数 62 Sheet sheet = null; //页数
66 Row row = null; //行数 63 Row row = null; //行数
67 Cell cell = null; //列数 64 Cell cell = null; //列数
68 -  
69 list = new ArrayList<List<Object>>(); 65 list = new ArrayList<List<Object>>();
70 //遍历Excel中所有的sheet 66 //遍历Excel中所有的sheet
71 for (int i = 0; i < work.getNumberOfSheets(); i++) { 67 for (int i = 0; i < work.getNumberOfSheets(); i++) {
72 //取某个sheet 68 //取某个sheet
73 sheet = work.getSheetAt(i); 69 sheet = work.getSheetAt(i);
74 - if(sheet== null){continue;}  
75 - 70 + if (sheet == null) {
  71 + continue;
  72 + }
76 //遍历当前sheet中的所有行 73 //遍历当前sheet中的所有行
77 for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) { 74 for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
78 row = sheet.getRow(j); 75 row = sheet.getRow(j);
79 - if(row == null){continue;}  
80 - 76 + if (row == null) {
  77 + continue;
  78 + }
81 //遍历所有的列 79 //遍历所有的列
82 List<Object> li = new ArrayList<Object>(); 80 List<Object> li = new ArrayList<Object>();
83 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { 81 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
@@ -87,34 +85,33 @@ public class ExcelUtils { @@ -87,34 +85,33 @@ public class ExcelUtils {
87 list.add(li); 85 list.add(li);
88 } 86 }
89 } 87 }
90 -  
91 return list; 88 return list;
92 -  
93 } 89 }
94 - public static List<List<Object>> getListByExcelVersion3(InputStream in, String fileName) throws Exception{  
95 - List<List<Object>> list = null;  
96 90
  91 + public static List<List<Object>> getListByExcelVersion3(InputStream in, String fileName) throws Exception {
  92 + List<List<Object>> list = null;
97 //创建Excel工作薄 93 //创建Excel工作薄
98 - Workbook work = getWorkbook(in,fileName);  
99 - if(null == work){ 94 + Workbook work = getWorkbook(in, fileName);
  95 + if (null == work) {
100 throw new Exception("创建Excel工作薄为空!"); 96 throw new Exception("创建Excel工作薄为空!");
101 } 97 }
102 Sheet sheet = null; //页数 98 Sheet sheet = null; //页数
103 Row row = null; //行数 99 Row row = null; //行数
104 Cell cell = null; //列数 100 Cell cell = null; //列数
105 -  
106 list = new ArrayList<List<Object>>(); 101 list = new ArrayList<List<Object>>();
107 //遍历Excel中所有的sheet 102 //遍历Excel中所有的sheet
108 for (int i = 0; i < work.getNumberOfSheets(); i++) { 103 for (int i = 0; i < work.getNumberOfSheets(); i++) {
109 //取某个sheet 104 //取某个sheet
110 sheet = work.getSheetAt(i); 105 sheet = work.getSheetAt(i);
111 - if(sheet== null){continue;}  
112 - 106 + if (sheet == null) {
  107 + continue;
  108 + }
113 //遍历当前sheet中的所有行 109 //遍历当前sheet中的所有行
114 for (int j = sheet.getFirstRowNum() + 1; j <= sheet.getLastRowNum(); j++) { 110 for (int j = sheet.getFirstRowNum() + 1; j <= sheet.getLastRowNum(); j++) {
115 row = sheet.getRow(j); 111 row = sheet.getRow(j);
116 - if(row == null){continue;}  
117 - 112 + if (row == null) {
  113 + continue;
  114 + }
118 //遍历所有的列 115 //遍历所有的列
119 List<Object> li = new ArrayList<Object>(); 116 List<Object> li = new ArrayList<Object>();
120 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { 117 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
@@ -124,39 +121,37 @@ public class ExcelUtils { @@ -124,39 +121,37 @@ public class ExcelUtils {
124 list.add(li); 121 list.add(li);
125 } 122 }
126 } 123 }
127 -  
128 return list; 124 return list;
129 -  
130 } 125 }
131 126
132 /** 127 /**
133 - * @Description:根据文件后缀,自适应上传文件的版本  
134 * @param inStr,fileName 128 * @param inStr,fileName
135 * @return 129 * @return
136 * @throws Exception 130 * @throws Exception
  131 + * @Description:根据文件后缀,自适应上传文件的版本
137 */ 132 */
138 - public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception{ 133 + public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
139 Workbook wb = null; 134 Workbook wb = null;
140 String fileType = fileName.substring(fileName.lastIndexOf(".")); 135 String fileType = fileName.substring(fileName.lastIndexOf("."));
141 - if(excel2003L.equals(fileType)){ 136 + if (excel2003L.equals(fileType)) {
142 wb = new HSSFWorkbook(inStr); //2003- 137 wb = new HSSFWorkbook(inStr); //2003-
143 - }else if(excel2007U.equals(fileType)){ 138 + } else if (excel2007U.equals(fileType)) {
144 wb = new XSSFWorkbook(inStr); //2007+ 139 wb = new XSSFWorkbook(inStr); //2007+
145 - }else{ 140 + } else {
146 throw new Exception("解析的文件格式有误!"); 141 throw new Exception("解析的文件格式有误!");
147 } 142 }
148 return wb; 143 return wb;
149 } 144 }
150 145
151 /** 146 /**
152 - * @Description:对表格中数值进行格式化  
153 * @param cell 147 * @param cell
154 * @return 148 * @return
  149 + * @Description:对表格中数值进行格式化
155 */ 150 */
156 //解决excel类型问题,获得数值 151 //解决excel类型问题,获得数值
157 public static String getValue(Cell cell) { 152 public static String getValue(Cell cell) {
158 String value = ""; 153 String value = "";
159 - if(null == cell){ 154 + if (null == cell) {
160 return value; 155 return value;
161 } 156 }
162 switch (cell.getCellType()) { 157 switch (cell.getCellType()) {
@@ -166,14 +161,15 @@ public class ExcelUtils { @@ -166,14 +161,15 @@ public class ExcelUtils {
166 //如果是date类型则 ,获取该cell的date值 161 //如果是date类型则 ,获取该cell的date值
167 Date date = DateUtil.getJavaDate(cell.getNumericCellValue()); 162 Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
168 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 163 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
169 - value = format.format(date);;  
170 - }else {// 纯数字  
171 - BigDecimal big= new BigDecimal(cell.getNumericCellValue()); 164 + value = format.format(date);
  165 + ;
  166 + } else {// 纯数字
  167 + BigDecimal big = new BigDecimal(cell.getNumericCellValue());
172 value = big.toString(); 168 value = big.toString();
173 //解决1234.0 去掉后面的.0 169 //解决1234.0 去掉后面的.0
174 - if(null!= value&&!"".equals(value.trim())){ 170 + if (null != value && !"".equals(value.trim())) {
175 String[] item = value.split("[.]"); 171 String[] item = value.split("[.]");
176 - if(1<item.length&&"0".equals(item[1])){ 172 + if (1 < item.length && "0".equals(item[1])) {
177 value = item[0]; 173 value = item[0];
178 } 174 }
179 } 175 }
@@ -193,16 +189,14 @@ public class ExcelUtils { @@ -193,16 +189,14 @@ public class ExcelUtils {
193 break; 189 break;
194 // 布尔类型 190 // 布尔类型
195 case BOOLEAN: 191 case BOOLEAN:
196 - value = " "+ cell.getBooleanCellValue(); 192 + value = " " + cell.getBooleanCellValue();
197 break; 193 break;
198 default: 194 default:
199 value = cell.getStringCellValue(); 195 value = cell.getStringCellValue();
200 } 196 }
201 - if("null".endsWith(value.trim())){  
202 - value= ""; 197 + if ("null".endsWith(value.trim())) {
  198 + value = "";
203 } 199 }
204 return value; 200 return value;
205 } 201 }
206 -  
207 -  
208 } 202 }
backend/lpg-manage/pom.xml
@@ -84,6 +84,7 @@ @@ -84,6 +84,7 @@
84 <configuration> 84 <configuration>
85 <nonFilteredFileExtensions> 85 <nonFilteredFileExtensions>
86 <nonFilteredFileExtension>xls</nonFilteredFileExtension> 86 <nonFilteredFileExtension>xls</nonFilteredFileExtension>
  87 + <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
87 </nonFilteredFileExtensions> 88 </nonFilteredFileExtensions>
88 </configuration> 89 </configuration>
89 </plugin> 90 </plugin>
backend/lpg-manage/src/main/java/com/hotent/lpg/manage/controller/MQpxxController.java
1 package com.hotent.lpg.manage.controller; 1 package com.hotent.lpg.manage.controller;
2 2
  3 +import cn.afterturn.easypoi.excel.ExcelExportUtil;
  4 +import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
3 import cn.hutool.core.util.ObjectUtil; 5 import cn.hutool.core.util.ObjectUtil;
  6 +import com.alibaba.fastjson.JSON;
4 import com.baomidou.mybatisplus.core.metadata.IPage; 7 import com.baomidou.mybatisplus.core.metadata.IPage;
5 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; 8 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
6 import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 9 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -9,6 +12,7 @@ import com.hotent.base.util.BeanUtils; @@ -9,6 +12,7 @@ import com.hotent.base.util.BeanUtils;
9 import com.hotent.base.util.StringUtil; 12 import com.hotent.base.util.StringUtil;
10 import com.hotent.lpg.common.model.WQpssxx; 13 import com.hotent.lpg.common.model.WQpssxx;
11 import com.hotent.lpg.common.model.WQpxx; 14 import com.hotent.lpg.common.model.WQpxx;
  15 +import com.hotent.lpg.manage.dto.BottleBatchImportDTO;
12 import com.hotent.lpg.manage.dto.QpczDTO; 16 import com.hotent.lpg.manage.dto.QpczDTO;
13 import com.hotent.lpg.manage.dto.ReceiveQpRequest; 17 import com.hotent.lpg.manage.dto.ReceiveQpRequest;
14 import com.hotent.lpg.manage.manager.MCzxxManager; 18 import com.hotent.lpg.manage.manager.MCzxxManager;
@@ -20,13 +24,18 @@ import com.hotent.runtime.script.ScriptImpl; @@ -20,13 +24,18 @@ import com.hotent.runtime.script.ScriptImpl;
20 import io.swagger.annotations.ApiOperation; 24 import io.swagger.annotations.ApiOperation;
21 import io.swagger.annotations.ApiParam; 25 import io.swagger.annotations.ApiParam;
22 import lombok.extern.slf4j.Slf4j; 26 import lombok.extern.slf4j.Slf4j;
  27 +import org.apache.poi.ss.usermodel.Workbook;
23 import org.springframework.beans.factory.annotation.Autowired; 28 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.web.bind.annotation.*; 29 import org.springframework.web.bind.annotation.*;
25 import org.springframework.web.multipart.MultipartFile; 30 import org.springframework.web.multipart.MultipartFile;
26 31
  32 +import javax.servlet.http.HttpServletResponse;
  33 +import java.io.OutputStream;
  34 +import java.net.URLEncoder;
27 import java.util.ArrayList; 35 import java.util.ArrayList;
28 import java.util.HashMap; 36 import java.util.HashMap;
29 import java.util.List; 37 import java.util.List;
  38 +import java.util.Map;
30 39
31 /** 40 /**
32 * 员工气瓶 41 * 员工气瓶
@@ -228,4 +237,61 @@ public class MQpxxController { @@ -228,4 +237,61 @@ public class MQpxxController {
228 log.info("耗时={}秒", (end - start) / 1000); 237 log.info("耗时={}秒", (end - start) / 1000);
229 return CommonResult.ok().value("气瓶导入成功"); 238 return CommonResult.ok().value("气瓶导入成功");
230 } 239 }
  240 +
  241 + @ApiOperation(value = "气瓶导入模板下载")
  242 + @PostMapping("/bottleBatchImportTemplate")
  243 + public void bottleBatchImportTemplate(HttpServletResponse response) throws Exception {
  244 + BottleBatchImportDTO bottleBatchImportDTO = BottleBatchImportDTO.builder()
  245 + .ro("1")
  246 + .czdwmc("云梦县城南液化气有限公司")
  247 + .ewm("4020968725")
  248 + .ccbh("1041628")
  249 + .zybh("000000")
  250 + .qpzl("液化石油气钢瓶")
  251 + .czjz("液化石油气")
  252 + .zzny("2016-09-20")
  253 + .xcjyny("2028-08-20")
  254 + .bcjyny("2024-08-20")
  255 + .sjsynx("2028-08-20")
  256 + .qprj("35.5")
  257 + .qpzt("正常")
  258 + .qpgg("YSP35.5")
  259 + .gcgzyl("2.1")
  260 + .sjbh("2.5")
  261 + .lrzh("9712001")
  262 + .qpcpzlzms("1")
  263 + .qpaqzljdjyzs("1")
  264 + .qpcqzm("1")
  265 + .qpjyhgzm("1")
  266 + .tjsj("2024/8/20 7:58:57")
  267 + .qpyszl("16.5")
  268 + .ssyyl("3.2")
  269 + .zzdw("佛山市良琦燃气具有限公司")
  270 + .jyhbh("44702")
  271 + .qpbz("自有产权瓶").build();
  272 + Map map = JSON.parseObject(JSON.toJSONString(bottleBatchImportDTO), Map.class);
  273 + TemplateExportParams params = new TemplateExportParams(
  274 + "doc/qpdadrmb.xlsx");
  275 + params.setColForEach(true);
  276 + Workbook workbook = ExcelExportUtil.exportExcel(params, map);
  277 + String fileName = "气瓶档案导入模板.xlsx";
  278 + response.setContentType("APPLICATION/OCTET-STREAM");
  279 + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
  280 + response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
  281 + response.addHeader("filename", fileName);
  282 + OutputStream os = null;
  283 + try {
  284 + os = response.getOutputStream();
  285 + if (workbook != null) {
  286 + workbook.write(os);
  287 + }
  288 + os.flush();
  289 + os.close();
  290 + } catch (Exception e) {
  291 + e.printStackTrace();
  292 + } finally {
  293 + if (os != null)
  294 + os.close();
  295 + }
  296 + }
231 } 297 }
backend/lpg-manage/src/main/java/com/hotent/lpg/manage/dto/BottleBatchImportDTO.java 0 → 100644
@@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
  1 +package com.hotent.lpg.manage.dto;
  2 +
  3 +import io.swagger.annotations.ApiModel;
  4 +import io.swagger.annotations.ApiModelProperty;
  5 +import lombok.Builder;
  6 +import lombok.Data;
  7 +
  8 +@Data
  9 +@Builder
  10 +@ApiModel(description = "气瓶导入模板")
  11 +public class BottleBatchImportDTO {
  12 +
  13 + @ApiModelProperty(value = "RO")
  14 + private String ro;
  15 + @ApiModelProperty(value = "充装单位名称")
  16 + private String czdwmc;
  17 + @ApiModelProperty(value = "二维码")
  18 + private String ewm;
  19 + @ApiModelProperty(value = "出厂编号")
  20 + private String ccbh;
  21 + @ApiModelProperty(value = "自有编号")
  22 + private String zybh;
  23 + @ApiModelProperty(value = "气瓶种类")
  24 + private String qpzl;
  25 + @ApiModelProperty(value = "充装介质")
  26 + private String czjz;
  27 + @ApiModelProperty(value = "制造年月")
  28 + private String zzny;
  29 + @ApiModelProperty(value = "下次检验年月")
  30 + private String xcjyny;
  31 + @ApiModelProperty(value = "本次检验年月")
  32 + private String bcjyny;
  33 + @ApiModelProperty(value = "设计使用年限")
  34 + private String sjsynx;
  35 + @ApiModelProperty(value = "气瓶容积")
  36 + private String qprj;
  37 + @ApiModelProperty(value = "气瓶状态")
  38 + private String qpzt;
  39 + @ApiModelProperty(value = "气瓶规格")
  40 + private String qpgg;
  41 + @ApiModelProperty(value = "公称工作压力")
  42 + private String gcgzyl;
  43 + @ApiModelProperty(value = "设计壁厚")
  44 + private String sjbh;
  45 + @ApiModelProperty(value = "录入账号")
  46 + private String lrzh;
  47 + @ApiModelProperty(value = "气瓶产品质量证明书")
  48 + private String qpcpzlzms;
  49 + @ApiModelProperty(value = "气瓶安全质量监督检验证书")
  50 + private String qpaqzljdjyzs;
  51 + @ApiModelProperty(value = "气瓶产权证明")
  52 + private String qpcqzm;
  53 + @ApiModelProperty(value = "气瓶检验合格证明")
  54 + private String qpjyhgzm;
  55 + @ApiModelProperty(value = "添加时间")
  56 + private String tjsj;
  57 + @ApiModelProperty(value = "气瓶原始重量")
  58 + private String qpyszl;
  59 + @ApiModelProperty(value = "水试验压力")
  60 + private String ssyyl;
  61 + @ApiModelProperty(value = "制造单位")
  62 + private String zzdw;
  63 + @ApiModelProperty(value = "检验环编号")
  64 + private String jyhbh;
  65 + @ApiModelProperty(value = "气瓶备注")
  66 + private String qpbz;
  67 +}
backend/lpg-manage/src/main/resources/doc/qpdadrmb.xlsx 0 → 100644
No preview for this file type