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);
HSSFCell cell= row.getCell(6);
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?
Taken from the linked page above...
Modifying the file you have read in is simple. You retrieve the object via an assessor method, remove it via a parent object's remove method (sheet.removeRow(hssfrow)) and create objects just as you would if creating a new xls. When you are done modifying cells just call workbook.write(outputstream)
So basically the suggestion you have been given IS the solution that the makers of the POI library are also giving. Regardless of processes involved the outcome is still the same.
- open an existing xls file
- update a particular cell with a value x
In your code, if after your 'fis.close()' statement you query (through java code!) the value of that specific cell (1:6), does it contain the value?
If yes, than your code has met your requirements.
If you need the new value to be in the xls file that you opened, some form of writing or overwriting will be necessary though.
harry ganguly wrote:Is there anyway I can do it?Can anyone tell me some way to do so??
Take a careful look at all the steps that are taken in the code example I copied here from the POI quick guide.
1. open a file (with a certain path and filename)
2. create a workbook from it
3. navigate to the cell
4. create the cell if it doesn't exist yet
5. fill the cell with a value (of type x)
6. write the workbook to a file (to that same path and with the same filename; overwrite)
If you omit step 6 the update of the cell will never 'persist', will never be saved to the file. The new value will be available in memory though. As the following code demonstrates:
As pointed out by Jan Hoppman: FileInputStream will not be enough to have your update written to the file.
harry ganguly wrote:@Jan: FileOuputStream will create a new wb....which is not my reqmnt.
It is. You need to put your changes into the file, that is, write it. you should just overwrite your old file. Read Wim's post.
harry ganguly wrote:Hi chaitanya,
Can you please give me one example??Since i cant find cell.setCellValue(val) function if i use jxl.jar. How to set val in a cell???
It'd be really helpful if you can put some light on the same.
I'm asking in reference to jxl.jar. Please give an example..
Let the tutorial guide you; if you get in trouble with the code while following the tutorial, I'm sure there's enough helpful hands around to get you on your way.
And a last little tap on the fingers: don't let a small hiccup with one API force you to go to the next one so soon. I feel that we have given quite a lot of pointers as to how to solve your initial problem. If you don't get a grip on that problem while using POI, chances are you won't get a grip with JExcelApi either.
I added code tags to your post, and doesn't it look better. Always use the tags. Thank you for the update.