Excel ワークシートは多くの機能を備えた表計算であり、その中でも数式と関数は間違いなく最も重要な機能の一つである。数式と関数を使用することで、ユーザーはデータに対して幅広い計算、統計、論理演算を実行することができ、意味のある分析結論を迅速かつ正確に導き出すことができます。この記事では、Spire.XLS for .NET を使用して Excel ワークシートに数式と関数を追加する方法を説明します。 Spire.XLS for.NET をインストールします まず、Spire.XLS for.NET パッケージに含まれている DLL ファイルを.NETプロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。 PM> Install-Package Spire.XLS Excel ワークシートに数式と関数を挿入する Spire.XLS for .NET が提供する Worksheet.Range[int row, int column].Formula プロパティを使用すると、Excel ワークシートの指定したセルに数式や関数を追加することができます。 Excel のワークシートに数式や関数を追加する主な手順は以下の通りです: Workbook のオブジェクトを作成します。 Workbook.Worksheets[int index] プロパティを使って、指定したワークシートを取得します。 セルにデータを書き込み、セルの書式を設定します。 Range[int row, int column].Formula プロパティを使って、ワークシートの指定されたセルに数式と関数を追加します。 Workbook.SaveToFile() メソッドを使用してワークブックを保存します。 C# VB.NET using Spire.Xls; using System; namespace InsertFormulasAndFunctions { internal class Program { static void Main(string[] args) { //Workbookのオブジェクトを作成する Workbook workbook = new Workbook(); //最初のワークシートを取得する Worksheet worksheet = workbook.Worksheets[0]; //currentRowとcurrentFormulaの2つの変数を宣言する int currentRow = 1; String currentFormula = null; //列の幅を設定する worksheet.SetColumnWidth(1, 32); worksheet.SetColumnWidth(2, 16); //セルにコンテンツを書き込む worksheet.Range[currentRow, 1].Value = "試験データ"; worksheet.Range[currentRow, 2].NumberValue = 1; worksheet.Range[currentRow, 3].NumberValue = 2; worksheet.Range[currentRow, 4].NumberValue = 3; worksheet.Range[currentRow, 5].NumberValue = 4; worksheet.Range[currentRow, 6].NumberValue = 5; currentRow += 2; worksheet.Range[currentRow, 1].Value = "数式または関数"; worksheet.Range[currentRow, 2].Value = "結果"; //セルの書式を設定する CellRange range = worksheet.Range[currentRow, 1, currentRow, 2]; range.Style.Font.FontName = "Yu Gothic UI"; range.Style.KnownColor = ExcelColors.LightGreen; range.Style.FillPattern = ExcelPatternType.Solid; range.Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Medium; range.Style.Font.IsBold = true; //算術計算 currentFormula = "=1/2+3*4"; worksheet.Range[++currentRow, 1].Text = "'" + currentFormula; worksheet.Range[++currentRow, 2].Formula = currentFormula; //日付関数 currentFormula = "=TODAY()"; worksheet.Range[++currentRow, 1].Text = "'" + currentFormula; worksheet.Range[currentRow, 2].Formula = currentFormula; worksheet.Range[currentRow, 2].Style.NumberFormat = "YYYY/MM/DD"; //三角関数 currentFormula = "=SIN(PI()/6)"; worksheet.Range[++currentRow, 1].Text = "'" + currentFormula; worksheet.Range[currentRow, 2].Formula = currentFormula; //平均関数 currentFormula = "=AVERAGE(B1:F1)"; worksheet.Range[++currentRow, 1].Text = "'" + currentFormula; worksheet.Range[currentRow, 2].Formula = currentFormula; //合計関数 currentFormula = "=SUM(B1:F1)"; worksheet.Range[++currentRow, 1].Text = "'" + currentFormula; worksheet.Range[currentRow, 2].Formula = currentFormula; //ワークブックを保存する workbook.SaveToFile("数式と関数の追加.xlsx", FileFormat.Version2013); } } } Imports Spire.Xls Imports System Namespace InsertFormulasAndFunctions Friend Class Program Shared Sub Main(ByVal args() As String) 'Workbookのオブジェクトを作成する Dim workbook As Workbook = New Workbook() '最初のワークシートを取得する Dim worksheet As Worksheet = workbook.Worksheets(0) 'currentRowとcurrentFormulaの2つの変数を宣言する Dim currentRow As Integer = 1 Dim currentFormula As String = Nothing '列の幅を設定する worksheet.SetColumnWidth(1, 32) worksheet.SetColumnWidth(2, 16) 'セルにコンテンツを書き込む worksheet.Range(currentRow, 1).Value = "試験データ" worksheet.Range(currentRow, 2).NumberValue = 1 worksheet.Range(currentRow, 3).NumberValue = 2 worksheet.Range(currentRow, 4).NumberValue = 3 worksheet.Range(currentRow, 5).NumberValue = 4 worksheet.Range(currentRow, 6).NumberValue = 5 currentRow += 2 worksheet.Range(currentRow, 1).Value = "数式または関数" worksheet.Range(currentRow, 2).Value = "結果" 'セルの書式を設定する Dim range As CellRange = worksheet.Range(currentRow, 1, currentRow, 2) range.Style.Font.FontName = "Yu Gothic UI" range.Style.KnownColor = ExcelColors.LightGreen range.Style.FillPattern = ExcelPatternType.Solid range.Style.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Medium range.Style.Font.IsBold = True '算術計算 currentFormula = "=1/2+3*4""'" + currentFormulacurrentFormula '日付関数 currentFormula = "=TODAY()""'" + currentFormula worksheet.Range(currentRow, 2).Formula = currentFormula worksheet.Range(currentRow, 2).Style.NumberFormat = "YYYY/MM/DD" '三角関数 currentFormula = "=SIN(PI()/6)""'" + currentFormula worksheet.Range(currentRow, 2).Formula = currentFormula '平均関数 currentFormula = "=AVERAGE(B1:F1)""'" + currentFormula worksheet.Range(currentRow, 2).Formula = currentFormula '合計関数 currentFormula = "=SUM(B1:F1)""'" + currentFormula worksheet.Range(currentRow, 2).Formula = currentFormula 'ワークブックを保存する workbook.SaveToFile("数式と関数の追加.xlsx", FileFormat.Version2013) End Sub End Class End Namespace 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。