? ? ? ?Excel是辦公室自動化中非常重要的一款軟件,具有強大的數(shù)據(jù)分析和處理功能。其中,Excel公式(包括函數(shù))起了非常重要的作用。因此,掌握處理公式的能力有利于提高對Excel的應用水平,進而提高工作效率。本文將通過使用Java程序來演示如何添加、讀取Excel公式。
使用工具:Free Spire.XLS for Java (免費版)
Jar文件獲取及導入:
方法1:通過官網下載獲取jar包。解壓后將lib文件夾下的Spire.Xls.jar文件導入Java程序。(如下圖)
方法2:通過maven倉庫安裝導入。具體安裝教程詳見此網頁。
【示例1】添加Excel公式
import com.spire.xls.*;
public class InsertFormulas {
public?static void main(String[] args) {
//創(chuàng)建Workbook對象
Workbook workbook = new Workbook();
//獲取第一個工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//聲明兩個變量
int currentRow = 1;
String currentFormula =null;
//設置列寬
sheet.setColumnWidth(1, 32);
?sheet.setColumnWidth(2, 16);
//寫入用于測試的數(shù)據(jù)到單元格
sheet.getCellRange(currentRow,1).setValue("測試數(shù)據(jù):");
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);
//算數(shù)運算
currentFormula = "=1/2+3*4";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//日期函數(shù)
currentFormula = "=TODAY()";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
?sheet.getCellRange(currentRow,2).getStyle().setNumberFormat("YYYY/MM/DD");
//時間函數(shù)
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函數(shù)
currentFormula = "=IF(B1=5,\"Yes\",\"No\")";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//PI函數(shù)
currentFormula = "=PI()";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//三角函數(shù)
currentFormula = "=SIN(PI()/6)";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//計數(shù)函數(shù)
currentFormula = "=Count(B1:F1)";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//最大值函數(shù)
currentFormula = "=MAX(B1:F1)";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//平均值函數(shù)
currentFormula = "=AVERAGE(B1:F1)";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//求和函數(shù)
currentFormula = "=SUM(B1:F1)";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//保存文檔
workbook.saveToFile("output/InsertFormulas.xlsx",FileFormat.Version2013);
?}
}
公式添加效果:
【示例2】讀取Excel公式
import com.spire.xls.*;
public class ReadFormulas {
public?static void main(String[] args) {
//創(chuàng)建Workbook對象
Workbook workbook = new Workbook();
//加載Excel文檔
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\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());
??????????? }
? ? ? ? }
??? }
}
公式讀取效果:
(本文完)