微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# 处理Excel公式一——创建、读取Excel公式

对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便。此外,对于某些数值的信息来源,我们也可以通过读取数据中包含的公式来获取。下面的示例中将分享通过C# 来创建、读取Excel公式的方法

工具使用

下载安装该类库后,注意在程序中添加引用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 = string currentFormula = string.Empty;

设置1、2列列宽
sheet.SetColumnWidth(1,20);
sheet.SetColumnWidth(2,1)">12);

写入测试数据
sheet.Range[currentColumn,1)">1].Value = "测试数据:";
sheet.Range[currentColumn,2].NumberValue = 103].NumberValue = ; 
sheet.Range[currentColumn,1)">4].NumberValue = 305].NumberValue = 406].NumberValue = 50;

写入文本并设置区域格式
currentRow += 2;
sheet.Range[currentRow,1)">公式; 
sheet.Range[currentRow,1)">2].Value = 结果;
CellRange range = sheet.Range[currentRow,currentRow,1)">];
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)">1].Text = currentFormula;
sheet.Range[currentRow,1)">2].Formula = currentFormula;

日期函数
currentFormula = =Today()2].Style.NumberFormat = YYYY/MM/DD时间函
currentFormula = =Now()H:MM AM/PMIF逻辑函数
currentFormula = =IF(B1=5,\"Yes\",\"No\")PI函数
currentFormula = =PI()三角函数
currentFormula = =SIN(PI()/6)计数函数
currentFormula = =Count(B1:F1)求最大值函数
currentFormula = =MAX(B1:F1)平均值函数
currentFormula = =AVERAGE(B1:F1)求和函数
currentFormula = =SUM(B1:F1)2].Formula = currentFormula;

步骤 4 :保存文档

workbook.SavetoFile(Excel公式.xlsx,FileFormat.Version2013);
System.Diagnostics.Process.Start(");

完成代码后,调试运行程序,生成文档:

全部代码

using Spire.Xls;

namespace CreateFormula
{
    class Program
    {
        static void Main([] args)
        {
            新建一个工作簿,获取第一张工作表
            Workbook workbook =  Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            初始化currentRow、currentFormula
            ;
            .Empty;

            设置1、2列列宽
            sheet.SetColumnWidth();
            sheet.SetColumnWidth();

            写入测试数据
            sheet.Range[currentColumn,1)">;
            sheet.Range[currentColumn,1)">; 
            sheet.Range[currentColumn,1)">;

            写入文本并设置区域格式
            currentRow += ;
            sheet.Range[currentRow,1)">; 
            sheet.Range[currentRow,1)">;
            CellRange range = sheet.Range[currentRow,1)">];
            range.Style.Font.IsBold = ;
            range.Style.KNownColor = ExcelColors.LightGreen1;
            range.Style.FillPattern = ExcelPatternType.solid;
            range.Style.Borders[BordersLineType.EdgeBottom].Linestyle = LinestyleType.Medium;

            算术运算
            currentFormula = ;
            sheet.Range[++currentRow,1)"> currentFormula;
            sheet.Range[currentRow,1)"> currentFormula;

            日期函数
            currentFormula = 时间函
            currentFormula = IF逻辑函数
            currentFormula = PI函数
            currentFormula = 三角函数
            currentFormula = 计数函数
            currentFormula = 求最大值函数
            currentFormula = 平均值函数
            currentFormula = 求和函数
            currentFormula = 保存文档并打开
            workbook.SavetoFile(stem.Diagnostics.Process.Start();
        }
    }
}
View Code

 

【示例2】读取Excel公式

步骤 1 :实例化Workbook类,加载测试文档

Workbook workbook =  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.Formula);
    }
}
Console.ReadLine();

公式读取结果:

全部代码

 Spire.Xls;
 System;

 ReadFormula
{
    实例化一个Workbook
            Workbook workbook =  Workbook();

            加载测试文档
            workbook.LoadFromFile(获取一个工作表
            Worksheet sheet = workbook.Worksheets[遍历[B1:B13]的单元格
            ])
            {
                判断是否含有公式
                 (cell.HasFormula)
                {
                    输出含有公式的单元格及公式
                     cell.Formula);
                }
            }
            Console.ReadLine();
        }
    }
}
View Code

 

以上是本次关于“C# 创建、读取Excel公式”的全部内容

(本文完)

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