|
@@ -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) {
|