入参
dart
@RequestBody List<Map<String, Object>> dataList
dart
Sheet sheet = convertDataListToSheet(dataList);
dart
List<List<String>> data = wrapListMapToList(dataList);
dart
在这里插入代码片
dart
private List<List<String>> wrapListMapToList(List<Map<String, Object>> dataList) {
List<List<String>> data = new ArrayList<>();// [["1","4"],[33,666]]
for (Map<String, Object> row : dataList) {
List<String> rowData = new ArrayList<>();
for (Map.Entry<String, Object> entry : row.entrySet()) {
rowData.add(entry.getValue().toString());
}
data.add(rowData);
}
return data;
}
dart
//前8行是空白
public static Sheet convertDataListToSheets(List<Map<String, Object>> dataList) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
// Insert 8 blank rows before starting the data
for (int i = 0; i < 8; i++) {
sheet.createRow(i);
}
// Populate sheet with data starting from the ninth row
int rowIndex = 8; // Start from the ninth row
for (Map<String, Object> data : dataList) {
Row row = sheet.createRow(rowIndex++);
int cellIndex = 0;
for (Map.Entry<String, Object> entry : data.entrySet()) {
Cell cell = row.createCell(cellIndex++);
setCellValue(cell, entry.getValue());
}
}
return sheet;
}
//前8行是空白 第九行第一列去掉不读
dart
public static Sheet convertDataListToSheetRemoveNineAndOne(List<Map<String, Object>> dataList) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
// Insert 8 blank rows before starting the data
for (int i = 0; i < 8; i++) {
sheet.createRow(i);
}
// Populate sheet with data starting from the ninth row
int rowIndex = 8; // Start from the ninth row
for (Map<String, Object> data : dataList) {
Row row = sheet.createRow(rowIndex++);
int cellIndex = 0;
for (Map.Entry<String, Object> entry : data.entrySet()) {
Cell cell = row.createCell(cellIndex++);
setCellValue(cell, entry.getValue());
}
}
// Remove the first cell of the ninth row (index 8)
if (sheet.getRow(8) != null && sheet.getRow(8).getCell(0) != null) {
sheet.getRow(8).removeCell(sheet.getRow(8).getCell(0));
}
return sheet;
}
private static void setCellValue(Cell cell, Object value) {
if (value instanceof String) {
cell.setCellValue((String) value);
} else if (value instanceof Number) {
cell.setCellValue(((Number) value).doubleValue());
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean) value);
} else if (value instanceof java.util.Date) {
cell.setCellValue((java.util.Date) value);
} else {
cell.setCellValue(value != null ? value.toString() : "");
}
}
dart
private List<List<String>> wrapListMapToList(List<Map<String, Object>> dataList) {
List<List<String>> data = new ArrayList<>();// [["1","4"],[33,666]]
for (Map<String, Object> row : dataList) {
List<String> rowData = new ArrayList<>();
for (Map.Entry<String, Object> entry : row.entrySet()) {
rowData.add(entry.getValue().toString());
}
data.add(rowData);
}
return data;
}