|
@@ -22,9 +22,9 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
public abstract class ExcelWriter {
|
|
|
|
|
|
/**
|
|
|
- * 创建Excel生成工具
|
|
|
+ * 初始化带表头的Excel生成工具
|
|
|
*
|
|
|
- * @param type - XLSX / XLS
|
|
|
+ * @param type 文件格式,XLSX或XLS
|
|
|
* @return
|
|
|
*/
|
|
|
public static ExcelWriter create(ExcelType type) {
|
|
@@ -40,10 +40,10 @@ public abstract class ExcelWriter {
|
|
|
/**
|
|
|
* 创建sheet并写入String[]内容
|
|
|
*
|
|
|
- * @param sheetName - sheet名称
|
|
|
- * @param titles - 前置的标题,可以为空
|
|
|
- * @param columnNames - 数据列名称
|
|
|
- * @param dataIterator - String[]迭代器
|
|
|
+ * @param sheetName sheet名称
|
|
|
+ * @param titles 前置的标题,可以为空
|
|
|
+ * @param columnNames 数据列名称
|
|
|
+ * @param dataIterator String[]迭代器
|
|
|
*/
|
|
|
public void writeDataArrays(String sheetName, String[] titles, String[] columnNames,
|
|
|
Iterator<String[]> dataIterator) {
|
|
@@ -62,10 +62,10 @@ public abstract class ExcelWriter {
|
|
|
/**
|
|
|
* 创建sheet并写入DataMap对象
|
|
|
*
|
|
|
- * @param sheetName - sheet名称
|
|
|
- * @param titles - 前置的标题,可以为空
|
|
|
- * @param columnNames - 数据列名称
|
|
|
- * @param dataIterator - DataMap对象迭代器
|
|
|
+ * @param sheetName sheet名称
|
|
|
+ * @param titles 前置的标题,可以为空
|
|
|
+ * @param columnNames 数据列名称
|
|
|
+ * @param dataIterator DataMap对象迭代器
|
|
|
*/
|
|
|
public void writeDataMaps(String sheetName, String[] titles, String[] columnNames, Iterator<DataMap> dataIterator) {
|
|
|
Sheet sheet = getWorkbook().createSheet(sheetName);
|
|
@@ -83,10 +83,10 @@ public abstract class ExcelWriter {
|
|
|
/**
|
|
|
* 创建sheet并写入自定义对象
|
|
|
*
|
|
|
- * @param sheetName - sheet名称
|
|
|
- * @param titles - 前置标题,可以为空
|
|
|
- * @param objectType - 自定义对象类型
|
|
|
- * @param objectIterator - 自定义对象迭代器
|
|
|
+ * @param sheetName sheet名称
|
|
|
+ * @param titles 前置标题,可以为空
|
|
|
+ * @param objectType 自定义对象类型
|
|
|
+ * @param objectIterator 自定义对象迭代器
|
|
|
* @param <E>
|
|
|
*/
|
|
|
public <E> void writeObjects(String sheetName, String[] titles, Class<E> objectType, Iterator<E> objectIterator) {
|
|
@@ -104,7 +104,7 @@ public abstract class ExcelWriter {
|
|
|
}
|
|
|
|
|
|
private void writeTitle(Sheet sheet, AtomicInteger rowIndex, String[] titles) {
|
|
|
- if (titles != null) {
|
|
|
+ if (titles != null && titles.length > 0) {
|
|
|
for (String title : titles) {
|
|
|
Row row = sheet.createRow(rowIndex.getAndIncrement());
|
|
|
Cell cell = row.createCell(0);
|
|
@@ -131,12 +131,12 @@ public abstract class ExcelWriter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void writeData(Sheet sheet, AtomicInteger rowIndex, String[] columnNames, DataMap data) {
|
|
|
+ private void writeData(Sheet sheet, AtomicInteger rowIndex, String[] columnNames, DataMap dataMap) {
|
|
|
Row dataRow = sheet.createRow(rowIndex.getAndIncrement());
|
|
|
int columnIndex = 0;
|
|
|
for (String name : columnNames) {
|
|
|
Cell cell = dataRow.createCell(columnIndex++);
|
|
|
- cell.setCellValue(data.getOrDefault(name, StringUtils.EMPTY));
|
|
|
+ cell.setCellValue(dataMap.getOrDefault(name, StringUtils.EMPTY));
|
|
|
}
|
|
|
}
|
|
|
|