如何使用Java HSSF附加到现有的Excel文件
我对Java还比较陌生,并且已经拼凑了足够的代码,允许我将数据写入新的excel文件。 但是,我希望它写入(追加到最后)到现有的文件。 prepareDataToWriteToExcel()获取一些数据来写入3列数据。
public List writeDataToExcelFile(String fileName) throws IOException { Map excelData = prepareDataToWriteToExcel(); List receiversList=new ArrayList(); HSSFWorkbook myWorkBook = new HSSFWorkbook(); HSSFSheet mySheet = myWorkBook.createSheet(); HSSFRow myRow = null; HSSFCell myCell = null; Iterator it=excelData.entrySet().iterator(); int rowNum=0; while (it.hasNext()) { myRow = mySheet.createRow(rowNum); Map.Entry pairs = (Map.Entry)it.next(); String[]arr= (String[]) pairs.getValue(); for (int cellNum = 0; cellNum < arr.length ; cellNum++){ myCell = myRow.createCell((short) cellNum); myCell.setCellValue(arr[cellNum]); } receiversList.add(arr[2]); rowNum++; } try{ FileOutputStream out = new FileOutputStream(fileName); myWorkBook.write(out); System.out.println("WRITING TO EXCEL COMPLETED"); out.close(); }catch(Exception e){} return receiversList; }
我发现这是更新现有的Excel工作表的一个很好的例子。 您会注意到,您可以使用要修改的文件创build工作簿。 这将允许您find您可以创build新列的位置并写入数据。
try { FileInputStream file = new FileInputStream(new File("C:\\update.xls")); HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = workbook.getSheetAt(0); Cell cell = null; // Find empty cell // Update the value of cell file.close(); FileOutputStream outFile = new FileOutputStream(new File("C:\\update.xls")); workbook.write(outFile); outFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
- 为什么不从Excel中加载信息(* .xlsx)(使用Apache POI)?
- 程序暂停尝试读取Excel工作簿(Apache POI)
- 创buildXSSFWorkbook实例时发生VerifyError
- 如何在java中使用selenium webdriver中的apache poi在excel文件中创build新工作表
- 在Intellij中为JAVA导入Apache POI
- 使用java读取excel表单的一列
- 无法从具有POI的STRING单元获取NUMERIC值
- 即使在使用apache poi的代码相同的情况下,excel单元也会在不同的机器上收缩
- 通过使用Apache POI的单元格引用字母标识符获取单元格