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 ...
随机推荐
- poj 3601Tower of Hanoi
Tower of Hanoi Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 1895 Accepted: 646 De ...
- 以P2P网贷为例互联网金融产品如何利用大数据做风控?
以P2P网贷为例互联网金融产品如何利用大数据做风控? 销售环节 了解客户申请意愿和申请信息的真实性:适用于信贷员模式. 风控关键点 亲见申请人,亲见申请人证件,亲见申请人签字,亲见申请人单位. 审 ...
- dotnet 设计规范 · 结构体定义
X 不要给结构体默认构造函数 默认的C#编译器也不让开发者定义有默认构造的结构体 X 不要定义易变的属性 易变的属性指的是在调用属性返回值的时候返回的是新的实例,易变的属性会有很多的问题. ✓ 需要确 ...
- H3C 寻找邻居
- win10 uwp 发布旁加载自动更新
在很多企业使用的程序都是不能通过微软商店发布,原因很多,其中我之前的团队开发了很久的应用,结果发现没有用户能从微软应用商店下载所以我对应用商店没有好感.但是作为一个微软粉丝,怎么能不支持 UWP 开发 ...
- SpringSecurity 自定义用户 角色 资源权限控制
SpringSecurity 自定义用户 角色 资源权限控制 package com.joyen.learning.security; import java.sql.ResultSet; impor ...
- C# 转换类型和字符串
有时候我们需要互转类型和字符串,把字符串转类型.把类型转字符串. 如果是基础类型,可以使用 x.Parse 这个方法,很多基础类型都支持. 那么我们可以使用 TypeDescriptor string ...
- vue-cli常用插件集合
element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI ...
- vue-learning:2 - template - directive
指令 directive 在上一节我们知道,VUE的template模板通过VUE指令API实现与页面的视图层联系.所以本节将聚集在实现视图层交互的VUE指令系统directive的基础使用. 我们先 ...
- Team Foundation Server 2015使用教程【5】:默认团队checkin权限修改