Browse Source

修改tools-poi,补充自定义单元格数据封装类,提供三种类型数据写入支持;修改原writeDataArrays方法为writeStringArrays

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 8 months ago
parent
commit
daa874422b

+ 35 - 11
tools-poi/src/main/java/com/qmth/boot/tools/excel/ExcelWriter.java

@@ -3,10 +3,8 @@ package com.qmth.boot.tools.excel;
 import com.qmth.boot.tools.excel.convertor.ValueConvertor;
 import com.qmth.boot.tools.excel.convertor.ValueConvertors;
 import com.qmth.boot.tools.excel.enums.ExcelType;
-import com.qmth.boot.tools.excel.model.CellStyleConfig;
-import com.qmth.boot.tools.excel.model.DataMap;
-import com.qmth.boot.tools.excel.model.FieldParam;
-import com.qmth.boot.tools.excel.model.ObjectParam;
+import com.qmth.boot.tools.excel.model.CellValue;
+import com.qmth.boot.tools.excel.model.*;
 import com.qmth.boot.tools.excel.writer.XlsWriter;
 import com.qmth.boot.tools.excel.writer.XlsxWriter;
 import org.apache.commons.lang3.StringUtils;
@@ -39,14 +37,14 @@ public abstract class ExcelWriter {
     }
 
     /**
-     * 创建sheet并写入String[]内容
+     * 创建sheet并写入字符串数组内容
      *
      * @param sheetName    sheet名称
      * @param titles       前置的标题,可以为空
      * @param columnNames  数据列名称
-     * @param dataIterator String[]迭代器
+     * @param dataIterator String数组迭代器
      */
