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

    1.说明:创建数据库 CREATE DATABASE database-name .说明:删除数据库 drop database dbname .说明:备份sql server --- 创建 备份数据 ...

  2. Rewrite JSON project with Fetch

    上传 JSON 数据 使用fetch()来发布json编码的数据. var url = 'https://example.com/profile'; var data = {username: 'ex ...

  3. React Native搭建开发环境 之 --走过的坑

    React Native是使用JavaScript和React编写原生移动应用 我的开发平台是基于windows系统,所以只支持android,要是想开发ios系统,那就只能考虑使用沙盒环境 接下来就 ...

  4. UEditor之实现配置简单的图片上传示例

    UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...

  5. 玩转vue前进刷新,后退不刷新and按需刷新

    大白萝卜小课堂开讲了!带你玩转vue前进后退按需刷新! 用vue做后台管理项目,特别是有列表页.列表数据详情页.列表数据修改页功能的码友们,几乎都被vue前进后退都刷新的逻辑坑过,本萝卜更是! 萝卜的 ...

  6. ABP入门系列(2)——领域层创建实体

    ABP入门系列目录--学习Abp框架之实操演练 这一节我们主要和领域层打交道.首先我们要对ABP的体系结构以及从模板创建的解决方案进行一一对应.网上有代码生成器去简化我们这一步的任务,但是不建议初学者 ...

  7. HTML5调用手机摄像机、相册功能 <input>方法

    最近用MUI框架做webapp项目,在有PLUS环境的基础上能直接调用手机底层的API来使用拍照或从相册选择上传功能! 在查资料的时候,想起了另一种用input调用摄像和相册功能的方法,之前没有深入了 ...

  8. [Swift]LeetCode214. 最短回文串 | Shortest Palindrome

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. [Swift]LeetCode451. 根据字符出现频率排序 | Sort Characters By Frequency

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  10. [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...