|
@@ -37,6 +37,28 @@ public abstract class ExcelWriter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 创建sheet并写入String[]内容
|
|
|
+ *
|
|
|
+ * @param sheetName - sheet名称
|
|
|
+ * @param titles - 前置的标题,可以为空
|
|
|
+ * @param columnNames - 数据列名称
|
|
|
+ * @param dataIterator - String[]迭代器
|
|
|
+ */
|
|
|
+ public void writeDataArrays(String sheetName, String[] titles, String[] columnNames,
|
|
|
+ Iterator<String[]> dataIterator) {
|
|
|
+ Sheet sheet = getWorkbook().createSheet(sheetName);
|
|
|
+ AtomicInteger rowIndex = new AtomicInteger(0);
|
|
|
+ //生成标题
|
|
|
+ writeTitle(sheet, rowIndex, titles);
|
|
|
+ //生成表头
|
|
|
+ writeColumnName(sheet, rowIndex, columnNames);
|
|
|
+ //生成数据
|
|
|
+ while (dataIterator.hasNext()) {
|
|
|
+ writeArray(sheet, rowIndex, columnNames, dataIterator.next());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 创建sheet并写入DataMap对象
|
|
|
*
|
|
@@ -114,7 +136,15 @@ public abstract class ExcelWriter {
|
|
|
int columnIndex = 0;
|
|
|
for (String name : columnNames) {
|
|
|
Cell cell = dataRow.createCell(columnIndex++);
|
|
|
- cell.setCellValue(data.getOrDefault(name, ""));
|
|
|
+ cell.setCellValue(data.getOrDefault(name, StringUtils.EMPTY));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void writeArray(Sheet sheet, AtomicInteger rowIndex, String[] columnNames, String[] data) {
|
|
|
+ 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);
|
|
|
}
|
|
|
}
|
|
|
|