|
|
@@ -1,15 +1,19 @@
|
|
1
|
1
|
package api.util.helper;
|
|
2
|
2
|
|
|
|
3
|
+
|
|
3
|
4
|
import com.alibaba.fastjson2.JSON;
|
|
4
|
5
|
import io.swagger.annotations.ApiModelProperty;
|
|
5
|
6
|
import lombok.extern.slf4j.Slf4j;
|
|
|
7
|
+import org.apache.poi.hpsf.UnicodeString;
|
|
6
|
8
|
import org.apache.poi.hssf.usermodel.*;
|
|
7
|
9
|
import org.apache.poi.ss.usermodel.*;
|
|
8
|
10
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
11
|
+import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
|
9
|
12
|
import org.springframework.stereotype.Component;
|
|
10
|
13
|
|
|
11
|
14
|
import javax.servlet.http.HttpServletResponse;
|
|
12
|
15
|
import java.io.*;
|
|
|
16
|
+import java.lang.reflect.AnnotatedArrayType;
|
|
13
|
17
|
import java.lang.reflect.Field;
|
|
14
|
18
|
import java.math.BigDecimal;
|
|
15
|
19
|
import java.text.DecimalFormat;
|
|
|
@@ -260,7 +264,7 @@ public class ExcelHelper<T> {
|
|
260
|
264
|
public void exportExcel(String suffix, List<T> list, String title) {
|
|
261
|
265
|
HttpServletResponse response=ServletHelper.getResponse();
|
|
262
|
266
|
try {
|
|
263
|
|
- Map<String, Field> filedMap = new HashMap<>();
|
|
|
267
|
+ LinkedHashMap<String, Field> filedMap = new LinkedHashMap<>();
|
|
264
|
268
|
for (Field field : clazz.getDeclaredFields()) {
|
|
265
|
269
|
if (field.getAnnotation(ApiModelProperty.class) != null) {
|
|
266
|
270
|
filedMap.put(field.getAnnotation(ApiModelProperty.class).value(), field);
|
|
|
@@ -300,6 +304,8 @@ public class ExcelHelper<T> {
|
|
300
|
304
|
cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
301
|
305
|
cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
302
|
306
|
cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
307
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
308
|
+
|
|
303
|
309
|
|
|
304
|
310
|
int limit=60000;
|
|
305
|
311
|
int rowindex=0;
|
|
|
@@ -317,6 +323,26 @@ public class ExcelHelper<T> {
|
|
317
|
323
|
}
|
|
318
|
324
|
}
|
|
319
|
325
|
|
|
|
326
|
+ for (int columnNum = 0; columnNum <= filedMap.size(); columnNum++)
|
|
|
327
|
+ {
|
|
|
328
|
+ int columnWidth = sheet.getColumnWidth(columnNum) / 256;//获取当前列宽
|
|
|
329
|
+ for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++)
|
|
|
330
|
+ {
|
|
|
331
|
+ Row currentRow = sheet.getRow(rowNum);
|
|
|
332
|
+ if (currentRow.getCell(columnNum) != null)
|
|
|
333
|
+ {
|
|
|
334
|
+ Cell currentCell = currentRow.getCell(columnNum);
|
|
|
335
|
+ int length = currentCell.toString().getBytes("UTF-8").length;//获取当前单元格的内容宽度
|
|
|
336
|
+ if (columnWidth < length)//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度
|
|
|
337
|
+ {
|
|
|
338
|
+ columnWidth = length;
|
|
|
339
|
+ }
|
|
|
340
|
+ }
|
|
|
341
|
+ }
|
|
|
342
|
+ sheet.setColumnWidth(columnNum, columnWidth * 256);
|
|
|
343
|
+ }
|
|
|
344
|
+
|
|
|
345
|
+
|
|
320
|
346
|
if (StringHelper.isEmpty(title)) {
|
|
321
|
347
|
title = System.currentTimeMillis() + "";
|
|
322
|
348
|
}
|
|
|
@@ -347,7 +373,7 @@ public class ExcelHelper<T> {
|
|
347
|
373
|
* @param wb
|
|
348
|
374
|
* @param filedMap
|
|
349
|
375
|
*/
|
|
350
|
|
- private Sheet createSheet(Workbook wb,Map<String, Field> filedMap,CellStyle style){
|
|
|
376
|
+ private Sheet createSheet(Workbook wb,LinkedHashMap<String, Field> filedMap,CellStyle style){
|
|
351
|
377
|
Sheet sheet = wb.createSheet();
|
|
352
|
378
|
Row headerRow = sheet.createRow(0);
|
|
353
|
379
|
headerRow.setHeightInPoints(16);
|
|
|
@@ -388,7 +414,7 @@ public class ExcelHelper<T> {
|
|
388
|
414
|
* @param t
|
|
389
|
415
|
* @param style
|
|
390
|
416
|
*/
|
|
391
|
|
- private void createRow(Sheet sheet,int rowindex,Map<String, Field> filedMap,T t,CellStyle style)
|
|
|
417
|
+ private void createRow(Sheet sheet,int rowindex,LinkedHashMap<String, Field> filedMap,T t,CellStyle style)
|
|
392
|
418
|
{
|
|
393
|
419
|
Row row = sheet.createRow(rowindex);
|
|
394
|
420
|
row.setHeightInPoints(16);
|