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 ...
随机推荐
- 菜鸡谈OO 第一单元总结
“OOP永远是我的好朋友爸爸!” ——来自某无能狂怒的菜鸡 身处在OO的第一个摸鱼黄金周中的我,感觉到了巨大的满足感.如果写博客这种充满意义的事情可以代替我们亲爱的作业,那么我提议每周来两个:)下面开 ...
- json转义 使用 JavaScriptSerializer 时 需要添加的引用
当创建JavaScriptSerializer创建对象时,JavaScriptSerializer jss=new JavaScriptSerializer():时. 1. 需要添加的是Syste ...
- FluentDataflow - Fluent Style TPL Dataflow
我的新英文博客文章: FluentDataflow - Fluent Style TPL Dataflow 介绍了本人最新发布的一个开源类库:FluentDataflow--Fluent风格的TPL ...
- 从零开始学深度学习mxnet教程:安装以及基本操作
一.导言 本教程适合对人工智能有一定的了解的同学,特别是对实际使⽤深度学习感兴趣的⼤学⽣.⼯程师和研究⼈员.但本教程并不要求你有任何深度学习或者机器学习的背景知识,我们将从头开始解释每⼀个概念.虽然深 ...
- emWin酿造机过程演示,含uCOS-III和FreeRTOS两个版本
第2期:酿造机过程演示 配套例子:V6-902_STemWin提高篇实验_酿造机过程演示(uCOS-III)V6-903_STemWin提高篇实验_酿造机过程演示(FreeRTOS) 例程下载地址:h ...
- win7系统下dos界面无法自由调整大小
刚开始在win7系统,在dos界面下做MySQL的实验,很多数据不能显示界面上,只能显示固定的大小,以为这是系统的原因,后来在网上查找了一些资料.终于发现可以自由调节dos界面大小的方法.下面给出截图 ...
- Javascript高级编程学习笔记(90)—— Canvas(7) 绘制图像
绘制图像 2D绘图上下文内置了对图像的支持 如果希望将一幅图绘制到画布上,可以使用 drawImage() 的方法 该方法有三种不同的参数数组合以对应不同的应用场景 将<img>绘制到画布 ...
- [Swift]LeetCode726. 原子的数量 | Number of Atoms
Given a chemical formula (given as a string), return the count of each atom. An atomic element alway ...
- [Swift]LeetCode967. 连续差相同的数字 | Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
- 「造个轮子」——cicada 源码分析
前言 两天前写了文章<「造个轮子」--cicada(轻量级 WEB 框架)> 向大家介绍了 cicada 之后收到很多反馈,也有许多不错的建议. 同时在 GitHub 也收获了 80 几颗 ...