Java 添加、讀取Excel公式

? ? ? ?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());

??????????? }

? ? ? ? }

??? }

}

公式讀取效果:


(本文完)

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 使用首先需要了解他的工作原理 1.POI結構與常用類 (1)創(chuàng)建Workbook和Sheet (2)創(chuàng)建單元格 (...
    長城ol閱讀 8,553評論 2 25
  • 轉自鏈接 目錄 1.認識NPOI 2.使用NPOI生成xls文件 2.1創(chuàng)建基本內容 2.1.1創(chuàng)建Workboo...
    腿毛褲閱讀 10,703評論 1 3
  • 轉自鏈接 2.3.5 IF函數(shù) 2.3.6 CountIf和SumIf函數(shù) 2.3.7 Lookup函數(shù) 2.3....
    腿毛褲閱讀 12,959評論 0 0
  • 眾所周知,Excel是日常最常用的辦公軟件之一。它不僅僅能夠方便的處理表格和進行圖形分析,同時也能夠進行各種...
    Tina_Tang閱讀 444評論 0 1
  • VBA訂制工具欄 http://club.excelhome.net/thread-1047254-1-1.htm...
    大海一滴寫字的地方閱讀 2,274評論 0 0