Skip to content

Commit

Permalink
fix nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
曾文超 Reid Zeng committed Nov 30, 2023
1 parent 8c0d30b commit 45b4d9b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/main/java/com/zwc/sqldataprocessor/exporter/XlsxExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public class XlsxExporter implements Exporter {
public byte[] export(DataList table, boolean exportNulls) {
LinkedHashMap<String, DataList> tables = new LinkedHashMap<>();
tables.put("sheet1", table);
return export(tables);
return export(tables, exportNulls);
}

@Override
public String getExtension() {
return "xlsx";
}

public byte[] export(LinkedHashMap<String, DataList> tables) {
public byte[] export(LinkedHashMap<String, DataList> tables, boolean exportNulls) {
try (SXSSFWorkbook workbook = new SXSSFWorkbook(1000)) {
for (String sheetName : tables.keySet()) {
DataList table = tables.get(sheetName);
Expand All @@ -47,18 +47,21 @@ public byte[] export(LinkedHashMap<String, DataList> tables) {
headFont.setBold(true);
headStyle.setFont(headFont);
int index = 0;
writeRow(sheet, index, table.columns, headStyle);
writeRow(sheet, index, table.columns, headStyle, false);

// 写入行数据
for (List<String> row : table.rows) {
index++;
writeRow(sheet, index, row, null);
writeRow(sheet, index, row, null, exportNulls);
}

// 检测列最大文字数
int[] charCounts = new int[table.columns.size()];
Consumer<List<String>> detectCharCount = row -> {
for (int columnIndex = 0; columnIndex < row.size(); ++columnIndex) {
if (row.get(columnIndex) == null) {
continue;
}
int charCount = row.get(columnIndex).getBytes(Charset.forName("GBK")).length;
charCounts[columnIndex] = Math.max(charCounts[columnIndex], charCount);
}
Expand Down Expand Up @@ -92,14 +95,15 @@ public byte[] export(LinkedHashMap<String, DataList> tables) {
/**
* 写入行数据
*/
void writeRow(SXSSFSheet sheet, int rowIndex, List<String> values, CellStyle style) {
void writeRow(SXSSFSheet sheet, int rowIndex, List<String> values, CellStyle style, boolean exportNulls) {
SXSSFRow row = sheet.createRow(rowIndex);
row.setHeight((short)400);
for (int columnIndex = 0; columnIndex < values.size(); ++columnIndex) {
SXSSFCell cell = row.createCell(columnIndex);

// 写入数据
cell.setCellValue(values.get(columnIndex));
String value = values.get(columnIndex);
cell.setCellValue(value == null && exportNulls ? "<null>" : value);

// 设置样式
if (style != null) {
Expand Down

0 comments on commit 45b4d9b

Please sign in to comment.