作者:乔山办公网日期:
返回目录:excel表格制作
HSSFWorkbook wb = new HSSFWorkbook();//建立新e68a84e79fa5e98193366HSSFWorkbook对象HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象HSSFRow row = sheet.createRow((short)0);//建立新行HSSFCell cell0 = row.createCell((short)0);//建立新cellHSSFCell cell1 = row.createCell((short)1);//建立新cellcell0.setCellValue("物品");cell1.setCellValue("价格");HSSFRow row1 = sheet.createRow((short)1);//建立新行cell0 = row1.createCell((short)0);//建立新cellcell1 = row1.createCell((short)1);//建立新cellcell0.setCellValue("一、期初物品");cell1.setCellValue("");HSSFRow row2 = sheet.createRow((short)2);//建立新行cell0 = row2.createCell((short)0);//建立新cellcell1 = row2.createCell((short)1);//建立新cellcell0.setCellValue("二、当期物品");cell1.setCellValue("");FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();
你查出的列可能有空的值,判断一下吧,如果为null就添加一个"",应该是这个问题,我原来遇到过,它不允许你添加进单元格的值为null
XSSF不能读取Excel2003以前(包括2003)的版本百,
没需要就按你之前的继续,度如果在读取前判断文件是知2003前的版本还是2007的版本,提供个思路。XSSF和HSSF虽然道在不同的包里,但却引版用了同一接口Workbook,
Workbook book = null;
try {
book = new XSSFWorkbook(excelFile);
} catch (Exception ex) {
book = new HSSFWorkbook(new FileInputStream(excelFile));
}
各版本的Excel中测试,没有权发生异常