Java 新增、讀取 Excel 公式
阿新 • • 發佈:2020-09-03
當我們在計算處理Excel表格中的資料時,為了提高工作效率,我們常常會用到各種Excel函式公式, 本文將介紹如何使用Free Spire.XLS for Java新增公式到Excel單元格,以及如何讀取單元格中的公式。
基本步驟:
1. 下載Free Spire.XLS for Java包並解壓縮,然後將lib資料夾下的Spire.Xls.jar包作為依賴項匯入到Java應用程式中。(也可直接通過Maven倉庫安裝JAR包(配置pom.xml檔案的程式碼見下文))
2. 在Java應用程式中新建一個Java Class(此處我命名為MergeCells 和 UnmergeCells), 然後輸入相應的Java程式碼並執行。
配置pom.xml檔案:
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls.free</artifactId> <version>2.2.0</version> </dependency> </dependencies>
新增公式
import com.spire.xls.*; public class InsertFormulas { public static void main(String[] args) { //建立Workbook物件 Workbook workbook = new Workbook(); //獲取第一個工作表 Worksheet sheet = workbook.getWorksheets().get(0); //宣告兩個變數 int currentRow = 1; String currentFormula= null; //設定列寬 sheet.setColumnWidth(1, 26); 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); //儲存文件 workbook.saveToFile("InsertFormulas.xlsx",FileFormat.Version2013); } }
讀取公式
import com.spire.xls.*; public class ReadFormulas { public static void main(String[] args) { //建立Workbook物件 Workbook workbook = new Workbook(); //載入Excel文件 workbook.loadFromFile("InsertFormulas.xlsx"); //獲取第一個工作表 Worksheet sheet = workbook.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()); } } } }