操作excel表格用公式来处理数据时,可通过创建公式来运算数据,或通过读取公式来获取数据信息来源。这里使用了java类库(Free Spire.XLS for Java 免费版)获取文件包后,解压,将lib文件夹下的jar文件导入Java程序。

如图:

(免费视频教程分享:java视频教程)

1、创建公式import com.spire.xls.*;

public class AddFormula {

public static void main(String[] args) {

//创建Workbook对象

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、读取公式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());

}

}

}

}

公式读取结果:

相关文章教程推荐:java快速入门

java生成函数excel_java实现在excel中创建及读取公式相关推荐

  1. excel获取公式的值java,java实现在excel中创建及读取公式

    操作excel表格用公式来处理数据时,可通过创建公式来运算数据,或通过读取公式来获取数据信息来源.这里使用了java类库(Free Spire.XLS for Java 免费版)获取文件包后,解压,将 ...

  2. java 饼图 框架_Java 在 Excel 中创建饼图/环形图

    import com.spire.xls.*;importcom.spire.xls.charts.ChartSerie;importcom.spire.xls.charts.ChartSeries; ...

  3. 随机数写excel java代码_基于python实现在excel中读取与生成随机数写入excel中

    搜索热词 具体要求是:在一份已知的excel表格中读取学生的学号与姓名,再将这些数据放到新的excel表中的第一列与第二列,最后再生成随机数作为学生的考试成绩. 首先要用到的数据库有:xlwt,xlr ...

  4. java 下拉列表 可折叠 qq分组_Java在Excel中创建多级分组、折叠或展开分组的实现...

    本文介绍通过Java程序在Excel创建分组的方法,可对行或列分组进行分组并设置明细数据是否展开或折叠.设置数据分组并展开或折叠时,可通过以下方法: 方法一: 通过方法sheet.groupByRow ...

  5. java 下拉列表 可折叠 qq分组_Java 在Excel中创建多级分组、折叠或展开分组

    以下经验内容分享通过Java程序在Excel中创建多级分组.折叠或展开分组. 可对行或列分组进行分组并设置明细数据是否展开或折叠.设置数据分组并展开或折叠时,可通过以下方法: 方法一: 通过方法she ...

  6. java excel 展开折叠_Java在Excel中创建多级分组、折叠或展开分组的实现

    本文介绍通过java程序在excel创建分组的方法,可对行或列分组进行分组并设置明细数据是否展开或折叠.设置数据分组并展开或折叠时,可通过以下方法: 方法一: 通过方法sheet.groupbyrow ...

  7. excel箱形图中位数_在Excel中创建简单的箱形图

    excel箱形图中位数 A box plot (box and whisker chart) lets you show how numbers are distributed in a set of ...

  8. iframe视频代码_在Excel中创建IFRAME代码

    iframe视频代码 If you want to embed a YouTube video on your website, YouTube will create some IFRAME cod ...

  9. 在Excel中创建汇总

    It's easy to create a running total in Excel, and show the accumulated amount in each row. You can u ...

最新文章

  1. 斩获 12w+ 星标的神仙项目再度上榜,简直就是一套活生生的自学编程百科全书!...
  2. golang协程退出
  3. apt get 安装mysql5.7_ubuntu18.04中安装mysql(5.7)步骤详细介绍
  4. 有时候,爱就是那么简单
  5. 字符流写数据的5种方式
  6. 使用Spring开发Java RESTful Web服务的7个理由
  7. python图形模块_使用Python图形模块:有没有办法将当前窗口保存为图像?
  8. LeetCode 1897. 重新分配字符使所有字符串都相等
  9. 剑指offer面试题32 - II. 从上到下打印二叉树 II(二叉树)(BFS)
  10. OpenCV:框选某一特定区域并保存到图片
  11. windows脚本bat批处理 关闭显示器 关闭显示器脚本bat
  12. 项目记事【SpringMVC-1】:后台接收前端传来的JSON,并转成对象
  13. php5.0手册,tp5.0 开发手册下载|
  14. java mp3剪切_mp3剪切器如何剪切mp3音频文件
  15. CTC blank 理解
  16. 网络狂飙2(netspeeder2) v2.3 简体中文版 怎么用
  17. 【Ci24R1小尺寸 DFN8/2*2】2.4G双向系统超低成本之选
  18. python 微信公众号发文章_如何使用 Python 爬取微信公众号文章
  19. php增加横排文字的间距,html段落内文字设置字间距间隔
  20. 微型计算机的ALU部件包括在( )之中,微机原理第二章复习题(附答案)期末考试题...

热门文章

  1. 编写Makefile中遇到的各种奇葩问题汇总
  2. 虚幻引擎C++编程游戏开发基础
  3. 如何对 Rocksdb以及类似存储引擎社区 提出 有效的性能问题?
  4. 深入理解ceph-disk activate 源码逻辑
  5. window.open和window.location.href的几种用法
  6. Linux如何查看当前目录下文件的个数
  7. SAP的安装后基本设定
  8. ulimit -SHn 65535 含义
  9. 把Doc文档转换成rtf格式
  10. libevent介绍