Apache POI

Apache POI

/*
 * Created on 2006-9-13
 */
package com.livedoor.finance.credit.admin.business.event.service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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.Region;

/**
 * @author Warren~@_@
 */
public class CatOutputSpecification2 {
    HSSFWorkbook hssfWorkbook;
    HSSFSheet hssfSheet;
    HSSFRow hssfRow;
    HSSFCell hssfCell;
   
    OutputStream out;
   
    private String fontName = "MS Pゴシツク";
    private short fontSize = (short) 11;
    private short fontTitleSize = (short) 16;
   
    public CatOutputSpecification2() {
       
    }
   
    public static void main(String[] args) {
        CatOutputSpecification2 cat = new CatOutputSpecification2();
        cat.executeExcel();
       
        System.out.println("========");
    }
   
    public void executeExcel() {
        String filePath = null;
        FileOutputStream fos = null;
       
        filePath = "./data/testPOI.xls";
       
        try {
            fos = new FileOutputStream(filePath);
           
            generate(fos);
           
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @param fos
     */
    private void generate(OutputStream out) {
        // TODO Auto-generated method stub
        this.out = out;
       
        drawWB();
    }

    /**
     *
     */
    private void drawWB() {
        // TODO Auto-generated method stub
        hssfWorkbook = new HSSFWorkbook();
       
        hssfSheet = hssfWorkbook.createSheet();
        hssfWorkbook.setSheetName(0, "test", HSSFWorkbook.ENCODING_UTF_16);
       
        hssfSheet.setDisplayGridlines(true);
       
        for (int i = 0; i < 30; i++) {
            hssfSheet.setColumnWidth((short) i, (short) (4.25 * 256));
        }
       
        drawCell();
       
        try {
            hssfWorkbook.write(out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     *
     */
    private void drawCell() {
        // 1~2 row
        HSSFRow row_001 = hssfSheet.createRow(1);
        HSSFRow row_002 = hssfSheet.createRow(2);
       
        HSSFCell cell_001_000 = row_001.createCell((short) 0);
        HSSFCell cell_002_029 = row_002.createCell((short) 29);
       
        hssfSheet.addMergedRegion(new Region(1, (short) 0, 2, (short) 29));
       
        // 4 row
        HSSFRow row_004 = hssfSheet.createRow(4);
       
        HSSFCell cell_004_001 = row_004.createCell((short) 1);
        HSSFCell cell_004_005 = row_004.createCell((short) 5);
        HSSFCell cell_004_013 = row_004.createCell((short) 13);
       
        hssfSheet.addMergedRegion(new Region(4, (short) 5, 4, (short) 13));
       
        // 5~11 row
        HSSFRow row_005 = hssfSheet.createRow(5);
        HSSFRow row_011 = hssfSheet.createRow(11);
       
        HSSFCell cell_005_000 = row_005.createCell((short) 0);
        HSSFCell cell_011_029 = row_011.createCell((short) 29);
       
        hssfSheet.addMergedRegion(new Region(5, (short) 0, 11, (short) 29));
       
        // 13 row
        HSSFRow row_013 = hssfSheet.createRow(13);
       
        HSSFCell cell_013_000 = row_013.createCell((short) 0);
       
        // 14 row
        HSSFRow row_014 = hssfSheet.createRow(14);
       
        HSSFCell cell_014_001 = row_014.createCell((short) 1);
        HSSFCell cell_014_007 = row_014.createCell((short) 7);
        HSSFCell cell_014_008 = row_014.createCell((short) 8);
        HSSFCell cell_014_027 = row_014.createCell((short) 27);
       
        hssfSheet.addMergedRegion(new Region(14, (short) 1, 14, (short) 7));
        hssfSheet.addMergedRegion(new Region(14, (short) 8, 14, (short) 27));
       
        // 15 row
        HSSFRow row_015 = hssfSheet.createRow(14);
       
        HSSFCell cell_015_001 = row_015.createCell((short) 1);
        HSSFCell cell_015_007 = row_015.createCell((short) 7);
        HSSFCell cell_015_008 = row_015.createCell((short) 8);
        HSSFCell cell_015_027 = row_015.createCell((short) 27);
       
        hssfSheet.addMergedRegion(new Region(15, (short) 1, 15, (short) 7));
        hssfSheet.addMergedRegion(new Region(15, (short) 8, 15, (short) 27));
       
        // 16 row
        HSSFRow row_016 = hssfSheet.createRow(14);
       
        HSSFCell cell_016_001 = row_016.createCell((short) 1);
        HSSFCell cell_016_007 = row_016.createCell((short) 7);
        HSSFCell cell_016_008 = row_016.createCell((short) 8);
        HSSFCell cell_016_027 = row_016.createCell((short) 27);
       
        hssfSheet.addMergedRegion(new Region(16, (short) 1, 16, (short) 7));
        hssfSheet.addMergedRegion(new Region(16, (short) 8, 16, (short) 27));
       
        // 17 row
        HSSFRow row_017 = hssfSheet.createRow(14);
       
        HSSFCell cell_017_001 = row_017.createCell((short) 1);
        HSSFCell cell_017_007 = row_017.createCell((short) 7);
        HSSFCell cell_017_008 = row_017.createCell((short) 8);
        HSSFCell cell_017_027 = row_017.createCell((short) 27);
       
        hssfSheet.addMergedRegion(new Region(17, (short) 1, 17, (short) 7));
        hssfSheet.addMergedRegion(new Region(17, (short) 8, 17, (short) 27));
       
        // 18 row
        HSSFRow row_018 = hssfSheet.createRow(14);
       
        HSSFCell cell_018_001 = row_018.createCell((short) 1);
        HSSFCell cell_018_007 = row_018.createCell((short) 7);
        HSSFCell cell_018_008 = row_018.createCell((short) 8);
        HSSFCell cell_018_027 = row_018.createCell((short) 27);
       
        hssfSheet.addMergedRegion(new Region(18, (short) 1, 18, (short) 7));
        hssfSheet.addMergedRegion(new Region(18, (short) 8, 18, (short) 27));
       
        // 19 row
        HSSFRow row_019 = hssfSheet.createRow(14);
       
        HSSFCell cell_019_001 = row_019.createCell((short) 1);
        HSSFCell cell_019_007 = row_019.createCell((short) 7);
        HSSFCell cell_019_008 = row_019.createCell((short) 8);
        HSSFCell cell_019_027 = row_019.createCell((short) 27);
       
        hssfSheet.addMergedRegion(new Region(19, (short) 1, 19, (short) 7));
        hssfSheet.addMergedRegion(new Region(19, (short) 8, 19, (short) 27));
       
        // set style
        cell_001_000.setCellStyle(cellStyle_title());
       
        // set encoding
        cell_001_000.setEncoding(HSSFCell.ENCODING_UTF_16);
 
        // set value
        cell_001_000.setCellValue("テクス");
    }

    /**
     * @return style_title
     */
    private HSSFCellStyle cellStyle_title() {
        HSSFCellStyle style_title = hssfWorkbook.createCellStyle();
        style_title.setFont(getFont_title());
        style_title.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
        return style_title;
    }

    /**
     * @return font_title
     */
    private HSSFFont getFont_title() {
        HSSFFont font_title = hssfWorkbook.createFont();
        font_title.setFontName(fontName);
        font_title.setFontHeightInPoints(fontTitleSize);
        font_title.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        return font_title;
    }
}
~@_@

你可能感兴趣的:(Apache POI)