當我們在計算處理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()); } } } }