excel 公式 java_Java读取、创建Excel的公式
码农公社 210.net.cn 210= 1024 10月24日一个重要的节日--码农(程序员)节
操作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());
}
}
}
}
excel 公式 java_Java读取、创建Excel的公式相关推荐
- excel导出 服务器运行失败,用vc生成excel报表,提示“创建excel服务器失败”,该如何处理...
当前位置:我的异常网» VC/MFC » 用vc生成excel报表,提示"创建excel服务器失败", 用vc生成excel报表,提示"创建excel服务器失败" ...
- r导入txt乱码_R学习笔记系列—R语言从文本和Excel文件中读取数据-excel打开是乱码...
1.4.1 从文本文件中导入数据 可以使用 read.table() 函数从带分隔符的文本文件中导入数.调用格式为: 这个函数看上去很复杂,但其实很多参数在使用时不用明确指出来,使用默认值可以满足绝大 ...
- excel教学 java_Java怎样处理EXCEL的读取
须要包:poi-3.5.jar.poi-ooxml-3.5.jar 实例: publicclassProcessExcel { privateWorkbook wb =null; privateShe ...
- python写入excel表格_Python读取写入Excel表格
原博文 2019-08-27 15:35 − Python操作Excel,主要用到xlrd和xlwt这两个库,即xlrd是读Excel,xlwt是写Excel的库 1.Python读取Excel表格 ...
- 导出excel文件 java_java实现导出excel文件
实现方法如下: (视频教程推荐:java课程) 1.首先新建一个SpringBoot项目 2.导入依赖–pom.xml<?xml version="1.0" encoding ...
- Php生成货运交接单excel表,PHPExcel读取文件excel模板文件导出数据信息
在使用PHPExcel导出数据的时候,我们会碰到有时候导出的excel的格式是比较复杂的,所以我们可以通过一个excel预先排版好的模板文件,用户PHPExcel读取该文件,填充内容就行了,这样处理非 ...
- java怎么读取excel思路_java读取本地excel数据
一 注解的定义 注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法.局部变量.方法参 ...
- asp.net 中 Interop.Excel.dll 无模版创建 excel 并下载到本地
// 调用此方法 需要添加引用 -> COM -> Microsoft Excel 12.0 Object Librarypublic void ExportExcel(){// exce ...
- php快速读取超大excel,PHP如何读取超大excel文件,划重点了
文讲的是php读取超大文件实现例子, 实现方法: 1. 直接采用file函数来操作 注: 由于 file函数是一次性将所有内容读入内存,而php为了防止一些写的比较糟糕的程序占用太多的内存而导致系统内 ...
最新文章
- 基于U-Net图像分割的划痕缺陷分割(课程设计)
- 支持向量机SVM模型中C和gamma参数分别是什么?对模型有什么影响?
- Ruby on Rails 学习笔记 - 2 Toy
- 浅入 spring ioc 单例容器
- Linux动态库应用
- ASP.NET MVC 个人学习笔记之 Controller传值
- excel函数中if android,在Android中阅读Excel
- 【2018.4.7】模拟赛之四-ssl2385 猜数【水题】
- HiveQL: 数据操作
- fullPage.js插件用法(转发)
- vscode调试时如何在launch.json中添加环境变量
- 电脑照片抠图软件哪个好?来看看这两个简单好用的工具
- matlab掷色子游戏,掷骰子游戏四种玩法_聚会游戏必备
- Java学习day07-认识类和对象(单元测试Test)
- CSS3 文字边框 -webkit-text-stroke 镂空的字体
- 【linux】什么是栈回溯
- 机器学习 (十三) 电商O2O优惠券使用预测-1
- Codeforces Round #614 (Div. 2) E. Xenon's Attack on the Gangs(DP记忆化搜索+思维)
- Vue实现省、市、县三级联动
- 地址池命令 思科理由_思科交换机DHCP功能和使用简介
热门文章
- C#——Windows银行储蓄管理系统
- Ensight 操作技巧
- PON、EPON、GPON的区别
- Redis高可用哨兵Sentinel
- 知云文献翻译打不开_科研星火 | 基础技能——翻译篇
- isbn书号查询php代码,php根据isbn书号查询amazon网站上的图书信息的示例_PHP教程
- mysql必知必会第5版pdf_sql必知必会
- cadence 17.2 入门学习3 allegro
- 如何利用UDP组播实现海康网络摄像机(IPC)的自动探测【源码】【监控】【录播】【NVR】【ONVIF】
- 洛谷p4230 连环病原体 题解