parent
d73ba0057a
commit
1252ced11b
@ -0,0 +1,125 @@
|
||||
package com.dsideal.QingLong.YunXiao.Util;
|
||||
|
||||
import com.jfinal.plugin.activerecord.Record;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class YunXiaoExportExcelUtil {
|
||||
|
||||
public void exportToExcel(String filePath, List<Record> list) {
|
||||
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = workbook.createSheet("课程整体");
|
||||
// 创建样式
|
||||
CellStyle headerStyle = createHeaderStyle(workbook);
|
||||
CellStyle dataStyle = createDataStyle(workbook);
|
||||
// 设置列宽
|
||||
sheet.setColumnWidth(0, 4000); // 区域
|
||||
sheet.setColumnWidth(1, 5000); // 机构编号
|
||||
sheet.setColumnWidth(2, 8000); // 机构名称
|
||||
sheet.setColumnWidth(3, 4000); // 办学类型
|
||||
sheet.setColumnWidth(4, 3000); // 教职工数
|
||||
sheet.setColumnWidth(5, 3000); // 专任教师数
|
||||
sheet.setColumnWidth(6, 3000); // 班级数
|
||||
sheet.setColumnWidth(7, 3000); // 在校生数
|
||||
|
||||
// 创建表头
|
||||
Row headerRow = sheet.createRow(0);
|
||||
headerRow.setHeight((short) (28 * 20));
|
||||
|
||||
String[] headers = {
|
||||
"区域", "机构编号", "机构名称", "办学类型",
|
||||
"教职工数", "专任教师数", "班级数", "在校生数"
|
||||
};
|
||||
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
Cell cell = headerRow.createCell(i);
|
||||
cell.setCellValue(headers[i]);
|
||||
cell.setCellStyle(headerStyle);
|
||||
}
|
||||
int rowNum = 1;
|
||||
|
||||
// 填充数据
|
||||
for (Record record : list) {
|
||||
Row row = sheet.createRow(rowNum++);
|
||||
row.setHeight((short) (28 * 20));
|
||||
|
||||
// 区域
|
||||
createCell(row, 0, record.getStr("县区"), dataStyle);
|
||||
// 机构编号
|
||||
createCell(row, 1, record.getStr("单位号"), dataStyle);
|
||||
// 机构名称
|
||||
createCell(row, 2, record.getStr("单位名称"), dataStyle);
|
||||
// 办学类型
|
||||
createCell(row, 3, record.getStr("学校类型"), dataStyle);
|
||||
//教职工数
|
||||
createCell(row, 4, String.valueOf(record.getInt("教职工数")), dataStyle);
|
||||
//专任教师数
|
||||
createCell(row, 5, String.valueOf(record.getInt("专任教师数")), dataStyle);
|
||||
//班级数
|
||||
createCell(row, 6, String.valueOf(record.getInt("班级数")), dataStyle);
|
||||
//在校生数
|
||||
createCell(row, 7, String.valueOf(record.getInt("在校生数")), dataStyle);
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
|
||||
workbook.write(fileOut);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void createCell(Row row, int column, String value, CellStyle style) {
|
||||
Cell cell = row.createCell(column);
|
||||
cell.setCellValue(value != null ? value : "");
|
||||
cell.setCellStyle(style);
|
||||
}
|
||||
|
||||
private CellStyle createHeaderStyle(XSSFWorkbook workbook) {
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
|
||||
// 设置背景色为浅灰色
|
||||
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
||||
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
|
||||
// 设置边框
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
|
||||
// 设置居中对齐
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
|
||||
// 设置字体
|
||||
Font font = workbook.createFont();
|
||||
font.setBold(true);
|
||||
font.setFontHeightInPoints((short) 11);
|
||||
style.setFont(font);
|
||||
|
||||
return style;
|
||||
}
|
||||
|
||||
private CellStyle createDataStyle(XSSFWorkbook workbook) {
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
|
||||
// 设置边框
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
|
||||
// 设置居中对齐
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
|
||||
return style;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue