Java添加、讀取Excel公式

操作excel表格用公式來處理數據時,可通過創建公式來運算數據,或通過讀取公式來獲取數據信息來源。本文以通過Java代碼來演示在Excel中創建及讀取公式的方法。這裏使用了Excel Java類庫(Free Spire.XLS for Java 免費版),在官網下載獲取文件包後,解壓,將lib文件夾下的jar文件導入Java程序;或者通過maven倉庫下載並導入。導入結果如下:
Java添加、讀取Excel公式

1. 創建公式

import com.spire.xls.*;

public class AddFormula {
    public static void main(String[] args) {
        //創建Workbook對象
        Workbook wb = new Workbook();

        //獲取第一個工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //聲明兩個變量
        int currentRow = 1;
        String currentFormula = null;

        //設置列寬
        sheet.setColumnWidth(1, 32);
        sheet.setColumnWidth(2, 16);

        //寫入用於測試的數據到單元格
        sheet.getCellRange(currentRow,1).setValue("測試數據:");
        sheet.getCellRange(currentRow,2).setNumberValue(1);
        sheet.getCellRange(currentRow,3).setNumberValue(2);
        sheet.getCellRange(currentRow,4).setNumberValue(3);
        sheet.getCellRange(currentRow,5).setNumberValue(4);
        sheet.getCellRange(currentRow,6).setNumberValue(5);

        //寫入文本
        currentRow += 2;
        sheet.getCellRange(currentRow,1).setValue("公式:") ; ;
        sheet.getCellRange(currentRow,2).setValue("結果:");

        //設置單元格格式
        CellRange range = sheet.getCellRange(currentRow,1,currentRow,2);
        range.getStyle().getFont().isBold(true);
        range.getStyle().setKnownColor(ExcelColors.LightGreen1);
        range.getStyle().setFillPattern(ExcelPatternType.Solid);
        range.getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeBottom).setLineStyle(LineStyleType.Medium);

        //算數運算
        currentFormula = "=1/2+3*4";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //日期函數
        currentFormula = "=TODAY()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);
        sheet.getCellRange(currentRow,2).getStyle().setNumberFormat("YYYY/MM/DD");

        //時間函數
        currentFormula = "=NOW()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);
        sheet.getCellRange(currentRow,2).getStyle().setNumberFormat("H:MM AM/PM");

        //IF函數
        currentFormula = "=IF(B1=5,\"Yes\",\"No\")";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //PI函數
        currentFormula = "=PI()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //三角函數
        currentFormula = "=SIN(PI()/6)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //計數函數
        currentFormula = "=Count(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //最大值函數
        currentFormula = "=MAX(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //平均值函數
        currentFormula = "=AVERAGE(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //求和函數
        currentFormula = "=SUM(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //保存文檔
        wb.saveToFile("AddFormulas.xlsx",FileFormat.Version2013);
        wb.dispose();
    }
}

公式創建結果:
Java添加、讀取Excel公式

2.讀取公式

import com.spire.xls.*;

public class ReadFormula {
    public static void main(String[] args) {
        //加載Excel文檔
        Workbook wb = new Workbook();
        wb.loadFromFile("AddFormulas.xlsx");

        //獲取第一個工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //遍歷B1到B13的單元格
        for (Object cell: sheet.getCellRange("B1:B13"))
        {
            CellRange cellRange = (CellRange)cell;

            //判斷單元格是否含有公式
            if (cellRange.hasFormula())
            {
                //打印單元格及公式
                String certainCell = String.format("單元格[%d, %d]含有公式:", cellRange.getRow(), cellRange.getColumn());
                System.out.println(certainCell + cellRange.getFormula());
            }
        }
    }
}

公式讀取結果:
Java添加、讀取Excel公式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章