import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* This is a sample to create an Excel Sheet using Jakarta POI API
*
* @author Nitin Aggarwal
* @version 1.0
*/
public class CreateXL {
/** A place for the output Excel file to go */
public static String outputFile = “C:/Work/JPOI/excel_in_java.xls”;
public static void main(String argv[]) {
try {
// Create a New XL Document
HSSFWorkbook wb = new HSSFWorkbook();
// Make a worksheet in the XL document created
HSSFSheet sheet = wb.createSheet();
// Create row at index zero ( Top Row)
HSSFRow row = sheet.createRow((short) 0);
// Create a cell at index zero ( Top Left)
HSSFCell cell = row.createCell((short) 0);
// Lets make the cell a string type
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// Type some content
cell.setCellValue(1234567.0);
// cell.setCellValue(“This is Nitin’s Sample Code”);
// The Output file is where the xls will be created
FileOutputStream fOut = new FileOutputStream(outputFile);
// Write the XL sheet
wb.write(fOut);
fOut.flush();
// Done Deal..
fOut.close();
System.out.println(“File Created ..”);
} catch (Exception e) {
System.out.println(“!!BANG!! xlCreate() : ” + e);
}
}
}
Let’s concentrate on just the interesting steps of Jakarta POI usage:
- Create a new Excel document:
workbook = new HSSFWorkbook();
- Make a worksheet in that document and give the worksheet a name:
sheet = workbook.createSheet("Nitin's Sample sheet");
- Set the first three columns’ widths:
sheet.setColumnWidth((short)0,(short)10000 );
- Create the header line:
HSSFRow row = sheet.createRow((short)0);
- Create and set font and cell style:
HSSFFont font = workbook.createFont(); font.setColor(HSSFFont.COLOR_RED); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Create the style HSSFCellStyle cellStyle= workbook.createCellStyle(); cellStyle.setFont(font);
- Use the cell style:
HSSFCell cell = row.createCell((short) 0); cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue("Class Name ");
- Write the output file:
FileOutputStream fOut = new FileOutputStream(outputFile); // Write the Excel sheet workbook.write(fOut); fOut.flush(); // Done deal. Close it. fOut.close();
So Now we know how to write to an excel file using java.
In order to read from an excel file Click Here : http://nitinaggarwal.wordpress.com/2009/02/21/reading-contents-of-excel-using-java-code/
Related
24 Responses so far.
Recent Posts
- 12.1.0.2 RAC Installation – Step by Step installation on VMware
- OSB FTP Poller continuously throwing exception – while no file pending on FTP Location
- Using JMS Transport with OSB
- Installing Spring Tool Suite and configuring Spring for a development PC
- Rest Enabling SOA using OSB in Oracle 12c
Recent Comments
- Nits on Installing Oracle SOA Suite 12c
- Rajesh Krishna on Installing Oracle SOA Suite 12c
- Nits on Installing Oracle SOA Suite 12c
- Rohinii on Installing Oracle SOA Suite 12c
- Mahitha on Oracle Service Bus (OSB) Development Best Practices.
Archives
- December 2015
- May 2015
- November 2014
- September 2014
- August 2014
- July 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- May 2013
- March 2013
- February 2013
- November 2012
- October 2012
- September 2012
- May 2012
- March 2012
- February 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- June 2011
- May 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- March 2010
- February 2010
- December 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- August 2008
- June 2008
- May 2008
Categories
- AIA
- AXIS2
- Best Practices
- BPEL
- BPM
- Budget
- Build
- Design Patterns
- ESB
- Hermes
- Hibernate
- Home Owners
- Java
- JMS
- JSR 168
- Maven
- Mediator
- Messaging
- Nitin
- OER
- OESB
- Oracle
- OSB
- OWSM
- Portlets
- SDLC
- Sequencing
- Singleton
- SOA
- SOAP
- Spring
- Struts
- UK
- Uncategorized
- Web Services
- WLST
Meta
To find out more, including how to control cookies, see here: Cookie Policy
Recent Posts
- 12.1.0.2 RAC Installation – Step by Step installation on VMware
- OSB FTP Poller continuously throwing exception – while no file pending on FTP Location
- Using JMS Transport with OSB
- Installing Spring Tool Suite and configuring Spring for a development PC
- Rest Enabling SOA using OSB in Oracle 12c
Recent Comments
- Nits on Installing Oracle SOA Suite 12c
- Rajesh Krishna on Installing Oracle SOA Suite 12c
- Nits on Installing Oracle SOA Suite 12c
- Rohinii on Installing Oracle SOA Suite 12c
- Mahitha on Oracle Service Bus (OSB) Development Best Practices.
Archives
- December 2015
- May 2015
- November 2014
- September 2014
- August 2014
- July 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- May 2013
- March 2013
- February 2013
- November 2012
- October 2012
- September 2012
- May 2012
- March 2012
- February 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- June 2011
- May 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- March 2010
- February 2010
- December 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- August 2008
- June 2008
- May 2008
Categories
- AIA
- AXIS2
- Best Practices
- BPEL
- BPM
- Budget
- Build
- Design Patterns
- ESB
- Hermes
- Hibernate
- Home Owners
- Java
- JMS
- JSR 168
- Maven
- Mediator
- Messaging
- Nitin
- OER
- OESB
- Oracle
- OSB
- OWSM
- Portlets
- SDLC
- Sequencing
- Singleton
- SOA
- SOAP
- Spring
- Struts
- UK
- Uncategorized
- Web Services
- WLST
Check out this Java Code Library from Aspose.Cells for java where you can find many sample codes in java related to excel spreadsheet including, reading, writing, modifying and even converting excel files to many other formats.
Hey man, nice blog! I need some help. How could I write in an existing file, I know the cell and the row? Any ideas? Thanks!
HI,
my excel sheet is having around 1lakh rows and few colums.
when i am writing this excel to a file using FileOutputStream,
its taking a long time..
Does anybody have an idea to speedup this???
Hi
Can i typecast workbook to writableworkbook in jxl?
If you are using Oracle database, you can write content to Excel file with ORA_EXCEL PL/SQL package (www.oraexcel.com)
can u give me the code for reading only name of excel file?
Hi Mate, Unfortunately I no longer have my workspace saved, I will have to redo the whole thing which I am planning to @ some point but not sure when… You should get help from the API.
i have a small requirement to append data to the already existing excel file???????….please help me
how to append data to already existing excel file???….plz reply
is it possible to create a directory manually??????by using the directory i want to save file??is it possible???if so means,pls give some example also…
Thanks..
thanks to all…. 🙂
Hi,
Is it possible to modify the cells of an existing excel sheet with out changing the content.
It is Sharvan, try to explore the API for the relevant operation.
Hi Nitin
I want to write into already existing excel file,with out replacing the current data.
Hi Nishil,
I have been very busy over the last few months… And would not be able to look at your issue anytime sooner…
Will surely try to give it a shot ASAP.
Apologies for that…
Cheers Nitin
posted Today 3:47:55 PM 0
Thanks Nitin.Its very useful.
I am working on Export to Excel functionality using jxls,where I will search the records,store in a bean,dispaly it in dispaly tag and when click on
the button ‘Export to Excel’ it will go to another jsp where I can select the fileds to export.
While exporting I am hiding the columns that has not been selected using
transformer.setColumnPropertyNamesToHide(hideCols);
and then
transformer.transformXLS(“c:Test.xls”, beans, “C:genereatedsheet.xls”);
But now I dont want to use hide functionality.
Is there any way where I can create excel only with selected fileds.
I dont want to use setColumnPropertyNamesToHide,only selected columns should be created in excel.
Thanks
Sujata
Hi Nitin,
I want to retrieve multiple records from database and write them columwise in each cell of excelsheet.
eg: there are 27 rows with 3 columns. How will we write the code for this using poi?
[…] The busiest day of the year was December 22nd with 145 views. The most popular post that day was Writing to Excel using java code. […]
Nitin rocking all the world
Hi Nitin,
Can you give me any idea how to append to an existing excel file?
regards,
Ankur
Hi Nitin,
Thanks for the code. I am trying to append to the excel file and have modified the statement :
FileOutputStream fOut = new FileOutputStream(outputFile); to
FileOutputStream fOut = new FileOutputStream(outputFile, true);
means i have opened the file in append mode. But the file still remains the same. Can you give me any idea how to append?
Nitin,
can u give any idea if i want to write multiple sheets in one excel file.
// officetools.jar available at http://www.dancrintea.ro/xls-to-pdf/
import officetools.OfficeFile;
FileInputStream fis=new FileInputStream(new File(”test.xls”));
OfficeFile f=new OfficeFile(fis,”localhost”,”8100″, false);
// write 495 to a column
for(int i=0;i<=99;i++)
f.setCell(1,i, “495”);
[…] by Writing to Excel using java code « Nitinaggarwal’s Blog — February 21, 2009 […]