Are you writting to the same file that you have open?

-Andy

[EMAIL PROTECTED] wrote:

>Hi Andy,
>
>Thanks for your quick anwser. I tried it with a simple test, but it doesn't
>help. I checked with the debugger immediately before wb.write().The workbook
>object had the new row and cell as well as the changed cell values, but the
>written file showed only the changed cells.
>
>Peter
>
>
>
>
>public void test(){
>
>    HSSFRow r = null;
>    HSSFCell c = null;
>    HSSFSheet s = wb.getSheetAt(0);
>
>    // change cell values
>    c = s.getRow(5).getCell((short)(3));
>    c.setCellType(HSSFCell.CELL_TYPE_STRING);
>    c.setCellValue( "XXXXXX" );
>
>
>    // create a row
>    r = s.createRow((short)15);
>    c = r.createCell( (short)5, HSSFCell.CELL_TYPE_STRING );
>    c.setCellValue("teststring");
>  }
>
>  public boolean write(){
>    // create a new file
>    try{
>
>      FileOutputStream out    = new FileOutputStream("c:\\temp\\test.xls");
>
>      // write the workbook to the output stream
>      wb.write(out);
>      out.close();
>      return true;
>    }
>
>
>
>
>-----Ursprüngliche Nachricht-----
>Von: Andrew C. Oliver [mailto:[EMAIL PROTECTED]]
>Gesendet: Dienstag, 9. April 2002 14:29
>An: poi users
>Betreff: Re: Adding rows to an existing xls-file doesn't work with my
>code
>
>
>Can you try writing a simple test case which opens the workbook, changes
>some records and writes it back adding some hard coded strings (without
>all of the  rest).  
>
>Thanks,
>
>-Andy
>
>On Tue, 2002-04-09 at 07:59, [EMAIL PROTECTED] wrote:
>
>>Hi,
>>
>>I'm new to POI and would like to use it for generating XLS-Files. Now
>>something doesn't work  as I expect. 
>>
>>What should happen:
>>1) I would like to read an xls-file as template (about 20 rows).
>>2) Then some cells in the workbook are changed.
>>3) Then some rows should be added to the workbook.
>>4) The workbook should be written as a new xls file.
>>
>>Everything works fine, the changes under 2) are done, but the rows under
>>
>3)
>
>>are not added. I don't get an exception and using a debugger I've seen
>>
>that
>
>>the rows are created, as well as the cells are created and filled with
>>
>data.
>
>>They are just missing in the written file. 
>>
>>If I create a new workbook instead of reading one, adding rows with
>>
>similar
>
>>code works without any problem.
>>
>>For sure I'm doing something wrong, but I can't find where. Does somebody
>>have a hint for me?
>>
>>
>>The code looks like:
>>
>>//snippet of "caller"-class
>>(...)
>>//produce somehow an arraylist of arrayobjects with strings to write in
>>
>the
>
>>cells
>>ArrayList testdata = (...)
>>FahrplanXLS fp = new FahrplanXLS("test.xls");
>>fp.setFahrplanData( testdata );
>>fp.write();
>>(...)
>>
>>
>>//parts of class with logic for changing cells and adding records to the
>>workbook
>>public class FahrplanXLS {
>>  private HSSFWorkbook  wb = null; // declare a workbook object reference
>>  
>>  /** constructor
>>   *
>>   */      
>>  public FahrplanXLS(String inFilename) {
>>
>>    try
>>    {
>>
>>      Filesystem fs = new Filesystem( new FileInputStream(inFilename) );
>>      // create the workbook
>>      wb = new HSSFWorkbook( fs );
>>    }
>>    catch(FileNotFoundException e){
>>    }
>>    catch(IOException e){
>>    }
>>
>>  }
>>
>>
>>  /**
>>   * writes fahrplan data in the template and adds new rows with
>>
>additional
>
>>   * data
>>   * @param fahrplandata
>>   */
>>  public void setFahrplanData( ArrayList fahrplandata ){
>>    HSSFCell cell = getCellWithTag( XLS_TAG_FAHRPLAN_VALUES );
>>    HSSFSheet sheet = wb.getSheetAt( actualSheetNum );
>>    
>>    //change some cell values of the template
>>    (...)
>>
>>    //get last rownum with data
>>    actualRowNum = sheet.getLastRowNum()+1;
>>    //add more records until arraylist is empty
>>    while( !fahrplandata.isEmpty() ){
>>      addRecord( sheet, actualCellNum, actualRowNum, (String[])
>>fahrplandata.remove(0) );
>>      actualRowNum++;
>>    }
>>  }
>>
>>
>>  /**
>>   * adds a line with Fahrplan data starting at the first row after header
>>information
>>   * @param cellNum  number of cell from the left for first value of the
>>record
>>   * @param rowNum   number of row to create
>>   * @param record   record of Strings with data
>>   */
>>  public void addRecord( HSSFSheet sheet, short cellNum, int rowNum,
>>String[] record ){
>>
>>    // create a row
>>    HSSFRow row = sheet.createRow( (short)rowNum );
>>    HSSFCell cell = null;
>>
>>    // create a cell for each array member and write the data
>>    for( short i = 0; i < record.length; i++ ){
>>      cell = row.createCell( (short)(cellNum + i),
>>
>HSSFCell.CELL_TYPE_STRING
>
>>);
>>      cell.setCellValue( record[i] );
>>    }
>>  }
>>
>>  /**
>>   * writes xls file
>>   * @return true if file was written else false
>>   */
>>  public boolean write(){
>>    // create a new file
>>    try{
>>      FileOutputStream out    = new FileOutputStream(filename);
>>
>>      // write the workbook to the output stream
>>      wb.write(out);
>>      out.close();
>>      return true;
>>    }
>>    catch(Exception e){
>>      //logit
>>      return false;
>>    }
>>  }
>>
>>}
>>
>>
>>Thanks in advance and kind regards,
>>
>>Peter
>>
>>---------------
>>Peter Häberli
>>BKW-FMB
>>Information Technology - Industrial Applications
>>phone ++41 (0)31 330 64 37 / 330 57 21
>>fax ++41 (0)31 330 52 04
>>mailto:[EMAIL PROTECTED]
>>
>>



Reply via email to