main
黄海 9 months ago
parent 263b423870
commit 7533f53f5b

@ -159,13 +159,14 @@ public class DataEaseController extends Controller {
*/ */
@Before(GET.class) @Before(GET.class)
@IsLoginInterface({}) @IsLoginInterface({})
@IsNumericInterface({"id"})
public void downSampleExcel(int id) throws IOException { public void downSampleExcel(int id) throws IOException {
String identity_idStr = CookieUtil.getValue(getRequest(), "identity_id"); String identity_idStr = CookieUtil.getValue(getRequest(), "identity_id");
int identity_id = 1; int identity_id = 1;
if (!StrKit.isBlank(identity_idStr)) { if (!StrKit.isBlank(identity_idStr)) {
identity_id = Integer.parseInt(identity_idStr); identity_id = Integer.parseInt(identity_idStr);
} }
String area_name = ""; String area_name = "云南省";
//获取他是哪个城市的管理员 //获取他是哪个城市的管理员
if (identity_id == 2) { if (identity_id == 2) {
area_name = "昆明市"; area_name = "昆明市";
@ -180,7 +181,7 @@ public class DataEaseController extends Controller {
//导出excel //导出excel
String excelFileName = dm.exportExcel(identity_id, tableName, dataSetName, tempDir, area_name); String excelFileName = dm.exportExcel(identity_id, tableName, dataSetName, tempDir, area_name);
//renderFile //renderFile
renderFile(new File(excelFileName), "【" + area_name + "】" + dataSetName + "." + "xlsx"); renderFile(new File(excelFileName), "【样例:" + area_name + "】" + dataSetName + "." + "xlsx");
} }
/** /**

@ -860,76 +860,73 @@ public class ExcelCommonUtil {
} }
} }
if (tableData == null || tableData.isEmpty()) {
//throw new IllegalArgumentException("The list of records must not be empty.");
return;
}
// 获取第一个Record的元数据以确定列名
Record firstRecord = tableData.getFirst();
String[] headers = firstRecord.getColumnNames();
int columnCount = headers.length;
// 创建Excel工作簿 // 创建Excel工作簿
Workbook workbook = new XSSFWorkbook(); Workbook workbook = new XSSFWorkbook();
// 创建一个Excel工作表 // 创建一个Excel工作表
Sheet sheet = workbook.createSheet("Sheet1"); Sheet sheet = workbook.createSheet("Sheet1");
// 创建标题行 if(tableData!=null && !tableData.isEmpty() ){
Row titleRow = sheet.createRow(0); // 获取第一个Record的元数据以确定列名
// 设置标题行的样式 Record firstRecord = tableData.getFirst();
CellStyle titleStyle = workbook.createCellStyle(); String[] headers = firstRecord.getColumnNames();
Font font = workbook.createFont(); int columnCount = headers.length;
font.setBold(true);
font.setFontHeightInPoints((short) 14); // 创建标题行
titleStyle.setFont(font); Row titleRow = sheet.createRow(0);
titleStyle.setBorderBottom(BorderStyle.THIN); // 设置标题行的样式
titleStyle.setBorderLeft(BorderStyle.THIN); CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setBorderRight(BorderStyle.THIN); Font font = workbook.createFont();
titleStyle.setBorderTop(BorderStyle.THIN); font.setBold(true);
font.setFontHeightInPoints((short) 14);
// 填充标题行 titleStyle.setFont(font);
for (int i = 1; i <= columnCount; i++) { titleStyle.setBorderBottom(BorderStyle.THIN);
Cell cell = titleRow.createCell(i - 1); titleStyle.setBorderLeft(BorderStyle.THIN);
cell.setCellValue(headers[i - 1]); titleStyle.setBorderRight(BorderStyle.THIN);
cell.setCellStyle(titleStyle); titleStyle.setBorderTop(BorderStyle.THIN);
}
// 填充标题行
// 创建数据行的样式
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 : tableData) {
Row row = sheet.createRow(rowNum++);
int cellNum = 0;
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
Cell cell = row.createCell(cellNum++); Cell cell = titleRow.createCell(i - 1);
Object value = record.get(headers[i - 1]); cell.setCellValue(headers[i - 1]);
cell.setCellValue(value == null ? "" : value.toString()); cell.setCellStyle(titleStyle);
cell.setCellStyle(dataStyle);
} }
}
// 设置最小列宽 // 创建数据行的样式
int minColumnWidth = 100 * 256 / 7; // 将像素转换为字符单位Excel的单位是1/256个字符宽度 CellStyle dataStyle = workbook.createCellStyle();
for (int i = 0; i < headers.length; i++) { font = workbook.createFont();
sheet.setColumnWidth(i, Math.max(minColumnWidth, sheet.getColumnWidth(i))); 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 : tableData) {
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);
}
}
// 自动调整列宽 // 设置最小列宽
for (int i = 0; i < headers.length; i++) { int minColumnWidth = 100 * 256 / 7; // 将像素转换为字符单位Excel的单位是1/256个字符宽度
sheet.autoSizeColumn(i); for (int i = 0; i < headers.length; i++) {
int columnWidth = sheet.getColumnWidth(i); sheet.setColumnWidth(i, Math.max(minColumnWidth, sheet.getColumnWidth(i)));
int newColumnWidth = Math.max(minColumnWidth, columnWidth); }
sheet.setColumnWidth(i, newColumnWidth);
// 自动调整列宽
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);
}
} }
// 将工作簿写入文件 // 将工作簿写入文件

Loading…
Cancel
Save