欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 创建、读取Excel公式

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

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

工具使用

下载安装该类库后,注意在程序中添加引用spire.xls.dll(dll文件可在安装路径下的bin文件夹中获取)

C# 创建、读取Excel公式

代码示例(供参考)

【示例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");

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

C# 创建、读取Excel公式

全部代码:

C# 创建、读取Excel公式
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");
        }
    }
}
view code

 

【示例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();

公式读取结果:

C# 创建、读取Excel公式

全部代码:

C# 创建、读取Excel公式
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();
        }
    }
}
view code

 

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

(本文完)