本篇內(nèi)容主要講解“Java怎么添加和讀取Excel公式”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Java怎么添加和讀取Excel公式”吧!
創(chuàng)新互聯(lián)專注于景德鎮(zhèn)企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城網(wǎng)站建設(shè)。景德鎮(zhèn)網(wǎng)站建設(shè)公司,為景德鎮(zhèn)等地區(qū)提供建站服務(wù)。全流程按需定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)操作excel表格用公式來處理數(shù)據(jù)時(shí),可通過創(chuàng)建公式來運(yùn)算數(shù)據(jù),或通過讀取公式來獲取數(shù)據(jù)信息來源。本文以通過Java代碼來演示在Excel中創(chuàng)建及讀取公式的方法。這里使用了Excel Java類庫(Free Spire.XLS for Java 免費(fèi)版),在官網(wǎng)下載獲取文件包后,解壓,將lib文件夾下的jar文件導(dǎo)入Java程序;或者通過maven倉庫下載并導(dǎo)入。導(dǎo)入結(jié)果如下:
Java 代碼示例
1. 創(chuàng)建公式
import com.spire.xls.*; public class AddFormula { public static void main(String[] args) { //創(chuàng)建Workbook對(duì)象 Workbook wb = new Workbook(); //獲取第一個(gè)工作表 Worksheet sheet = wb.getWorksheets().get(0); //聲明兩個(gè)變量 int currentRow = 1; String currentFormula = null; //設(shè)置列寬 sheet.setColumnWidth(1, 32); sheet.setColumnWidth(2, 16); //寫入用于測(cè)試的數(shù)據(jù)到單元格 sheet.getCellRange(currentRow,1).setValue("測(cè)試數(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í)間函數(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); //計(jì)數(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"); //獲取第一個(gè)工作表 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é)果:
到此,相信大家對(duì)“Java怎么添加和讀取Excel公式”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!