操作excel表格用公式來處理數(shù)據(jù)時,可通過創(chuàng)建公式來運(yùn)算數(shù)據(jù),或通過讀取公式來獲取數(shù)據(jù)信息來源。這里使用了java類庫(Free Spire.XLS for Java 免費(fèi)版)獲取文件包后,解壓,將lib文件夾下的jar文件導(dǎo)入Java程序。
創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、謝通門網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為謝通門等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
如圖:
1、創(chuàng)建公式
import com.spire.xls.*; public class AddFormula { public static void main(String[] args) { //創(chuàng)建Workbook對象 Workbook wb = new Workbook(); //獲取第一個工作表 Worksheet sheet = wb.getWorksheets().get(0); //聲明兩個變量 int currentRow = 1; String currentFormula = null; //設(shè)置列寬 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("結(jié)果:"); //設(shè)置單元格格式 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ù)運(yùn)算 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); //保存文檔 wb.saveToFile("AddFormulas.xlsx",FileFormat.Version2013); wb.dispose(); } }
公式創(chuàng)建結(jié)果:
2、讀取公式
import com.spire.xls.*; public class ReadFormula { public static void main(String[] args) { //加載Excel文檔 Workbook wb = new Workbook(); wb.loadFromFile("AddFormulas.xlsx"); //獲取第一個工作表 Worksheet sheet = wb.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()); } } } }
公式讀取結(jié)果:
以上就是java實現(xiàn)在excel中創(chuàng)建及讀取公式的詳細(xì)內(nèi)容,更多請關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!