main
黄海 2 years ago
parent b727be6889
commit a181bc792a

@ -4,10 +4,11 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import java.util.ArrayList;
@ -31,25 +32,68 @@ public class TestRegex {
}
}
public static final String excelPath="c:\\录取分数导出结果.xlsx";
public static final String excelPath = "c:\\录取分数导出结果.xlsx";
public static void ExportExcel(List<Record> list, List<Record> errlist) throws IOException {
String template = "D:\\dsWork\\FengHuang\\FengHuang\\src\\main\\java\\UnitTest\\录取分数导出模板.xlsx";
//创建工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(template));
//读取第一个工作表(这里的下标与list一样的从0开始取之后的也是如此)
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
XSSFSheet sheet1 = xssfWorkbook.getSheetAt(0);
//正文与表头不是一个颜色
XSSFFont txtFont = xssfWorkbook.createFont();
txtFont.setFontHeightInPoints((short) 14); //字体大小
txtFont.setFontName("宋体"); //字体
XSSFCellStyle cellStyleTxt = xssfWorkbook.createCellStyle();
cellStyleTxt.setFont(txtFont);
cellStyleTxt.setAlignment(HorizontalAlignment.CENTER);
cellStyleTxt.setFillForegroundColor(IndexedColors.LIME.getIndex());
cellStyleTxt.setWrapText(true);//设置自动换行
cellStyleTxt.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleTxt.setBorderBottom(BorderStyle.THIN); // 底部边框
cellStyleTxt.setBorderLeft(BorderStyle.THIN); // 左边边框
cellStyleTxt.setBorderRight(BorderStyle.THIN); // 右边边框
cellStyleTxt.setBorderTop(BorderStyle.THIN); // 上边边框
for (int i = 0; i < list.size(); i++) {
short rowHeight = 30 * 20;
Record record = list.get(i);
XSSFRow row = sheet.createRow(i + 2);
XSSFRow row = sheet1.createRow(i + 2);
XSSFCell cell = row.createCell(0);
cell.setCellStyle(cellStyleTxt);
cell.setCellValue(record.getStr("id"));
row.setHeight(rowHeight);
cell = row.createCell(1);
cell.setCellStyle(cellStyleTxt);
cell.setCellValue(record.getStr("zhuanye"));
row.setHeight(rowHeight);
cell = row.createCell(2);
cell.setCellValue(record.getStr("xf"));
cell.setCellStyle(cellStyleTxt);
row.setHeight(rowHeight);
cell = row.createCell(3);
cell.setCellValue(record.getStr("memo"));
cell.setCellStyle(cellStyleTxt);
row.setHeight(rowHeight);
}
//写入异常数据
XSSFSheet sheet2 = xssfWorkbook.getSheetAt(1);
for (int i = 0; i < errlist.size(); i++) {
short rowHeight = 30 * 20;
Record record = errlist.get(i);
XSSFRow row = sheet2.createRow(i + 2);
XSSFCell cell = row.createCell(0);
cell.setCellStyle(cellStyleTxt);
cell.setCellValue(record.getStr("id"));
row.setHeight(rowHeight);
cell = row.createCell(1);
cell.setCellStyle(cellStyleTxt);
cell.setCellValue(record.getStr("name"));
row.setHeight(rowHeight);
}
//生成文件
@ -132,6 +176,6 @@ public class TestRegex {
}
}
ExportExcel(list, errList);
System.out.println("恭喜,结果数据导出成功,地址:"+excelPath);
System.out.println("恭喜,结果数据导出成功,地址:" + excelPath);
}
}

Loading…
Cancel
Save