当前位置:
首页 > Python基础教程 >
-
C#教程之C# 处理Excel公式(一)——创建、读取
对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便。此外,对于某些数值的信息来源,我们也可以通过读取数据中包含的公式来获取。下面的示例中将分享通过C# 来创建、读取Excel公式的方法。
工具使用
- Spire.XLS for .NET 8.0
下载安装该类库后,注意在程序中添加引用Spire.Xls.dll(dll文件可在安装路径下的Bin文件夹中获取)
代码示例(供参考)
【示例1】创建Excel公式
步骤 1 :新建工作簿
Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0];
步骤 2 : 添加测试数据及文本,并设置文本格式等
//初始化currentRow、currentFormula int 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;
步骤 3 :写入函数
//算术运算 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;
步骤 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[0]; //初始化currentRow、currentFormula int 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公式
步骤 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[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公式”的全部内容。
(本文完)
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式