C# 创建、读取Excel公式
对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便。此外,对于某些数值的信息来源,我们也可以通过读取数据中包含的公式来获取。下面的示例中将分享通过C# 来创建、读取Excel公式的方法。
工具使用
- Spire.XLS for .NET 8.0
下载安装该类库后,注意在程序中添加引用Spire.Xls.dll(dll文件可在安装路径下的Bin文件夹中获取)
代码示例(供参考)【示例1】创建Excel公式
C#
using Spire.Xls;namespace CreateFormula
{class Program{static void Main(string[] args){//新建一个工作簿,获取第一个工作表Workbook workbook = new Workbook();Worksheet sheet = workbook.Worksheets[0];//初始化currentRow、currentFormulaint currentColumn = 1;int currentRow = 1;string currentFormula = string.Empty;//设置1、2列列宽sheet.SetColumnWidth(1, 20);sheet.SetColumnWidth(2, 12);//写入测试数据sheet.Range[currentColumn, 1].Value = "测试数据:";sheet.Range[currentColumn, 2].NumberValue = 10;sheet.Range[currentColumn, 3].NumberValue = 20; sheet.Range[currentColumn, 4].NumberValue = 30;sheet.Range[currentColumn, 5].NumberValue = 40;sheet.Range[currentColumn, 6].NumberValue = 50;//写入文本并设置区域格式currentRow += 2;sheet.Range[currentRow, 1].Value = "公式"; sheet.Range[currentRow, 2].Value = "结果";CellRange range = sheet.Range[currentRow, 1, currentRow, 2];range.Style.Font.IsBold = true;range.Style.KnownColor = ExcelColors.LightGreen1;range.Style.FillPattern = ExcelPatternType.Solid;range.Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Medium;//算术运算currentFormula = "=1/2+3*4";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//日期函数currentFormula = "=Today()";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;sheet.Range[currentRow, 2].Style.NumberFormat = "YYYY/MM/DD";//时间函数currentFormula = "=NOW()";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;sheet.Range[currentRow, 2].Style.NumberFormat = "H:MM AM/PM";//IF逻辑函数currentFormula = "=IF(B1=5,\"Yes\",\"No\")";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//PI函数currentFormula = "=PI()";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//三角函数currentFormula = "=SIN(PI()/6)";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//计数函数currentFormula = "=Count(B1:F1)";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//求最大值函数currentFormula = "=MAX(B1:F1)";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//平均值函数currentFormula = "=AVERAGE(B1:F1)";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//求和函数currentFormula = "=SUM(B1:F1)";sheet.Range[++currentRow, 1].Text = currentFormula;sheet.Range[currentRow, 2].Formula = currentFormula;//保存文档并打开workbook.SaveToFile("Excel公式.xlsx", FileFormat.Version2013);System.Diagnostics.Process.Start("Excel公式.xlsx");}}
}
公式创建结果(如下图):
【示例2】读取Excel公式
C#
using Spire.Xls;
using System;namespace ReadFormula
{class Program{static void Main(string[] args){//实例化一个WorkbookWorkbook workbook = new Workbook();//加载测试文档workbook.LoadFromFile("test.xlsx");//获取第一个工作表Worksheet sheet = workbook.Worksheets[0];//遍历[B1:B13]的单元格foreach (var cell in sheet.Range["B1:B13"]){//判断是否含有公式if (cell.HasFormula){//输出含有公式的单元格及公式string certainCell = String.Format("Cell[{0},{1}]", cell.Row, cell.Column);Console.WriteLine(certainCell + " 含有公式: " + cell.Formula);}}Console.ReadLine();}}
}
公式读取结果(如下图)
以上是本次关于“C# 创建、读取Excel公式”的全部内容。
(本文完)
转载于:https://blog.51cto.com/eiceblue/2286140
C# 创建、读取Excel公式相关推荐
- java生成函数excel_如何通过Java代码实现创建和读取Excel公式
如何通过Java代码实现创建和读取Excel公式 发布时间:2020-06-06 21:58:24 来源:亿速云 阅读:250 作者:Leah 如何通过Java代码实现创建和读取Excel公式?相信大 ...
- java解析excel公式_Java 添加、读取Excel公式
Excel是办公室自动化中非常重要的一款软件,具有强大的数据分析和处理功能.其中,Excel公式(包括函数)起了非常重要的作用.因此,掌握处理公式的能力有利于提高对Excel的应用水平,进而提高工作效 ...
- (谷粒项目八)poi基础应用,读取excel公式、导出大Excel方案
什么是POI Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. HSSF - 提供读写Microso ...
- Python中,读取excel公式值,结果为None的解决办法
openpyxl库可以非常方便的处理xlsx文件,但是这个库修改过xlsx文件之后,如果再次用openpyxl打开该文件,会发现无法获取到公式的计算结果,即可以获取到公式,但是无法获取公式结果,返回一 ...
- java jxl读取xlsx_Java添加、读取Excel公式
操作excel表格用公式来处理数据时,可通过创建公式来运算数据,或通过读取公式来获取数据信息来源.本文以通过Java代码来演示在Excel中创建及读取公式的方法.这里使用了Excel Java类库(F ...
- python使用openpyxl库创建读取Excel表格
使用openpyxl库读取和写入Excel表格 1.导入openpyxl库 pip install openpyxl 2.创建xlsx文件 import datetime from openpyxl ...
- java excel添加公式_Java添加、读取Excel公式
import com.spire.xls.*;public classAddFormula {public static voidmain(String[] args) {//创建Workbook对象 ...
- python读取excel公式计算结果为none_python读取excel保存公式无法读取
原因: 以data_only=True或默认data_only=False打开会得到两种不同的结果,各自独立,即data_only=True状态下打开的,会发现公式结果为None(空值)或者一个计算好 ...
- java poi读取excel公式,返回计算值(转)
http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi; 2 3 import o ...
- java excel公式计算_java poi读取excel公式,返回计算值(转) | 学步园
http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi; 2 3 import org ...
最新文章
- 谷歌AI公布新项目:未来你的宠物可能真的会是“机器”狗!
- 在 Linux 中使用超级用户权限
- 浅谈runtime运行时机制
- 6行代码!用Python将PDF转为word
- gc频繁的暂停启动_减少主要GC暂停的频率
- UIAutomator输入中文
- 硬派SUV坦克500撞色款预计年底上市
- python 卷积神经网络 应用_卷积神经网络在目标定位中的应用
- Windows 相关镜像及补丁下载地址
- Hadoop fs shell命令汇总
- hive和hadoop版本对应关系
- C#多线程和线程池 【转】
- 自定义 SpringBoot Banner 图案
- 明星里程被盗?看区块链如何加强个人信息安全保护
- Boot Loader 的设计与实现
- java重置按钮功能函数_Bootstrap按钮功能之查询按钮和重置按钮
- 面试|C# .net 面试题
- “应用程序配置不正确”解决方案
- 多平台容器镜像构建就看这一篇
- SCS【8】单细胞转录组之筛选标记基因 (Monocle 3)
热门文章
- 【Sarah】第一周
- php无框架开发web项目,web.framework
- Python基础--02
- 郑州超级计算机叫什么,国家超级计算郑州中心通过验收 2020超级计算机行业发展策略...
- 磁珠 符号_如何理解电子元件磁珠?
- autoflowchart软件使用步骤_【AutoFlowChart流程图自动生成软件】|C语言流程图生成工具(AutoFlowchart)下载_v1.0 中文版_9号软件下载...
- AndroidStudio修改布局文件运行无效
- python怎么理解函数的参数_Python中函数参数理解
- configure: error: gperf is needed
- MacOS新版本,不能装老版本Xcode