在Apache POI中打开EXISTING xls

时间:2013-07-09 18:59:57

标签: java apache-poi

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

2 个答案:

答案 0 :(得分:18)

使用以下

中的一个
 XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(xlFileAddress));

OR

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

OR

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

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

答案 1 :(得分:8)

您是否尝试过阅读Apache POI HowTo "Reading or modifying an existing file"?这应该包括你......

基本上,您要做的是取自QuickGuide,例如this for loading a File

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();