最近的一个项目中要生成excel文档,考虑到客户给的文件实在麻烦,用POI来全新生成太麻烦,关键是布局不好弄,但是通过读取已经存在的模板来生成excel文件代码就简化多了,以下是一段简单的代码实例:
try
{
POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream("c:\\template.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell((short)2);
if (cell == null)
cell = row.createCell((short)2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("testtest");
// 输出文件
FileOutputStream fileOut = new FileOutputStream("c:\\test.xls");
wb.write(fileOut);
fileOut.close();
}catch(Exception e)
{
e.printStackTrace();
}