|
|
|
@ -17,6 +17,7 @@ import org.apache.poi.ss.util.RegionUtil;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.sql.ResultSetMetaData;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@ -843,4 +844,91 @@ public class ExcelCommonUtil {
|
|
|
|
|
}
|
|
|
|
|
sheet.removeMergedRegion(index);//移除合并单元格
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将List<Record>数据写入到Excel中
|
|
|
|
|
*
|
|
|
|
|
* @param records
|
|
|
|
|
* @param filePath
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static void writeExcel(List<Record> records, String filePath) throws IOException {
|
|
|
|
|
if (records == null || records.isEmpty()) {
|
|
|
|
|
throw new IllegalArgumentException("The list of records must not be empty.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取第一个Record的元数据以确定列名
|
|
|
|
|
Record firstRecord = records.getFirst();
|
|
|
|
|
String[] headers = firstRecord.getColumnNames();
|
|
|
|
|
int columnCount = headers.length;
|
|
|
|
|
|
|
|
|
|
// 创建Excel工作簿
|
|
|
|
|
Workbook workbook = new XSSFWorkbook();
|
|
|
|
|
// 创建一个Excel工作表
|
|
|
|
|
Sheet sheet = workbook.createSheet("Sheet1");
|
|
|
|
|
|
|
|
|
|
// 创建标题行
|
|
|
|
|
Row titleRow = sheet.createRow(0);
|
|
|
|
|
// 设置标题行的样式
|
|
|
|
|
CellStyle titleStyle = workbook.createCellStyle();
|
|
|
|
|
Font font = workbook.createFont();
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
font.setFontHeightInPoints((short) 14);
|
|
|
|
|
titleStyle.setFont(font);
|
|
|
|
|
titleStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
titleStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
titleStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
titleStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
|
|
// 填充标题行
|
|
|
|
|
for (int i = 1; i <= columnCount; i++) {
|
|
|
|
|
Cell cell = titleRow.createCell(i - 1);
|
|
|
|
|
cell.setCellValue(headers[i - 1]);
|
|
|
|
|
cell.setCellStyle(titleStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建数据行的样式
|
|
|
|
|
CellStyle dataStyle = workbook.createCellStyle();
|
|
|
|
|
font = workbook.createFont();
|
|
|
|
|
font.setFontHeightInPoints((short) 14);
|
|
|
|
|
dataStyle.setFont(font);
|
|
|
|
|
dataStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
dataStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
dataStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
dataStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
|
|
// 填充数据行
|
|
|
|
|
int rowNum = 1;
|
|
|
|
|
for (Record record : records) {
|
|
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
|
|
int cellNum = 0;
|
|
|
|
|
for (int i = 1; i <= columnCount; i++) {
|
|
|
|
|
Cell cell = row.createCell(cellNum++);
|
|
|
|
|
Object value = record.get(headers[i - 1]);
|
|
|
|
|
cell.setCellValue(value == null ? "" : value.toString());
|
|
|
|
|
cell.setCellStyle(dataStyle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置最小列宽
|
|
|
|
|
int minColumnWidth = 100 * 256 / 7; // 将像素转换为字符单位(Excel的单位是1/256个字符宽度)
|
|
|
|
|
for (int i = 0; i < headers.length; i++) {
|
|
|
|
|
sheet.setColumnWidth(i, Math.max(minColumnWidth, sheet.getColumnWidth(i)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自动调整列宽
|
|
|
|
|
for (int i = 0; i < headers.length; i++) {
|
|
|
|
|
sheet.autoSizeColumn(i);
|
|
|
|
|
int columnWidth = sheet.getColumnWidth(i);
|
|
|
|
|
int newColumnWidth = Math.max(minColumnWidth, columnWidth);
|
|
|
|
|
sheet.setColumnWidth(i, newColumnWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将工作簿写入文件
|
|
|
|
|
try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
|
|
|
|
|
workbook.write(fileOut);
|
|
|
|
|
}
|
|
|
|
|
// 关闭工作簿
|
|
|
|
|
workbook.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|