添加新行后Excel文件不会更改

我开始做一个新的过程,从一个Excel文件添加行到另一个Excel文件,换句话说:追加行到一个现有的Excel表。

为了做到这一点,我正在testing下面的代码,基于我在互联网上find的代码:

私人全球声明:

private HSSFRow row; private HSSFCell cell; private FileInputStream inFile; private POIFSFileSystem poiFile; private HSSFSheet excelSheet = null; private HSSFWorkbook excelBook = null; private FileOutputStream outFile = null; private static String outputFileName; 

我用主要的方法开始一切:

 public static void main(String args[]){ outputFileName = "C:\\test1.xls"; List<String> newContent = new ArrayList<>(); newContent.add("Test Val1"); newContent.add("Test Val2"); newContent.add("Test Val3"); testingClass test = new testingClass(); test.overwriteExcel(outputFileName,newContent); } 

有了这个我创build的文件,并与我假装更新它:

 public void overwriteExcel(String outputFileName, List<String> newContent){ try{ if(new File(outputFileName).exists()){ //--If the file exists/is already created outFile = new FileOutputStream(new File(outputFileName),true); /*With the "true" parameter, the change must been applied but, nothing happens...*/ inFile = new FileInputStream(new File(outputFileName)); poiFile = new POIFSFileSystem(inFile); excelBook = new HSSFWorkbook(poiFile); excelSheet = excelBook.getSheetAt(0); }else{ //--Create the file outFile = new FileOutputStream(new File(outputFileName)); excelBook = new HSSFWorkbook(); excelSheet = excelBook.createSheet("Sheet1"); } int i = 0; for(Iterator iter = newContent.iterator(); iter.hasNext();){ Object val = iter.next(); if(val!=null){ row = excelSheet.createRow(excelSheet.getLastRowNum()+1); cell = row.createCell(0); //--Temporaly, I change this val manually cell.setCelval(newContent.get(i++)); } } excelBook.write(outFile); outFile.flush(); outFile.close(); }catch(Exception ex){ JOptionPane.showMessageDialog(null,"Error: "+ex.getMessage(),"Warning!",JOptionPane.ERROR_MESSAGE); } } 

当课程结束他的过程时,我看到了文件,我可以感知文件大小的变化,但是当我打开它,一切都是一样的,closures文件后,这将返回到他以前的大小…

试图了解发生了什么,我改变了“0”row.createCell(0)中的“1”,我删除文件,重复该过程,一切正常,但之后,当我再次将值更改为“0”还没有被应用…我不知道这是否是一些文件权限…

我认为代码有问题,这不是我第一次使用excel文件,但是这是我第一次尝试修改它。

提前,谢谢!

我曾经在同样的情况下挣扎,而且很奇怪,你不必以追加模式打开输出stream:

简而言之; 更改

 outFile = new FileOutputStream(new File(outputFileName),true); 

 outFile = new FileOutputStream(new File(outputFileName)); 

原因在于写入方法倾向于将完整的工作簿写入输出stream,并且以附加模式打开意味着将现有工作簿“附加”到由此创build的新工作簿并且不会发生(这似乎“失败”无一例外)。

相反,输出stream应该以“新”模式打开(没有布尔值为真),这样就有一个单独的最新和最好的工作簿,作为一个整体写入stream中。

希望它是有道理的! 🙂

我已经对你的代码进行了一些修改,以便我理解下面的POI版本,这个工作非常好:

 private FileInputStream inFile; private POIFSFileSystem poiFile; private XSSFRow row; private XSSFCell cell; private XSSFSheet excelSheet = null; private XSSFWorkbook excelBook = null; private static String outputFileName; public void overwriteExcel(File file, List<String> newContent) { try { if (file.exists()) { inFile = new FileInputStream(file); // poiFile = new POIFSFileSystem(inFile); excelBook = new XSSFWorkbook(inFile); excelSheet = excelBook.getSheetAt(0); System.out.println("Here -> " + excelSheet.getLastRowNum()); } else { // --Create the file excelBook = new XSSFWorkbook(); excelSheet = excelBook.createSheet("Sheet1"); } int i = 0; for (String val : newContent.toArray(new String[newContent.size()])) { if (val != null) { System.out.println("Row " + excelSheet.getLastRowNum()); row = excelSheet.createRow(excelSheet.getLastRowNum() + 1); cell = row.createCell(0); cell.setCellValue(newContent.get(i++)); } } FileOutputStream outFile = new FileOutputStream(file); excelBook.write(outFile); // outFile.flush(); outFile.close(); } catch (Exception ex) { /* * JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(), * "Warning!", JOptionPane.ERROR_MESSAGE); */ ex.printStackTrace(); } } public static void main(String args[]) { outputFileName = "demo4.xlsx"; List<String> newContent = new ArrayList<>(); newContent.add("Test Val1"); newContent.add("Test Val2"); newContent.add("Test Val3"); WriteExcel test = new WriteExcel(); test.overwriteExcel(new File(outputFileName), newContent); }