POI写入xlsx文件
作者:互联网
我们在开发中经常会遇到将信息打印到xlsx文件中的功能,可以使用POI来完成。
创建Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
创建工作表sheet
HSSFSheet sheet = workbook.createSheet();
创建第一行,标题行
HSSFRow rowFirst = sheet.createRow(0);
创建第一列
HSSFCell cellFirst = rowFirst.createCell(0);
设置单元格内容
cellFirst.setCellValue("上传失败文件");
合并单元格(起始行, 终止行, 起始列, 终止列)
CellRangeAddress cra =new CellRangeAddress(0, 0, 0, 3);
sheet.addMergedRegion(cra);
设置单元格样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
设置字体样式
Font fontStyle = workbook.createFont();
字体加粗
fontStyle.setBold(true);
设置字体
fontStyle.setFontName("黑体");
设置字体大小
fontStyle.setFontHeightInPoints((short) 22);
将字体样式添加到单元格样式中
cellStyle.setFont(fontStyle);
设置对齐格式为居中
cellStyle.setAlignment(HorizontalAlignment.CENTER.getCode);
设置边框
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
为刚刚的单元格添加样式
cellFirst.setCellStyle(cellStyle);
第二行,表头行(记得更改字体样式)
HSSFRow row = sheet.createRow(1);
添加数据
String[] title = {"#","文件路径","文件名称","上传失败原因"};
HSSFCell cell = null;
for (int i = 0;i < title.length;i ++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(cellStyle);
}
追加数据
int index = 2;
for (String fileName : errorFileNameMap.keySet()){
HSSFRow nextrow = sheet.createRow(index);
HSSFCell cell2 = nextrow.createCell(0);
cell2.setCellValue(index - 1);
cell2.setCellStyle(cellStyle);
cell2=nextrow.createCell(1);
cell2.setCellValue(errorFileNameMap.get(fileName));
cell2.setCellStyle(cellStyle);
cell2 = nextrow.createCell(2);
cell2.setCellValue(fileName);
cell2.setCellStyle(cellStyle);
cell2 = nextrow.createCell(3);
cell2.setCellValue("文件名称格式不匹配");
cell2.setCellStyle(cellStyle);
index ++;
}
创建文件
File file=new File("C:\\var\\my\\上传失败文件.xlsx");
写入文件
file.createNewFile();
FileOutputStream stream = org.apache.commons.io.FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
示例代码
/**
* POI写入xlsx文件
*
* @param errorFileNameMap <文件名,文件绝对路径>
* @param dbErrorFileNameMap <文件名,文件绝对路径>
*/
public static void POIExcelCre(Map<String,String> errorFileNameMap,Map<String,String> dbErrorFileNameMap){
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow rowFirst = sheet.createRow(0);
HSSFCell cellFirst = rowFirst.createCell(0);
cellFirst.setCellValue("上传失败文件");
CellRangeAddress cra =new CellRangeAddress(0, 0, 0, 3);
sheet.addMergedRegion(cra);
HSSFCellStyle cellStyle = workbook.createCellStyle();
Font fontStyle = workbook.createFont();
fontStyle.setBold(true);
fontStyle.setFontName("黑体");
fontStyle.setFontHeightInPoints((short) 22);
// 将字体样式添加到单元格样式中
cellStyle.setFont(fontStyle);
// 边框,居中
cellStyle.setAlignment(HorizontalAlignment.CENTER.getCode);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellFirst.setCellStyle(cellStyle);
fontStyle.setBold(false);
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short) 12);
cellStyle.setFont(fontStyle);
//第二行,表头行
HSSFRow row = sheet.createRow(1);
String[] title = {"#","文件路径","文件名称","上传失败原因"};
HSSFCell cell = null;
for (int i = 0;i < title.length;i ++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(cellStyle);
}
//追加数据
int index = 2;
for (String fileName : errorFileNameMap.keySet()){
HSSFRow nextrow = sheet.createRow(index);
HSSFCell cell2 = nextrow.createCell(0);
cell2.setCellValue(index - 1);
cell2.setCellStyle(cellStyle);
cell2=nextrow.createCell(1);
cell2.setCellValue(errorFileNameMap.get(fileName));
cell2.setCellStyle(cellStyle);
cell2 = nextrow.createCell(2);
cell2.setCellValue(fileName);
cell2.setCellStyle(cellStyle);
cell2 = nextrow.createCell(3);
cell2.setCellValue("文件名称格式不匹配");
cell2.setCellStyle(cellStyle);
index ++;
}
if (dbErrorFileNameMap != null){
for (String fileName : dbErrorFileNameMap.keySet()){
HSSFRow nextrow = sheet.createRow(index);
HSSFCell cell2 = nextrow.createCell(0);
cell2.setCellValue(index - 1);
cell2.setCellStyle(cellStyle);
cell2=nextrow.createCell(1);
cell2.setCellValue(dbErrorFileNameMap.get(fileName));
cell2.setCellStyle(cellStyle);
cell2 = nextrow.createCell(2);
cell2.setCellValue(fileName);
cell2.setCellStyle(cellStyle);
cell2 = nextrow.createCell(3);
cell2.setCellValue("订单中心无此合同/报价编号");
cell2.setCellStyle(cellStyle);
index ++;
}
}
//创建文件
File file=new File("C:\\var\\genechem\\上传失败文件.xlsx");
try {
file.createNewFile();
FileOutputStream stream = org.apache.commons.io.FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
}catch (Exception e){}
}
标签:xlsx,cellStyle,setCellStyle,cell2,写入,nextrow,createCell,POI,setCellValue 来源: https://blog.csdn.net/weixin_44307338/article/details/122462599