1. 程式人生 > >C# 創建、讀取Excel公式

C# 創建、讀取Excel公式

區域 rom -o () 信息 cell intro 加載 https

對於數據量較大的表格,需要計算一些特殊數值時,我們通過運用公式能有效提高我們數據處理的速度和效率,對於後期數據的增刪改查等的批量操作也很方便。此外,對於某些數值的信息來源,我們也可以通過讀取數據中包含的公式來獲取。下面的示例中將分享通過C# 來創建、讀取Excel公式的方法。

工具使用

  • Spire.XLS for .NET 8.0
    下載安裝該類庫後,註意在程序中添加引用Spire.Xls.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)
    技術分享圖片
    代碼示例(供參考)

    【示例1】創建Excel公式

    C#

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公式

C#

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公式”的全部內容。
(本文完)

C# 創建、讀取Excel公式