wood burning stoves 2.0*
The moose likes Java in General and the fly likes Writing in existing excel(.xls) file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Writing in existing excel(.xls) file

harry ganguly
Greenhorn

Joined: Dec 14, 2010
Posts: 13
Hi All,
I want to write in an existing excel(in sheet1) file in a perticular cell(say row=1,cell=6). I'm using poi jar. But I could not write in that perticular cell.
String val= //value that I want to write;

String filePath = //getting the filepath. ;
File file=new File(filePath);
FileInputStream fis=new FileInputStream(file);


HSSFWorkbook wb=new HSSFWorkbook(fis);
HSSFSheet st=wb.getSheet("Sheet1");
HSSFRow row=st.getRow(1);
HSSFCell cell= row.getCell(6);
cell.setCellValue(val);
fis.close();


This should write val to sheet1, row 1 cell 6 of the perticular excel that I'm passing. In filepath I'm passing file path along with file name.

But this is not working though not throwing any errors.
Could someone please help?
Joshua Barrett
Greenhorn

Joined: Sep 08, 2010
Posts: 27
The FIS is used to create the HSSFWorkbook object.

I dont believe that changing cell values within the workbook object directly affects the FIS.

In my experiances with POI I have used xls files as a template but never had to ammend an existing file.

Perhaps close the current stream and write over the original file. I'm sure there is a more efficent way.

May I reccomend you use



instead of



Assuming sheet 1 is the first sheet in your workbook. You never know if you'll want to give the xls file a meaning full name and this will avoid you having to redeploy code.

harry ganguly
Greenhorn

Joined: Dec 14, 2010
Posts: 13
Thanks Joshua for pointing out the general way of pointing a sheetname.

Though this does not serve my requirement. If I go with your way, then the original file will be updated rather than getting updated. I just want to add one cell value rather than rewriting the file. So this is like append rather than overwrite

Could someone please give me some idea on this???
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Writing in existing excel(.xls) file

harry, please CarefullyChooseOneForum and UseOneThreadPerQuestion.

I'm locking this thread.


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Writing in existing excel(.xls) file