在Apache POI中打开EXISTING xls

我有一个现有的文件(C:\ wb.xls),我想打开并进行更改。 如何在Apachie POI中打开现有文件? 我找到的所有文档都必须与创建新文件一起使用。 如果您知道,如何在xls文件的顶部插入新行或如何自动格式化列宽?

使用以下之一

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(xlFileAddress)); 

要么

  Workbook wb = WorkbookFactory.create(new File(xlFileAddress)); 

要么

  Workbook wb = WorkbookFactory.create(new FileInputStream(xlFileAddress)); 

然后使用wb创建/读取/更新工作表/行/单元格,无论你想要什么。 详情请访问此处 。 这肯定会对你有所帮助。

您是否尝试过阅读Apache POI HowTo“阅读或修改现有文件” ? 这应该涵盖你……

基本上,您要做的就是从QuickGuide中获取,例如用于加载文件

 Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); Sheet s = wb.getSheetAt(0); // Get the 11th row, creating if not there Row r1 = s.getRow(10); if (r1 == null) r1 = s.createRow(10); // Get the 3rd column, creating if not there Cell c2 = r1.getCell(2, Row.CREATE_NULL_AS_BLANK); // Set a string to be the value c2.setCellValue("Hello, I'm the cell C10!"); // Save FileOutputStream out = new FileOutputStream("New.xls"); wb.write(out); out.close();