Win a copy of Design for the Mind this week in the Design forum!
    Bookmark Topic Watch Topic
  • New Topic

Writing in existing excel(.xls) file

 
harry ganguly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
Writing in existing excel(.xls) file

harry, please CarefullyChooseOneForum and UseOneThreadPerQuestion.

I'm locking this thread.
 
    Bookmark Topic Watch Topic
  • New Topic