Open EXISTING xls in Apache POI

女生的网名这么多〃 提交于 2020-01-10 04:49:04

问题


I have an existing file (C:\wb.xls) that I want to open and make changes to. How in Apachie POI do you open an existing file? All the documentation that I find has to go with creating a new file. And if you know, how do you insert a new row at the top of the xls file or how to autoformat column widths?


回答1:


Use one among the following

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

OR

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

OR

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

and then use wb for creating/reading/updating sheets/rows/cell whatever you want. For detail visit here. This will definitely help you.




回答2:


Did you try reading the Apache POI HowTo "Reading or modifying an existing file"? That should cover you...

Basically, what you'll want to do is taken from the QuickGuide eg 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();


来源:https://stackoverflow.com/questions/17556108/open-existing-xls-in-apache-poi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!