(9) 如何用Apache POI操作Excel檔案-----應用條件樣式(ConditionalFormatting),

來源:互聯網
上載者:User

(9) 如何用Apache POI操作Excel檔案-----應用條件樣式(ConditionalFormatting),

有的時候,我們可能想根據一些條件,把Excel工作表中的一些資料,根據某個特定的條件,修改一下滿足條件的單元格的樣式.

其中比較操作符對象ComparisonOperator支援除了BWTWEEN和NOT_BEWTEEN之外的其他的7個操作符。

package org.apache.poi.ss.usermodel;public final class ComparisonOperator{  public static final byte NO_COMPARISON = 0;  public static final byte BETWEEN = 1;  public static final byte NOT_BETWEEN = 2;  public static final byte EQUAL = 3;  public static final byte NOT_EQUAL = 4;  public static final byte GT = 5;  public static final byte LT = 6;  public static final byte GE = 7;  public static final byte LE = 8;    public ComparisonOperator() {}}


示範代碼如下:

import java.io.FileOutputStream;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.ComparisonOperator;import org.apache.poi.ss.usermodel.ConditionalFormattingRule;import org.apache.poi.ss.usermodel.FontFormatting;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.PatternFormatting;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.SheetConditionalFormatting;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.usermodel.XSSFCellStyle;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ConditionFormatDemo { public static void main(String[] args) throws Exception {   //1. 產生5行9列的資料    XSSFWorkbook wb = new XSSFWorkbook();     XSSFSheet sheet = wb.createSheet("Sheet1");        XSSFCellStyle style = wb.createCellStyle();        for(int i=0;i<5;i++){        Row row = sheet.createRow((short)i);        for(int j=0;j<9;j++){        Cell cell = row.createCell((short) j);        cell.setCellValue((i+1)*10+(j+1));        cell.setCellStyle(style);         }        }        //2.設定Condition Format        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.GT, "50");        PatternFormatting fill1 = rule1.createPatternFormatting();        fill1.setFillBackgroundColor(IndexedColors.RED.index);        fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);        FontFormatting  fontFormatting=rule1.createFontFormatting();        fontFormatting.setFontStyle(true, true);                CellRangeAddress[] regions = {                CellRangeAddress.valueOf("A1:I5")        };                    sheetCF.addConditionalFormatting(regions, rule1);        // 3.輸出Excel檔案        FileOutputStream fileOut = new FileOutputStream("conditionFormatTest.xlsx");        wb.write(fileOut);        fileOut.close();    }}

運行後的效果如下,


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.