对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便。此外,对于某些数值的信息来源,我们也可以通过读取数据中包含的公式来获取。下面的示例中将分享通过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公式的更多相关文章

  1. c#.net对excel的操作——创建一个excel报表两个sheet就是2个表分别添加内容

    添加引用:Microsoft.Office.Interop.Excel //创建excel对象,就是实例化一个excel对象            Application excel=new Appl ...

  2. 10.ODBC创建/读取Excel QT4

    看到一篇MFC的参考链接:https://blog.csdn.net/u012319493/article/details/50561046 改用QT的函数即可 创建Excel //创建Excel v ...

  3. C# 操作Excel基础篇(读取Excel、写入Excel)

    注意事项:Excel的数据表中最多只能储存65535行数据,超出后,需要将数据分割开来进行储存.同时对于Excel中的乱码象限,是由于编码的错误方式导致引起的! 一.读取Excel数据表,获得Data ...

  4. postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库

    最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...

  5. c#读取excel到dataset

    public DataSet TransExcelToDataSet(string fileName, List<string> sheetNames) { OleDbConnection ...

  6. 读取Excel,通过Testng完成数据驱动

    背景 数据驱动是我们写自动化脚本非常常用的技术,而Testng中数据驱动常用的注解是 @DataProvider,但是这个方法必须返回一个Object[][].最近常有学生问起,如果通过外部文件作为数 ...

  7. sql2008读取excel

    环境:win7(64位)+sql2008 sql语句: --启用Ad Hoc Distributed Queries: reconfigure reconfigure --使用完成后,关闭Ad Hoc ...

  8. php读取excel日期类型数据的例子

    提供一个读取的函数:  代码如下 复制代码 //excel日期转换函数function excelTime($date, $time = false) { if(function_exists('Gr ...

  9. python读取excel,数字都是浮点型,日期格式是数字的解决办法

    excel文件内容: 读取excel: # coding=utf-8 import xlrd import sys reload(sys) sys.setdefaultencoding('utf-8' ...

  10. Android 读取excel 文件

    在面对选择国家地区,选择手机号码区号等信息的时候,常常我们是读取已存好的数据,我现在读取的就是excel里面的数据,所以在此记录下读取的方法以及注意点. 下面就是读取国际地区手机区号的数据效果图: e ...

随机推荐

  1. [LeetCode] Fibonacci Number 斐波那契数字

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  2. RSA算法加解密

    package org.thcic.ejw.util.encrypt; import java.io.ByteArrayOutputStream; import java.security.Key; ...

  3. eclipse上的maven,添加依赖后无法自动下载相应的jar包

    报错信息: Failed to read artifact descriptor for org.quartz-scheduler:quartz-jobs:jar:2.2.3  org.eclipse ...

  4. go语言指针理解

  5. flink入门实例-Windows下本地模式跑SocketWordCount

    一般情况下,开发大数据处理程序,我们希望能够在本地编写代码并调试通过,能够在本地进行数据测试,然后在生产环境去跑“大”数据. 一.nc工具 配置windows的nc端口,在网上下载nc.exe(htt ...

  6. 为不具有change事件的html标签设置监听事件

    change事件会在文本内容或选项被更改时触发. 该事件仅适用于<input type="text">和<textarea>以及<select> ...

  7. Winform 窗体获得焦点

    给窗体添加Shown事件 public void Form_Shown(object sender, EventArgs e) { this.Activate(); this.Focus(); //定 ...

  8. 不高兴的o( ̄ヘ ̄o#)JJ

    题目描述 津津上初中了.妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班.另外每周妈妈还会送她去学习朗诵.舞蹈和钢琴.但是津津如果一天上课超过八个小时就会不高兴,而 ...

  9. FCC(ES6写法) Symmetric Difference

    创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...

  10. toastr操作完成提示框

    toastr.js组件 关于信息提示框,项目中使用的是toastr.js这个组件,这个组件最大的好处就是异步.无阻塞,提示后可设置消失时间,并且可以将消息提示放到界面的各个地方. 官方文档以及源码 源 ...