You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Amir Hossein Sharifzadeh <am...@gmail.com> on 2007/06/12 07:30:20 UTC
Re: problem with adding sheets to an Exist Excel File
Dear users/developers,
I have used POI lib in a Java Web Application, I have an already Excel
file contain a lot Sheets and Cells, I want to add additional sheets and
cells to this excel file without lose existing those sheets and cells.
But I can not.
Would you please help me for this or show me sample codes for this purpose.
Thanks a lot
-- Amir
Re: problem with adding sheets to an Exist Excel File
Posted by funky86 <ti...@gmail.com>.
Hello!
I'm in a hurry so I'll just copy/paste the solution for reading an excel
table. Try this on a copy of your file!
1.2.12. Reading and Rewriting Workbooks
POIFSFileSystem fs = new POIFSFileSystem(new
FileInputStream("workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell((short)3);
if (cell == null)
cell = row.createCell((short)3);
Busy Developers' Guide to HSSF Features
Page 7
Copyright © 2002-2006 The Apache Software Foundation All rights reserved.
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
1.2.2. New Sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
1.2.3. Creating Cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
Busy Developers' Guide to HSSF Features
Page 2
Copyright © 2002-2006 The Apache Software Foundation All rights reserved.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Amir Hossein Sharifzadeh wrote:
>
> Dear users/developers,
>
> I have used POI lib in a Java Web Application, I have an already Excel
> file contain a lot Sheets and Cells, I want to add additional sheets and
> cells to this excel file without lose existing those sheets and cells.
> But I can not.
> Would you please help me for this or show me sample codes for this
> purpose.
>
> Thanks a lot
> -- Amir
>
>
--
View this message in context: http://www.nabble.com/Re%3A-problem-with-adding-sheets-to-an-Exist-Excel-File-tf3905581.html#a11073479
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org