poi 根据模板导出excel
程序员文章站
2022-07-13 12:41:21
...
public static void main(String[] args) throws Exception{
//获取系统文档
InputStream inp=new FileInputStream(new File("D:\\workspace\\idea\\company\\new_gljy\\ssh_gljy\\201910rjkf0241\\SharkSucker\\pm\\src\\main\\resources\\static\\file\\进度汇总对比表.xlsx"));
//创建工作薄对象
XSSFWorkbook workbook=new XSSFWorkbook(inp);
//创建工作表对象
// XSSFSheet sheet = workbook.getSheet("Sheet1");
XSSFSheet sheet = workbook.getSheetAt(0);
//得到Excel表格 第6行
XSSFRow row = sheet.getRow(5);
if(null==row){
row=sheet.createRow(5);
}
//得到Excel工作表指定行的单元格 第五列
XSSFCell cell = row.getCell(0);
if(null==cell){
cell=row.createCell(0);
}
//合并单元格加背景色
cell.setCellValue("4554");
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//填充
cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(169, 169, 169)));
cell.setCellStyle(cellStyle);
XSSFCell cell1=null;
for (int i = 0; i < 10; i++) {
cell1 = row.getCell(i);
if(null==cell1){
cell1=row.createCell(i);
}
if(i==9){
XSSFCellStyle cellStyleBord = workbook.createCellStyle();
cellStyleBord.setBorderLeft(BorderStyle.THIN);
cell1.setCellStyle(cellStyleBord);
}
cell1.setCellStyle(cellStyle);
}
FileOutputStream out = new FileOutputStream("D:\\workspace\\idea\\company\\new_gljy\\" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() +".xls");
workbook.write(out);
out.close();
}