skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Sunday, September 16, 2012

Creation Of Excel Sheet using Java

Posted by Admin at 12:37 AM – 0 comments
 

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import org.apache.poi.hssf.util.HSSFColor.LAVENDER;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XSSFExcelGenerator {
            protected void buildExcelDocument(String fileName) {
                        FileOutputStream fileOutputStream = null;
                        try {
                                    fileOutputStream = new FileOutputStream(fileName);
                                    // Create Sheet.
                                    XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
                                    XSSFSheet sheet = xssfWorkbook.createSheet("SampleExcelSheet");
                                    // Font setting for sheet.
                                    XSSFFont font = xssfWorkbook.createFont();
                                    font.setBoldweight((short) 700);
                                    sheet.setDefaultColumnWidth(30);
                                    // Create Styles for sheet.
                                    XSSFCellStyle headerStyle = xssfWorkbook.createCellStyle();
                                    headerStyle.setFillForegroundColor(LAVENDER.index);
                                    headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
                                    headerStyle.setFont(font);
                                    XSSFCellStyle dataStyle = xssfWorkbook.createCellStyle();
                                    dataStyle.setWrapText(true);
                                    // Create Header Row
                                    XSSFRow headerRow = sheet.createRow(0);
                                    // Write row for header
                                    XSSFCell headerCell1 = headerRow.createCell(0);
                                    headerCell1.setCellStyle(headerStyle);
                                    headerCell1.setCellValue("Employee Name");
                                    XSSFCell headerCell2 = headerRow.createCell(1);
                                    headerCell2.setCellStyle(headerStyle);
                                    headerCell2.setCellValue("Designation");
                                    XSSFCell headerCell3 = headerRow.createCell(2);
                                    headerCell3.setCellStyle(headerStyle);
                                    headerCell3.setCellValue("Country");
                                    // Create First Data Row
                                    XSSFRow dataRow = sheet.createRow(1);
                                    // Write data in data row
                                    XSSFCell cell1 = dataRow.createCell(0);
                                    cell1.setCellStyle(dataStyle);
                                    cell1.setCellValue(new XSSFRichTextString("Sandeep"));
                                    XSSFCell cell2 = dataRow.createCell(1);
                                    cell2.setCellStyle(dataStyle);
                                    cell2.setCellValue(new XSSFRichTextString("Software Engineer"));
                                    XSSFCell cell3 = dataRow.createCell(2);
                                    cell3.setCellStyle(dataStyle);
                                    cell3.setCellValue(new XSSFRichTextString("India"));
                                    // write in excel
                                    xssfWorkbook.write(fileOutputStream);
                        } catch (Exception e) {
                                    e.printStackTrace();
                        } finally {
                                    /*
                                     * Close File Output Stream
                                     */
                                    try {
                                                if (fileOutputStream != null) {
                                                            fileOutputStream.close();
                                                }
                                    } catch (IOException ex) {
                                                ex.printStackTrace();
                                    }
                        }
            }
            public static void main(String[] args) {
                        /*
                         * call method and pass as an argument path of excel file. Please make
                         * sure that path of excel file is correct otherwise it will throw
                         * FileNotFound Exception.
                         */
                        new XSSFExcelGenerator().buildExcelDocument("C:" + File.separator
                                                + "files" + File.separator + "SampleExcel.xlsx");
            }
}

Labels: Excel Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger