Excel数式ってエクセルで使える便利な機能のひとつ。掛け算・割り算・引き算・足し算などをすることができます。また、Excel関数には数値関数、文字列操作関数、日付と時刻関数、論理関数、セル検索関数、情報関数等様々な種類があります。この記事ではSpire.XLSを使用して、Excelでよく使う関数を追加する、またセルの数式を取得する方法を紹介します。
下準備
1.E-iceblueの公式サイトからFree Spire.XLS無料版をダウンロードしてください。
2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.XLS.dllを参照に追加してください。
(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→XLS.dll”というようです。)
using Spire.Xls;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
//Workbookを作成します。
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//currentRow、currentFormulaを初期化します。
int currentRow = 1;
string currentFormula = string.Empty;
//1、2列の幅をせってします。
sheet.SetColumnWidth(1, 32);
sheet.SetColumnWidth(2, 16);
//データを追加します。
sheet.Range[currentRow, 1].Value = "テストデータ:";
sheet.Range[currentRow, 2].NumberValue = 1;
sheet.Range[currentRow, 3].NumberValue = 2; ;
sheet.Range[currentRow, 4].NumberValue = 3;
sheet.Range[currentRow, 5].NumberValue = 4;
sheet.Range[currentRow, 6].NumberValue = 5;
//テキストを追加します。
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;
//COUNT関数
currentFormula = "=Count(B1:F1)";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//MAX関数
currentFormula = "=MAX(B1:F1)";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//AVERAGE関数
currentFormula = "=AVERAGE(B1:F1)";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//SUM関数
currentFormula = "=SUM(B1:F1)";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//保存します。
workbook.SaveToFile("Excel数式.xlsx", FileFormat.Version2013);
}
}
}
using Spire.Xls;
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//Workbook作成します。
Workbook workbook = new Workbook();
//Excelをロードします。
workbook.LoadFromFile("Excel数式.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);
}
}
}
}
}