日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java添加、读取Excel公式

發布時間:2023/12/16 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java添加、读取Excel公式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

操作excel表格用公式來處理數據時,可通過創建公式來運算數據,或通過讀取公式來獲取數據信息來源。本文以通過Java代碼來演示在Excel中創建及讀取公式的方法。這里使用了Excel Java類庫(Free Spire.XLS for Java 免費版),在官網下載獲取文件包后,解壓,將lib文件夾下的jar文件導入Java程序;或者通過maven倉庫下載并導入。導入結果如下:

1. 創建公式

Workbook wb = new Workbook(); //獲取第一個工作表Worksheet sheet = wb.getWorksheets().get(0); //聲明兩個變量int currentRow = 1;String currentFormula = null; //設置列寬sheet.setColumnWidth(1, 32);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); //保存文檔wb.saveToFile("AddFormulas.xlsx",FileFormat.Version2013);wb.dispose();} }

公式創建結果:

2. 讀取公式

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

公式讀取結果:

愿與諸君共進步,大量的面試題及答案還有資深架構師錄制的視頻錄像:有Spring,MyBatis,Netty源碼分析,高并發、高性能、分布式、微服務架構的原理,JVM性能優化、分布式架構等這些成為架構師必備的知識體系,可以微信搜索539413949獲取,最后祝大家都能拿到自己心儀的offer

總結

以上是生活随笔為你收集整理的Java添加、读取Excel公式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。