Excel根据模板生成数据

就是有个模板,完全保留模板样式的情况下,在相应的地方更改或保留数据

//参数按顺序分别是模板路径,生成后的文件保存到的路径,替换的内容(键为“行-列”),第几个sheet

public static void getExcelMB(String pathIn,String pathOut,Map<String, Object> changeMap,int SheetNum) throws IOException{
Workbook workbook;
Sheet sheet;
String bb;

try {
Set<String> keySet=changeMap.keySet();
FileInputStream is=new FileInputStream(pathIn);
workbook=new HSSFWorkbook(is);
sheet=(HSSFSheet) workbook.getSheetAt(SheetNum);
for (String key : keySet) {
String[] hl=key.split("-");
Row row=sheet.getRow(Integer.parseInt(hl[0]));
Cell cell=row.getCell(Integer.parseInt(hl[1]));
cell.setCellValue(String.valueOf(changeMap.get(key)));
}

FileOutputStream fileOut = new FileOutputStream(pathOut);
workbook.write(fileOut);
fileOut.close();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
posted @ 2018-04-08 15:23  Ice_Blue_Brother  阅读(430)  评论(0编辑  收藏  举报