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]