Showing posts with label export to excel. Show all posts
Showing posts with label export to excel. Show all posts

Tuesday, 26 February 2013

Format cell style in Excel document? Java Export Excel

package org.xxx.example.poi;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;

public class ExcelCellFormat {
    public static void main(String[] args) {
        //
        // Create an instance of workbook and sheet
        //
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();

        //
        // Create an instance of HSSFCellStyle which will be use to format the
        // cell. Here we define the cell top and bottom border and we also
        // define the background color.
        //
        HSSFCellStyle style = workbook.createCellStyle();
        style.setBorderTop((short) 6); // double lines border
        style.setBorderBottom((short) 1); // single line border
        style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

        //
        // We also define the font that we are going to use for displaying the
        // data of the cell. We set the font to ARIAL with 20pt in size and
        // make it BOLD and give blue as the color.
        //
        HSSFFont font = workbook.createFont();
        font.setFontName(HSSFFont.FONT_ARIAL);
        font.setFontHeightInPoints((short) 20);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.BLUE.index);
        style.setFont(font);

        //
        // We create a simple cell, set its value and apply the cell style.
        //
        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(1);
        cell.setCellValue(new HSSFRichTextString("Hi there... It's me again!"));
        cell.setCellStyle(style);      
        sheet.autoSizeColumn((short) 1);

        //
        // Finally we write out the workbook into an excel file.
        //
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("ExcelDemo.xls"));
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Export Excel from JAVA using POI

use POI jar for the same

package xx;

import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class ExportToExcel {

    public HSSFWorkbook createExcelFile() {
        return new HSSFWorkbook();
    }

    public HSSFSheet creatExcelSheet(String sheetName, HSSFWorkbook wb) {
        return wb.createSheet(sheetName);
    }

    public HSSFWorkbook createExcelForImport(String header[], List data, String sheetname) {

        HSSFWorkbook wb = createExcelFile();
        HSSFSheet hSSFSheet = creatExcelSheet(sheetname, wb);
        //set header
        HSSFRow row = hSSFSheet.createRow((short) 0);
        for (int i = 0; i < header.length; i++) {
            String headerTitle = header[i];
            HSSFCell cell = row.createCell((short) i);
            HSSFCellStyle style = wb.createCellStyle();
            style.setFillBackgroundColor(HSSFColor.DARK_YELLOW.index);
            HSSFFont font = wb.createFont();
//            font.setFontHeightInPoints((short) 20);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            style.setFont(font);
            cell.setCellStyle(style);
            cell.setCellValue(headerTitle);
        }
        //setData
        int counter = 0;
        int sheetCounter = 1;
        for (int i = 0; i < data.size(); i++) {
            counter++;
            if (counter == 32766) {
                hSSFSheet = creatExcelSheet(sheetname + sheetCounter++, wb);
                counter = 0;
            }
            row = hSSFSheet.createRow((short) counter);
            hSSFSheet.autoSizeColumn((short) 1);
            Object[] importData = (Object[]) data.get(i);
            for (int j = 0; j < importData.length; j++) {
                Object object = importData[j];
                HSSFCell cell = row.createCell((short) j);
                cell.setCellValue(object != null ? object.toString() : "");

            }
        }
        return wb;
    }
}