POI簡易協助文檔系列--給Excel設定樣式

來源:互聯網
上載者:User

標籤:

 正如Html需要CSS一樣,我們的POI產生的Excel同樣需要樣式才能更完美的表現我們的資料。下面還是從簡單的例子出發,學習和瞭解POI的樣式設計。

  一、我的位置。

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class ExcelStyle {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定義一個新的活頁簿        Sheet sheet=wb.createSheet("第一個Sheet頁");  // 建立第一個Sheet頁        Row row=sheet.createRow(2); // 建立一個行        row.setHeightInPoints(30);                createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);        createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);        createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);        createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);                FileOutputStream fileOut=new FileOutputStream("D:\\活頁簿.xls");        wb.write(fileOut);        fileOut.close();    }        /**     * 建立一個儲存格並為其設定指定的對齊     * @param wb 活頁簿     * @param row 行     * @param column  列     * @param halign  水平方向對其方式     * @param valign  垂直方向對其方式     */    private static void createCell(Workbook wb,Row row,short column,short halign,short valign){        Cell cell=row.createCell(column);  // 建立儲存格        cell.setCellValue(new HSSFRichTextString("我在這"));  // 設定值        CellStyle cellStyle=wb.createCellStyle(); // 建立儲存格樣式        cellStyle.setAlignment(halign);  // 設定儲存格水平方向對其方式        cellStyle.setVerticalAlignment(valign); // 設定儲存格垂直方向對其方式        cell.setCellStyle(cellStyle); // 設定儲存格樣式    }    }

 效果顯示:

二、我的邊框

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Calendar;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class Border {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定義一個新的活頁簿        Sheet sheet=wb.createSheet("第一個Sheet頁");  // 建立第一個Sheet頁        Row row=sheet.createRow(1); // 建立一個行                Cell cell=row.createCell(1); // 建立一個儲存格        cell.setCellValue(4);                CellStyle cellStyle=wb.createCellStyle();         cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部邊框        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部邊框顏色                cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  // 左邊邊框        cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左邊邊框顏色                cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右邊邊框        cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  // 右邊邊框顏色                cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上邊邊框        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  // 上邊邊框顏色                cell.setCellStyle(cellStyle);        FileOutputStream fileOut=new FileOutputStream("D:\\Border.xls");        wb.write(fileOut);        fileOut.close();    }}

效果顯示:

三、我的背景

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Calendar;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class Bg {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定義一個新的活頁簿        Sheet sheet=wb.createSheet("第一個Sheet頁");  // 建立第一個Sheet頁        Row row=sheet.createRow(1); // 建立一個行                Cell cell=row.createCell(1);        cell.setCellValue("看不清我");        CellStyle cellStyle=wb.createCellStyle();        cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色        cellStyle.setFillPattern(CellStyle.BIG_SPOTS);          cell.setCellStyle(cellStyle);                        Cell cell2=row.createCell(2);        cell2.setCellValue("我的前景色彩與眾不同");        CellStyle cellStyle2=wb.createCellStyle();        cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色彩        cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);          cell2.setCellStyle(cellStyle2);                FileOutputStream fileOut=new FileOutputStream("D:\\bg.xls");        wb.write(fileOut);        fileOut.close();    }}

效果顯示:

 

四、合併儲存格

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Calendar;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.IndexedColors;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;public class GetTogether {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定義一個新的活頁簿        Sheet sheet=wb.createSheet("第一個Sheet頁");  // 建立第一個Sheet頁        Row row=sheet.createRow(1); // 建立一個行                Cell cell=row.createCell(1);        cell.setCellValue("我們被合併儲存格啦!");                sheet.addMergedRegion(new CellRangeAddress(                1, // 起始行                2, // 結束行                1, // 其實列                2  // 結束列        ));                        FileOutputStream fileOut=new FileOutputStream("D:\\Together.xls");        wb.write(fileOut);        fileOut.close();    }}

效果顯示:

 

 

學習這些知識,嘗試做一個藝術氣息的Excel吧。美化你的Excel,讓資料“悅耳”起來。

 

POI簡易協助文檔系列--給Excel設定樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.