C # で Excel フ ァ イ ル を 読 み 込 む 方法

.NET4 +を使うとC #でMicrosoft Excelファイルを読み込み,操作することができるようになります.また,前提条件として使用するPCにExcelをインストールしておく必要がります. (ExcelをインストールしたPCがない場合 は 、 こ ち ち らNPOIを ご確認く だ さ い。 * リ ン ク 先 英語)

最初 にBiblioteca de objetos do Microsoft Excel XX.Xの 参照 設定 を 行 い ま す。 参照 設定 の COM タ グ。 通常 Excel のAliasを 使用 し ま す。

using Excel = Microsoft.Office.Interop.Excel;       //Microsoft Excel 14 object in references-> COM tab

次 に 、 そ れ ぞ れ の COM オ ブ ジ ェ ク ト に 対 し て 参照 設定 を 行 い ま す。

//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"sandbox_test.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

こ の 段 階 で Excel の 読 み 込 み が 可能 に な り ま す。 ま た 、 Excel は 0 ベ ー ス で は な い 点 に 注意 し て く く だ さ い。 現 段 階 で は た 、 は は

//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
//new line
if (j == 1)
Console.Write("rn");

//write the value to the console
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
Console.Write(xlRange.Cells[i, j].Value2.ToString() + "t");

//add useful things here!
}
}

最後 に 、 参照 を を ア ン マ ネ ー ジ ド メ モ リ に 設定 す る 必要 が あ り ま ま す。 こ の プ ロ セ ス が な い と Excel フ ァ イ ル 自 体 に フ ァ イ す す す ま

//cleanup
GC
.Collect();
GC
.WaitForPendingFinalizers();

//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad

//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);

//close and release
xlWorkbook
.Close();
Marshal.ReleaseComObject(xlWorkbook);

//quit and release
xlApp
.Quit();
Marshal.ReleaseComObject(xlApp);

よ り 詳 し く 紹 介 さ れ て る る 記事
StackOverflow
Interop Marshaling
Excel e C #

Código completo サ ン プ ル:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel; //microsoft Excel 14 object in references-> COM tab
namespace Sandbox
{
public class Read_From_Excel
{
public static void getExcelFile()
{

//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:UsersE56626DesktopTeddyVS2012Sandboxsandbox_test - Copy - Copy.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;

//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
//new line
if (j == 1)
Console.Write("rn");

//write the value to the console
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
Console.Write(xlRange.Cells[i, j].Value2.ToString() + "t");
}
}

//cleanup
GC
.Collect();
GC
.WaitForPendingFinalizers();

//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad

//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);

//close and release
xlWorkbook
.Close();
Marshal.ReleaseComObject(xlWorkbook);

//quit and release
xlApp
.Quit();
Marshal.ReleaseComObject(xlApp);
}
}
}