2013-07-09 94 views
5

我有一個現有文件(C:\ wb.xls),我想打開並進行更改。如何在Apachie POI中打開現有文件?我發現的所有文檔都需要創建一個新文件。如果您知道,您如何在xls文件的頂部插入新行或如何自動填寫列寬?在Apache POI中打開現有的xls

+0

看看本教程是否有幫助:http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/ – cmbaxter

回答

4

您是否試過閱讀Apache POI HowTo "Reading or modifying an existing file"?這應該包括你...

基本上,你會想要做的就是取自什麼QuickGuidethis 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(); 
14

使用一箇中,下述

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

OR

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

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

然後使用wb創建/讀取/更新表/行/單元任何你想要的。詳情請訪問here。這一定會對你有所幫助。

+1

請注意[從InputStream打開比較慢,需要的內存比從'File'打開,如POI文檔中所述](http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream) – Gagravarr

+0

注意如果使用maven,則需要依賴poi-ooxml而不僅僅是使用WorkbookFactory類。 –