-    public void writeDataArrays(String sheetName, String[] titles, String[] columnNames,
+    public void writeStringArrays(String sheetName, String[] titles, String[] columnNames,
             Iterator<String[]> dataIterator) {
         Sheet sheet = getWorkbook().createSheet(sheetName);
         AtomicInteger rowIndex = new AtomicInteger(0);
@@ -55,6 +53,28 @@ public abstract class ExcelWriter {
         //生成表头
         writeColumnName(sheet, rowIndex, columnNames);
         //生成数据
+        while (dataIterator.hasNext()) {
+            writeArray(sheet, rowIndex, columnNames, CellValue.of(dataIterator.next()));
+        }
+    }
+
+    /**
+     * 创建sheet并写入CellValue数组内容
+     *
+     * @param sheetName    sheet名称
+     * @param titles       前置的标题,可以为空
+     * @param columnNames  数据列名称
+     * @param dataIterator CellValue数组迭代器
+     */
+    public void writeDataArrays(String sheetName, String[] titles, String[] columnNames,
+            Iterator<CellValue[]> dataIterator) {
+        Sheet sheet = getWorkbook().createSheet(sheetName);
+        AtomicInteger rowIndex = new AtomicInteger(0);
+        //生成标题
+        writeTitle(sheet, rowIndex, titles, columnNames.length);
+        //生成表头
+        writeColumnName(sheet, rowIndex, columnNames);
+        //生成数据
         while (dataIterator.hasNext()) {
             writeArray(sheet, rowIndex, columnNames, dataIterator.next());
         }
@@ -77,7 +97,7 @@ public abstract class ExcelWriter {
         writeColumnName(sheet, rowIndex, columnNames);
         //生成数据
         while (dataIterator.hasNext()) {
-            writeData(sheet, rowIndex, columnNames, dataIterator.next());
+            writeMap(sheet, rowIndex, columnNames, dataIterator.next());
         }
     }
 
@@ -154,7 +174,7 @@ public abstract class ExcelWriter {
         }
     }
 
-    private void writeData(Sheet sheet, AtomicInteger rowIndex, String[] columnNames, DataMap dataMap) {
+    private void writeMap(Sheet sheet, AtomicInteger rowIndex, String[] columnNames, DataMap dataMap) {
         Row dataRow = sheet.createRow(rowIndex.getAndIncrement());
         int columnIndex = 0;
         for (String name : columnNames) {
@@ -168,11 +188,15 @@ public abstract class ExcelWriter {
         }
     }
 
-    private void writeArray(Sheet sheet, AtomicInteger rowIndex, String[] columnNames, String[] data) {
+    private void writeArray(Sheet sheet, AtomicInteger rowIndex, String[] columnNames, CellValue[] dataArray) {
         Row dataRow = sheet.createRow(rowIndex.getAndIncrement());
         for (int columnIndex = 0; columnIndex < columnNames.length; columnIndex++) {
             Cell cell = dataRow.createCell(columnIndex);
-            cell.setCellValue(data.length > columnIndex ? data[columnIndex] : StringUtils.EMPTY);
+            if (dataArray.length > columnIndex) {
+                dataArray[columnIndex].writeToCell(cell);
+            } else {
+                cell.setCellValue(StringUtils.EMPTY);
+            }
             //尝试获取预设单元格样式
             CellStyle style = styleConfig.getStyle(sheet.getSheetName(), cell.getRowIndex(), cell.getColumnIndex());
             if (style != null) {

+ 60 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/model/CellValue.java

@@ -0,0 +1,60 @@
+package com.qmth.boot.tools.excel.model;
+
+import org.apache.poi.ss.usermodel.Cell;
+
+import java.util.Date;
+
+/**
+ * 写入Excel时多类型单元格数据封装类
+ */
+public class CellValue {
+
+    public static final int TYPE_STRING = 1;
+
+    public static final int TYPE_DOUBLE = 2;
+
+    public static final int TYPE_DATE = 3;
+
+    private Object value;
+
+    private int type;
+
+    private CellValue(int type, Object value) {
+        this.type = type;
+        this.value = value;
+    }
+
+    public static CellValue of(String value) {
+        return new CellValue(TYPE_STRING, value);
+    }
+
+    public static CellValue of(double value) {
+        return new CellValue(TYPE_DOUBLE, value);
+    }
+
+    public static CellValue of(Date value) {
+        return new CellValue(TYPE_DATE, value);
+    }
+
+    public static CellValue[] of(String[] array) {
+        CellValue[] valueArray = new CellValue[array.length];
+        for (int i = 0; i < array.length; i++) {
+            valueArray[i] = CellValue.of(array[i]);
+        }
+        return valueArray;
+    }
+
+    public void writeToCell(Cell cell) {
+        switch (type) {
+        case TYPE_DATE:
+            cell.setCellValue((Date) value);
+            break;
+        case TYPE_DOUBLE:
+            cell.setCellValue((Double) value);
+            break;
+        default:
+            cell.setCellValue((String) value);
+            break;
+        }
+    }
+}

+ 4 - 3
tools-poi/src/test/java/com/qmth/boot/test/tools/excel/write/ExcelWriteTest.java

@@ -4,6 +4,7 @@ import com.qmth.boot.test.tools.excel.model.ExcelTestEntity;
 import com.qmth.boot.test.tools.excel.model.GenderEnum;
 import com.qmth.boot.tools.excel.ExcelWriter;
 import com.qmth.boot.tools.excel.enums.ExcelType;
+import com.qmth.boot.tools.excel.model.CellValue;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.FillPatternType;
@@ -45,10 +46,10 @@ public class ExcelWriteTest {
         writer.setCellStyle("sheet1", cellStyle, 1, 0, 1);
         //        writer.writeObjects("sheet1", new String[] { "标题" }, ExcelTestEntity.class,
         //                Collections.singletonList(entity).iterator());
-        List<String[]> dataList = new LinkedList<>();
+        List<CellValue[]> dataList = new LinkedList<>();
         for (int i = 0; i < 10000; i++) {
             dataList.add(
-                    new String[] { RandomStringUtils.random(8, true, true), RandomStringUtils.random(16, true, true) });
+                    new CellValue[] { CellValue.of(i + 1), CellValue.of(RandomStringUtils.random(16, true, true)) });
         }
         writer.writeDataArrays("sheet1", new String[] { "考生表" }, new String[] { "编号", "姓名" }, dataList.iterator());
         writer.output(ous);
@@ -69,7 +70,7 @@ public class ExcelWriteTest {
         entity.setTeacher("!@#");
         ByteArrayOutputStream ous = new ByteArrayOutputStream();
         ExcelWriter writer = ExcelWriter.create(ExcelType.XLS);
-        writer.writeDataArrays("sheet1", new String[] { "考生表" }, new String[] { "编号", "姓名" },
+        writer.writeStringArrays("sheet1", new String[] { "考生表" }, new String[] { "编号", "姓名" },
                 Collections.singletonList(new String[] { "1", "张三" }).iterator());
         writer.output(ous);
         Assert.assertTrue(ous.toByteArray().length > 0);