Java添加、读取Excel公式
操作excel表格用公式来处理数据时,可通过创建公式来运算数据,或通过读取公式来获取数据信息来源。本文以通过Java代码来演示在Excel中创建及读取公式的方法。这里使用了Excel Java类库(Free Spire.XLS for Java 免费版),在官网下载获取文件包后,解压,将lib文件夹下的jar文件导入Java程序;或者通过maven仓库下载并导入。导入结果如下:

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 poi读取excel公式,返回计算值(转)
http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi; 2 3 import o ...
- C# 处理Excel公式(一)——创建、读取Excel公式
对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便.此外,对于某些数值的信息来源,我们也可以通过读取数据中包含 ...
- postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库
最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...
- Java Struts2读取Excel 2003/2007/2010例子
Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版. Apache POI ...
- Java POI读取Excel数据,将数据写入到Excel表格
1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...
- java POI读取excel 2007/2003
2003版office excel读取 import java.io.FileNotFoundException; import java.io.IOException; import java.io ...
- Java中读取Excel功能实现_POI
这里使用apache的poi进行读取excel 1,新建javaproject 项目:TestExcel 2,导入包 包下载地址:http://poi.apache.org/download.html ...
- java poi 读取excel内容
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import or ...
- Java POI 读取Excel数据转换为XML格式
1.首先要下载poi相关的包:http://poi.apache.org/ ,以下是所需的jar包 2.贴上详细的代码 public class ExcelToXml { /** * 将excel的 ...
- java实现读取excel文件内容
package excel; import java.io.FileInputStream; import java.io.InputStream; import java.text.SimpleDa ...
随机推荐
- Mac MAMP 使用终端shell操作mysql数据库
在MAMP中已经集成了phpMyAdmin,可以很方便的管理mysql数据库,但是有的情况是phpMyAdmin不能做到的.比如,导入sql文件,当sql文件非常大(大于20MB)的时候,apache ...
- 2019-10-5-dotnet-core-获取-MacAddress-地址方法
title author date CreateTime categories dotnet core 获取 MacAddress 地址方法 lindexi 2019-10-05 10:44:10 + ...
- H3C CHAP验证配置示例一
- Python--day47--mysql索引种类
- Python--day40--datetime,socketserver,Threading模块分别是哪些模块的高层模块
- spring security自定义指南
序 本文主要研究一下几种自定义spring security的方式 主要方式 自定义UserDetailsService 自定义passwordEncoder 自定义filter 自定义Authent ...
- 用生活例子来形象了解TCP-IP协议
TCP/IP模型四层协议 与 邮件? 1.应用层——与用户直接打交道 类似 要寄的物件 2.传输层——处理和增加源数据并传输到IP层 类似 快递单信息 3.IP层——分配地址和传送数据 类似 分拣站分 ...
- 2018-8-10-win10-uwp-x_Bind-无法获得资源
title author date CreateTime categories win10 uwp x:Bind 无法获得资源 lindexi 2018-08-10 19:17:19 +0800 20 ...
- 一眼看懂promise async的区别
// promise方法 let p1 = new Promise((resolve,reject) => { setTimeout(() => { resolve('我是p1') },4 ...
- 为什么Redis是单线程,性能还如此高?
一. Redis为什么是单线程 注意:redis 单线程指的是网络请求模块使用了一个线程,即一个线程处理所有网络请求,其他模块仍用了多个线程. 因为CPU不是Redis的瓶颈.Redis的瓶颈最有可能 ...