C# 创建Excel公式

 时间:2026-02-14 09:25:31

1、可通过E-iceblue或Nuget官网下载安装Spire.XLS for .NET后,注意在项目程序中添加引用Spire.Xls.dll(dll文件可在安装路径下的Bin文件夹中获取)。

C# 创建Excel公式

1、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、完成代码后,调试运行程序,生成文件(如下图)。

C# 创建Excel公式

  • FineReport制作html 报表:超级链接参数传递
  • C#/VB.NET实现拖拽添加文件/文件夹并获得路径
  • 怎样使用VBA筛选高于平均值之数据?
  • 怎样使用VBA批量修改数据标签?
  • Excel超链接第五讲利用hyperlink函数超链接网页
  • 热门搜索
    酱肉怎么做 镜怎么玩 烤麸怎么做好吃 百分之几怎么算 五角星怎么画 pants怎么读 怎么取消电脑开机密码 白衣天使怎么画 分数除法怎么算 樱桃吃多了会怎么样