您好,登錄后才能下訂單哦!
對于數據量較大的表格,需要計算一些特殊數值時,我們通過運用公式能有效提高我們數據處理的速度和效率,對于后期數據的增刪改查等的批量操作也很方便。此外,對于某些數值的信息來源,我們也可以通過讀取數據中包含的公式來獲取。下面的示例中將分享通過C# 來創建、讀取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");
}
}
}
公式創建結果(如下圖):
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公式”的全部內容。
(本文完)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。