2013年4月2日 星期二

POI-Excel 2007實作

此篇介紹使用POI套件建立excel檔案

使用的版本為poi-3.8-20120326

將樣式建立一個lib可供擴展及取用

我把建立時會用到的部分都詳列註解在上面

希望可以協助在使用上有問題的user

如果有需要提示下載的部分

可參考另一篇檔案下載

code如下:



package tester;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelTest {

    // 欄位資料
    private static String[] head = { "Item", "Quantity", "Price", "Total" };
    private static Object[][] detail = { { "A",13,20 },
            { "B", 3, 100 }, { "C", 90, 3 } };

    /*
     * 建立一個CellStyle的格式Map,可儲存需要用到的各種格式類型
     */
    private static Map<String, CellStyle> getStyles(Workbook wb) {
        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
        // 欄位樣式
        CellStyle style;
        // 字體樣式
        Font font;

        // 設立新字體格式
        font = wb.createFont();
        //字體樣式
        font.setFontName("標楷體");
        //字體大小
        font.setFontHeightInPoints((short) 20);
        //粗體
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        // 設立新欄位格式
        style = wb.createCellStyle();
        // 欄位水平位置
        style.setAlignment(CellStyle.ALIGN_CENTER);
        // 欄位垂直位置
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        // 加入字體樣式
        style.setFont(font);
        styles.put("title", style);

        font = wb.createFont();
        font.setFontHeightInPoints((short) 13);
        // 設定字體顏色
        font.setColor(IndexedColors.WHITE.getIndex());
        style = wb.createCellStyle();
        //底色
        style.setFillForegroundColor(IndexedColors.RED.getIndex());
        //填滿
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style.setFont(font);
        style.setAlignment(CellStyle.ALIGN_CENTER);
        // 自動換行
        style.setWrapText(true);
        styles.put("head", style);
        
        font = wb.createFont();
        font.setFontHeightInPoints((short) 10);
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_RIGHT);
        //設定邊框大小
        style.setBorderBottom((short) 1);
        style.setBorderTop((short) 1);
        style.setBorderLeft((short) 1);
        style.setBorderRight((short) 1);
        //設定邊框顏色
        style.setBottomBorderColor(IndexedColors.BLUE.getIndex());
        style.setTopBorderColor(IndexedColors.BLUE.getIndex());
        style.setLeftBorderColor(IndexedColors.BLUE.getIndex());
        style.setRightBorderColor(IndexedColors.BLUE.getIndex());
        style.setFont(font);
        styles.put("detail",style);
        
        return styles;
    }

    public static void main(String[] args) throws IOException {
        // 建立一個活頁簿
        Workbook wb;
        // XSSFWorkbook對應.xlsx檔案,若是97-2003 .xls 使用HSSFWorkbook
        wb = new XSSFWorkbook();

        Map<String, CellStyle> styles = getStyles(wb);
        // 建立工作表
        Sheet sheet = wb.createSheet();
        // 設定sheet頁面
        PrintSetup ps = sheet.getPrintSetup();
        // 橫面列印 true
        ps.setLandscape(true);
        // 頁高
        ps.setFitHeight((short) 1);
        // 頁高
        ps.setFitWidth((short) 1);
        //頁首、頁尾  在excel中會再*2.5 變成cm
        ps.setHeaderMargin((double)1);
        ps.setFooterMargin((double)1.5);
        // 列印時水平置中
        sheet.setHorizontallyCenter(true);
        // 調整為一頁
        sheet.setFitToPage(true);
        //頁面 上下左右 在excel中會再*2.5 變成cm
        sheet.setMargin(sheet.BottomMargin,(short)1);
        sheet.setMargin(sheet.TopMargin,(short)1);
        sheet.setMargin(sheet.LeftMargin,(short)1);
        sheet.setMargin(sheet.RightMargin,(short)1);

        // 列、欄
        Row row;
        Cell cell;

        // title
        row = sheet.createRow(0);
        row.setHeightInPoints((short) 30);
        cell = row.createCell(0);
        // 設定欄位格式
        cell.setCellStyle(styles.get("title"));
        // 欄位值
        cell.setCellValue("測試標題一");
        // 合併欄位
        sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$D$1"));

        // head
        row = sheet.createRow(1);
        row.setHeightInPoints((short) 20);
        for(int i=0;i<head.length;i++){
            cell=row.createCell(i);
            cell.setCellValue(head[i]);
            cell.setCellStyle(styles.get("head"));
        }
        
        //detail
        for(int i=0;i<detail.length;i++){
            row=sheet.createRow(i+2);
            cell=row.createCell(0);
            cell.setCellValue((String)detail[i][0]);
            cell.setCellStyle(styles.get("detail"));
            
            cell=row.createCell(1);
            cell.setCellValue((Integer)detail[i][1]);
            cell.setCellStyle(styles.get("detail"));
            
            cell=row.createCell(2);
            cell.setCellValue((Integer)detail[i][2]);
            cell.setCellStyle(styles.get("detail"));
            
            cell=row.createCell(3);
            String fun="B"+(i+3)+"*C"+(i+3);
            cell.setCellFormula(fun);
            cell.setCellStyle(styles.get("detail"));
        }
        
        //儲存檔案
        File file=new File("D:/","test.xlsx");
        FileOutputStream out=new FileOutputStream(file);
        wb.write(out);
        out.close();
    }
}

產出的excel表如下:



版面配置如下:





沒有留言:

張貼留言