使用apache poi修改现有的excel

当我修改服务器中的现有excel文件并复制到本地机器并打开它时, excel could not open because some content is unreadable, do you want to open and repair this workbook? 以下是我的代码。 有人可以build议我如何避免这种情况?

 File f = new File(directory+"users.xlsx"); FileInputStream fileInputStream = new FileInputStream(f); workbook = new XSSFWorkbook(fileInputStream); sheet = workbook.getSheetAt(0); Row row = sheet.getRow(4); for(int i = 0; i<5; i++) { Cell cell = row1.getCell(i); cell.setCellValue(formattedDate); cell.setCellStyle(normalStyle); } FileOutputStream out = new FileOutputStream(f, false); workbook.write(out); out.close(); 

一直试图解决8个小时,没有运气。 如果有人能够阐明这一点,将会感激不尽。

将输出移动到不同的文件,不要忘了closuresinput和输出,并使用这个http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html

 File f = new File(directory+"users.xlsx"); FileInputStream fileInputStream = new FileInputStream(f); Workbook workbook = WorkbookFactory.create(fileInputStream); fileInputStream.close(); sheet = workbook.getSheetAt(0); Row row = sheet.getRow(4); for(int i = 0; i<5; i++) { Cell cell = row1.getCell(i); cell.setCellValue(formattedDate); cell.setCellStyle(normalStyle); } FileOutputStream out = new FileOutputStream(f, false); workbook.write(out); out.close();