C# 处理Excel公式(一)——创建、读取Excel公式
对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便。此外,对于某些数值的信息来源,我们也可以通过读取数据中包含的公式来获取。下面的示例中将分享通过C# 来创建、读取Excel公式的方法。
工具使用
下载安装该类库后,注意在程序中添加引用Spire.Xls.dll(dll文件可在安装路径下的Bin文件夹中获取)
代码示例(供参考)
【示例1】创建Excel公式
步骤 1 :新建工作簿
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[];
步骤 2 : 添加测试数据及文本,并设置文本格式等
//初始化currentRow、currentFormula
int currentColumn = ;
int currentRow = ;
string currentFormula = string.Empty; //设置1、2列列宽
sheet.SetColumnWidth(, );
sheet.SetColumnWidth(, ); //写入测试数据
sheet.Range[currentColumn, ].Value = "测试数据:";
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ; //写入文本并设置区域格式
currentRow += ;
sheet.Range[currentRow, ].Value = "公式";
sheet.Range[currentRow, ].Value = "结果";
CellRange range = sheet.Range[currentRow, , currentRow, ];
range.Style.Font.IsBold = true;
range.Style.KnownColor = ExcelColors.LightGreen1;
range.Style.FillPattern = ExcelPatternType.Solid;
range.Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Medium;
步骤 3 :写入函数
//算术运算
currentFormula = "=1/2+3*4";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //日期函数
currentFormula = "=Today()";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula;
sheet.Range[currentRow, ].Style.NumberFormat = "YYYY/MM/DD"; //时间函数
currentFormula = "=NOW()";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula;
sheet.Range[currentRow, ].Style.NumberFormat = "H:MM AM/PM"; //IF逻辑函数
currentFormula = "=IF(B1=5,\"Yes\",\"No\")";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //PI函数
currentFormula = "=PI()";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //三角函数
currentFormula = "=SIN(PI()/6)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //计数函数
currentFormula = "=Count(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //求最大值函数
currentFormula = "=MAX(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //平均值函数
currentFormula = "=AVERAGE(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //求和函数
currentFormula = "=SUM(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula;
步骤 4 :保存文档
workbook.SaveToFile("Excel公式.xlsx", FileFormat.Version2013);
System.Diagnostics.Process.Start("Excel公式.xlsx");
完成代码后,调试运行程序,生成文档:
全部代码:
using Spire.Xls; namespace CreateFormula
{
class Program
{
static void Main(string[] args)
{
//新建一个工作簿,获取第一张工作表
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[]; //初始化currentRow、currentFormula
int currentColumn = ;
int currentRow = ;
string currentFormula = string.Empty; //设置1、2列列宽
sheet.SetColumnWidth(, );
sheet.SetColumnWidth(, ); //写入测试数据
sheet.Range[currentColumn, ].Value = "测试数据:";
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ;
sheet.Range[currentColumn, ].NumberValue = ; //写入文本并设置区域格式
currentRow += ;
sheet.Range[currentRow, ].Value = "公式";
sheet.Range[currentRow, ].Value = "结果";
CellRange range = sheet.Range[currentRow, , currentRow, ];
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, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //日期函数
currentFormula = "=Today()";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula;
sheet.Range[currentRow, ].Style.NumberFormat = "YYYY/MM/DD"; //时间函数
currentFormula = "=NOW()";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula;
sheet.Range[currentRow, ].Style.NumberFormat = "H:MM AM/PM"; //IF逻辑函数
currentFormula = "=IF(B1=5,\"Yes\",\"No\")";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //PI函数
currentFormula = "=PI()";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //三角函数
currentFormula = "=SIN(PI()/6)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //计数函数
currentFormula = "=Count(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //求最大值函数
currentFormula = "=MAX(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //平均值函数
currentFormula = "=AVERAGE(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //求和函数
currentFormula = "=SUM(B1:F1)";
sheet.Range[++currentRow, ].Text = currentFormula;
sheet.Range[currentRow, ].Formula = currentFormula; //保存文档并打开
workbook.SaveToFile("Excel公式.xlsx", FileFormat.Version2013);
System.Diagnostics.Process.Start("Excel公式.xlsx");
}
}
}
【示例2】读取Excel公式
步骤 1 :实例化Workbook类,加载测试文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
步骤 2 :获取工作表
Worksheet sheet = workbook.Worksheets[0];
步骤 3:读取公式
//遍历[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();
公式读取结果:
全部代码:
using Spire.Xls;
using System; namespace ReadFormula
{
class Program
{
static void Main(string[] args)
{
//实例化一个Workbook
Workbook workbook = new Workbook(); //加载测试文档
workbook.LoadFromFile("test.xlsx"); //获取第一个工作表
Worksheet sheet = workbook.Worksheets[]; //遍历[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公式”的全部内容。
(本文完)
C# 处理Excel公式(一)——创建、读取Excel公式的更多相关文章
- c#.net对excel的操作——创建一个excel报表两个sheet就是2个表分别添加内容
添加引用:Microsoft.Office.Interop.Excel //创建excel对象,就是实例化一个excel对象 Application excel=new Appl ...
- 10.ODBC创建/读取Excel QT4
看到一篇MFC的参考链接:https://blog.csdn.net/u012319493/article/details/50561046 改用QT的函数即可 创建Excel //创建Excel v ...
- C# 操作Excel基础篇(读取Excel、写入Excel)
注意事项:Excel的数据表中最多只能储存65535行数据,超出后,需要将数据分割开来进行储存.同时对于Excel中的乱码象限,是由于编码的错误方式导致引起的! 一.读取Excel数据表,获得Data ...
- postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库
最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...
- c#读取excel到dataset
public DataSet TransExcelToDataSet(string fileName, List<string> sheetNames) { OleDbConnection ...
- 读取Excel,通过Testng完成数据驱动
背景 数据驱动是我们写自动化脚本非常常用的技术,而Testng中数据驱动常用的注解是 @DataProvider,但是这个方法必须返回一个Object[][].最近常有学生问起,如果通过外部文件作为数 ...
- sql2008读取excel
环境:win7(64位)+sql2008 sql语句: --启用Ad Hoc Distributed Queries: reconfigure reconfigure --使用完成后,关闭Ad Hoc ...
- php读取excel日期类型数据的例子
提供一个读取的函数: 代码如下 复制代码 //excel日期转换函数function excelTime($date, $time = false) { if(function_exists('Gr ...
- python读取excel,数字都是浮点型,日期格式是数字的解决办法
excel文件内容: 读取excel: # coding=utf-8 import xlrd import sys reload(sys) sys.setdefaultencoding('utf-8' ...
- Android 读取excel 文件
在面对选择国家地区,选择手机号码区号等信息的时候,常常我们是读取已存好的数据,我现在读取的就是excel里面的数据,所以在此记录下读取的方法以及注意点. 下面就是读取国际地区手机区号的数据效果图: e ...
随机推荐
- Spring源码学习-容器BeanFactory(五) Bean的创建-探寻Bean的新生之路
写在前面 上面四篇文章讲了Spring是如何将配置文件一步一步转化为BeanDefinition的整个流程,下面就到了正式创建Bean对象实例的环节了,我们一起继续学习吧. 2.初始化Bean对象实例 ...
- LIS的优化算法O(n log n)
LIS的nlogn的优化:LIS的优化说白了其实是贪心算法,比如说让你求一个最长上升子序列把,一起走一遍. 比如说(4, 2, 3, 1, 2,3,5)这个序列,求他的最长上升子序列,那么来看,如果求 ...
- c++实验二
1.函数重载编程练习编写重载函数add(),实现对int型,double型,Complex型数据的加法 #include<iostream> using namespace std; st ...
- 教你 Debug 的正确姿势——记一次 CoreMotion 的 Crash
作者:林蓝东 最近的一个手机 QQ 版本发出去后收到比较多关于 CoreMotion 的 crash 上报,案发现场如下: 但是看看这个堆栈发现它完全不按照套路出牌啊! 乍一看是挂在 CoreMoti ...
- RabbitMQ消息队列系列教程(二)Windows下安装和部署RabbitMQ
摘要 本篇经验将和大家介绍Windows下安装和部署RabbitMQ消息队列服务器,希望对大家的工作和学习有所帮助! 目录 一.Erlang语言环境的搭建 二.RabbitMQ服务环境的搭建 三.Ra ...
- 用R处理一组数据的三种方式
USArrests是R附带的一个数据集,现在我们需要创建一个factor向量urbancat,如果UrbanPop列的某个值在中位数之上,就把urbancat对应位置的值设为1,否则设为0. 这种数据 ...
- Java堆和栈的区别和介绍,JVM的堆和栈
一.Java的堆内存和栈内存 Java把内存划分成两种:一种是堆内存,一种是栈内存. 堆:主要用于存储实例化的对象,数组.由JVM动态分配内存空间.一个JVM只有一个堆内存,线程是可以共享数据的. ...
- #Java学习之路——基础阶段(第七篇)
我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...
- the python challenge闯关记录(9-16)
9 第九关 是一张图,上面有很多的黑点,查看网页源代码发现了上一关的提示: 还发现了一大串的数字 感觉又是一个使用PIL库进行图像处理的题,百度后知道要将这些点连接起来并重新画图.但是不能在原始图上修 ...
- VSCode与Deepin资源管理器冲突
解决方式: xdg-mime default dde-file-manager.desktop inode/directory 此外,网上有较多推荐(在deepin 15.8版本上测试无效): gvf ...