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

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/13894.html
標籤:Java
上一篇:Java拷貝——深拷貝與淺拷貝
下一篇:Web服務器之Nginx
