Created
August 1, 2013 05:35
-
-
Save ishisaka/6128638 to your computer and use it in GitHub Desktop.
ClosedXMLを使った、Excel操作の例
ClosedXML http://closedxml.codeplex.com/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using ClosedXML.Excel; | |
/* | |
ClosedXMLを利用して、セルの値を取得するサンプル | |
*/ | |
namespace ClosedXMLSample2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) { | |
string filePath = args[0]; //ファイルパス | |
string sheetName = args[1]; //データを読み込むセルがあるワークシートの名前 | |
string addressName = args[2]; //ワークシートのセル位置 | |
using (var wb = new XLWorkbook(filePath)) { | |
var ws = wb.Worksheets.First(s => s.Name == sheetName); | |
string format; | |
//セルの型がわかるので処理がしやすい | |
switch (ws.Cell(addressName).DataType) { | |
case XLCellValues.Text: | |
Console.WriteLine("Excel Data Type is Text."); | |
Console.WriteLine(ws.Cell(addressName).Value); | |
break; | |
case XLCellValues.Number: | |
Console.WriteLine("Excel Data Type is Number."); | |
format = ws.Cell(addressName).Style.NumberFormat.Format; | |
Console.WriteLine("Format: " + format); | |
Console.WriteLine(ws.Cell(addressName).Value); | |
break; | |
case XLCellValues.Boolean: | |
Console.WriteLine("Excel Data Type is Boolean."); | |
Console.WriteLine(ws.Cell(addressName).Value); | |
break; | |
case XLCellValues.DateTime: | |
Console.WriteLine("Excel Data Type is DateTime."); | |
format = ws.Cell(addressName).Style.NumberFormat.Format; | |
Console.WriteLine("Format: " + format); | |
//セルのバリューはObject型なので、必要に応じてキャストしてやる | |
Console.WriteLine(((DateTime)ws.Cell(addressName).Value).ToString()); | |
break; | |
case XLCellValues.TimeSpan: | |
Console.WriteLine("Excel Data Type is DateTime"); | |
format = ws.Cell(addressName).Style.NumberFormat.Format; | |
Console.WriteLine("Format: " + format); | |
Console.WriteLine(((TimeSpan)ws.Cell(addressName).Value).ToString()); | |
break; | |
default: | |
break; | |
} | |
} | |
Console.Read(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ClosedXML.Excel; | |
//ClosedXMLを使ったサンプル | |
//ClosedXMLを使えばたった4行でExcelのファイルが作れる。 | |
//参照設定: | |
//ClosedXMLをNuGetで追加した後、VSの参照追加でWindowsBaseへの参照も追加 | |
namespace ClosedXMLSample1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) { | |
//WorkBookオブジェクトを追加 | |
using (var wb = new XLWorkbook()) { | |
//WorkSheetをWorkBookオブジェクトに追加 | |
var ws = wb.Worksheets.Add("Sample"); | |
//セルの値を変更 | |
ws.Cell("A1").Value = "Hello World"; | |
//作業内容を保存 | |
wb.SaveAs("HelloWorld.xlsx"); | |
//直感的ですな | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment