利用 poi 工具类将 list 中的内容填充到 excel 表格中
public void exportVehicleParameterComparisonTable(String outpath,String value, List<CarParamDetailEO> list) throws Exception {
String carmodel1 = "";
String carmodel2 = "";
String[] values = value.split(";");
carmodel1 = values[0].split("_")[1];
carmodel2 = values[1].split("_")[1];
String model_path= "D:\hcs\model.xlsx";
File in=new File(model_path);
File out =new File(outpath);
Common.copyFile(in, out);
FileInputStream readFile;
try {
readFile = new FileInputStream(out);
XSSFWorkbook wb = new XSSFWorkbook(readFile);
XSSFSheet sheet1 = wb.getSheet("Sheet1");
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
XSSFCellStyle styleRedFont = wb.createCellStyle();
styleRedFont.setAlignment(XSSFCellStyle.ALIGN_LEFT);
styleRedFont.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
styleRedFont.setBorderBottom(XSSFCellStyle.BORDER_THIN);
styleRedFont.setBorderLeft(XSSFCellStyle.BORDER_THIN);
styleRedFont.setBorderTop(XSSFCellStyle.BORDER_THIN);
styleRedFont.setBorderRight(XSSFCellStyle.BORDER_THIN);
styleRedFont.setWrapText(true);
styleRedFont.setFont(font);
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_LEFT);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
style.setBorderTop(XSSFCellStyle.BORDER_THIN);
style.setBorderRight(XSSFCellStyle.BORDER_THIN);
style.setWrapText(true);
XSSFRow row;
XSSFCell cell;
XSSFRichTextString carmodel1Style = new XSSFRichTextString(carmodel1+"_结构参数");
XSSFRichTextString carmodel2Style = new XSSFRichTextString(carmodel2+"_结构参数");
carmodel1Style.applyFont(0, (carmodel1+"_结构参数").lastIndexOf("_"), font);
carmodel2Style.applyFont(0, (carmodel2+"_结构参数").lastIndexOf("_"), font);
row = sheet1.createRow(0);
cell = row.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("参数项名称");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(carmodel1Style);
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue(carmodel1Style);
cell.setCellStyle(style);
int m = 1;
for (CarParamDetailEO eo : list) {
String orderNumber = eo.getOrderNumber() == null ? "" : eo.getOrderNumber().trim();
String parameterName = eo.getParameterName() == null ? "" : eo.getParameterName().trim();
String paramcontent = eo.getParamcontent() == null ? "" : eo.getParamcontent().trim();
String paramcontent0 = eo.getParamcontent0() == null ? "" : eo.getParamcontent0().trim();
row = sheet1.createRow(m++);
cell = row.createCell(0);
cell.setCellValue(orderNumber);
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(parameterName);
cell.setCellStyle(style);
if (paramcontent.equals(paramcontent0)){
cell = row.createCell(2);
cell.setCellValue(paramcontent);
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue(paramcontent0);
cell.setCellStyle(style);
} else {
cell = row.createCell(2);
cell.setCellValue(paramcontent);
cell.setCellStyle(styleRedFont);
cell = row.createCell(3);
cell.setCellValue(paramcontent0);
cell.setCellStyle(styleRedFont);
}
}
FileOutputStream ouputStream=new FileOutputStream(out);
try {
wb.write(ouputStream);
} catch (Exception e) {
e.printStackTrace();
}finally{
ouputStream.close();
ouputStream.flush();
}
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于