操作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快速入门

excel获取公式的值java,java实现在excel中创建及读取公式相关推荐

  1. java生成函数excel_java实现在excel中创建及读取公式

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

  2. 太实用了!在Java的Word 文档中插入或读取艺术字

    太实用了!在Java的Word 文档中插入或读取艺术字 简直不能太实用! 扫码关注<Java学研大本营>,加入读者群,分享更多精彩 艺术字是一组文本样式,允许您向文本添加设计元素,例如填充 ...

  3. Java:在网络路径中创建文件

    应用场景 使用Java语言在网络路径中创建文件. 实现 本地路径 首先在本地路径中创建文件的代码是这样的. import java.io.*; import java.text.CompactNumb ...

  4. 3分钟学会在 ASP.NET MVC 中创建、读取和编辑 Excel 电子表格

    在本文中,您将学习如何在ASP.NET MVC 应用程序中创建.读取和编辑 Excel 电子表格.为此,我们将创建一个由功能丰富的网格控件组成的电子表格应用程序,用于显示和编辑 Excel 文件,如下 ...

  5. 蓝桥杯-组合公式求值(java)

    算法提高 组合公式求值 时间限制:1.0s 内存限制:256.0MB问题描述给定n, m,求:输入格式输入一行,包含两个整数n, m.输出格式输出一行,包含求得的值,由于答案可能非常大,请输出此公式除 ...

  6. php获取python运行结果_“如何实现在PHP中调用Python并获取运行结果“

    如何实现在PHP中调用Python并获取运行结果 Python的import包件功能就跟PHP的include类似更确切的说应该更像是PHP中的require,因为Python里的import只要目就 ...

  7. Java学习笔记:Word中创建图表如此简单

    用法 Word中创建图表的方式是一样的. XWPFChart chart = WordHelpers.createChart(doc,500,300); XWPFChart和XSSFChart一样都是 ...

  8. python字典默认输出键还是值_说说在 Python 字典中如何在读取不存在的键时得到一个默认值...

    如果有方法能够在 Python 字典类型中,当读取不存在的键时能够得到一个默认值,那么代码就会变得更加直观.通过 defaultdict 类型可以实现这个目的1. 我们来改写一个 "输出单词 ...

  9. java 获取文本框值_在Java中从Excel获取文本框值

    这将有助于阅读您的Excel表格 HSSFWorkbook workbook = new HSSFWorkbook(fs); for (HSSFObjectData obj : workbook.ge ...

最新文章

  1. RS232与RS485的功能与区别!
  2. 我被面试官问到的问题-5
  3. jmeter启动报错 Error occurred during initialization of VM Could not reserve enough space for object heap
  4. 记录SpringBoot集成Shiro使用Redis作缓存遇到的一个问题(Key-Value)互换
  5. 7.多媒体☞图像图形拍照
  6. flink的web ui出现Server Response:Unable to load requested file /jars
  7. Flask/Django/Tornado语法对比(持续更新中)
  8. 散列表(哈希表)工作原理 (转)
  9. 行号 设置vim_在VSCode里面配置Vim正确姿势(细节解析)
  10. 在 Android 中调用二进制可执行程序(native executable )
  11. 系统封装 如何为原生PE集成软件
  12. mac vscode zsh git中文乱码解决方案
  13. android计算器(按钮版)
  14. winrar中文版去广告的方法,以winrar5.70简体中文版为例(其他版本也类似)
  15. 基于PLECS的Boost变换器仿真与分析
  16. 弧微分直角系最详细推导
  17. 基于负熵的快速不动点算法
  18. 破解WINDOWS7密码
  19. elastic search7 java开发简单教程
  20. [嵌入式开发模块]JY61姿态角度传感器 驱动模块

热门文章

  1. K_A03_005 基于多种单片机驱动TFT2.4(带触摸)显示图文 字符 简单时钟
  2. 河北环境工程学院计算机,河北环境工程学院信息工程系
  3. 3D Game Pr ramming Design(五):与游戏世界交互(对象池)
  4. 三菱(MITSUBISHI)CNC数据采集
  5. CRM系统需具有的九大功能
  6. 计算机电子表格三维簇状柱形图怎么设置,Excel中怎么制作柱形对比图【excel表格数据生成柱形图】...
  7. 听说INCA可以读手机GPS信号。你真的知道原理吗?
  8. 上交电院信息安全比计算机哪个好,上海交通大学-电子信息与电气工程学院-电子信息与电气工程学院...
  9. 用的Jlink但是却提示找不到Ulink的解决办法
  10. 需求管理手册-对需求描述的要求(8)