POI操作Excel的工具类

该类用于操作excel:写单元格,设置属性


/**
 * Created by XY on 2016/4/26.
 */
public class ExcelUtils {
    public static HSSFSheet ExcelSheet;
    public static HSSFWorkbook ExcelBook;
    public static HSSFRow Row;
    public static HSSFCell Cell;

    public static HSSFCellStyle greenCellStyle,redCellStyle;

    public static void setExcelFile(String Path,String SheetName) throws Exception{
        FileInputStream ExcelFile = new FileInputStream(Path);
        ExcelBook = new HSSFWorkbook(ExcelFile);
        ExcelSheet = ExcelBook.getSheet(SheetName); 
    }



    public static void setCellData(String Result, int RowNum, int ColNum) throws Exception{
        Row  = ExcelSheet.getRow(RowNum);
        Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
        if (Cell == null) {
            Cell = Row.createCell(ColNum);
            Cell.setCellValue(Result);
        } else {
            Cell.setCellValue(Result);
        }
    }
    public static String getCellDate(int RowNum,int CloNum){
        Cell=ExcelSheet.getRow(RowNum).getCell(CloNum);
        Cell.setCellType(Cell.CELL_TYPE_STRING);//设置cell单元格类型为string

        String cellData=Cell.getStringCellValue();
        return cellData;
    }
    public int getLastRowNums(){
        return ExcelSheet.getLastRowNum();
    }


    public static void setConditionalFormat(){
        SheetConditionalFormatting scf = ExcelSheet.getSheetConditionalFormatting();  
        //设置单元格条件格式
        ConditionalFormattingRule passRule = scf.createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"Failed\"", null);  
        PatternFormatting passFormatting = passRule.createPatternFormatting();  
        passFormatting.setFillBackgroundColor(HSSFColor.RED.index);  

        ConditionalFormattingRule failedRule = scf.createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"Passed\"", null);  
        PatternFormatting failedFormatting = failedRule.createPatternFormatting();  
        failedFormatting.setFillBackgroundColor(HSSFColor.GREEN.index);

        ConditionalFormattingRule nullRule = scf.createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"Null\"", null);  
        PatternFormatting nullFormatting = nullRule.createPatternFormatting();  
        nullFormatting.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);


        ConditionalFormattingRule[] cfRules = {passRule, failedRule,nullRule};

        CellRangeAddress[] regions = {CellRangeAddress.valueOf("E1:H100")};

        scf.addConditionalFormatting(regions, cfRules);
    }

    public static void writeFile(String Path) throws Exception{
        FileOutputStream fileOut = new FileOutputStream(Path);
        ExcelBook.write(fileOut);
        fileOut.flush();
        fileOut.close();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值