2012年12月19日水曜日

C# で Excel のセルを読み書き


C# (Visual Studio 2012 express)で Excel ファイルのA1セルを読み書きする処理
参考にさせてもらったサイト
http://code.msdn.microsoft.com/office/C-Excel-Sheet-5037d251
using System;
using Microsoft.Office.Interop.Excel;
namespace COMAutomation_Excel_CS
{
class Program
{
static void Main(string[] args)
{
string fileName = @"C:\mdb\temp2.xlsx";
Application xlApp = new Application();
if (xlApp != null)
{
xlApp.Visible = true;
Workbook wb = xlApp.Workbooks.Open(Filename: fileName);
((Worksheet)wb.Sheets[1]).Select();
Range aRange = xlApp.get_Range("A1") as Range;
if (aRange != null)
{
Console.WriteLine(aRange.Value2 ?? String.Empty);
aRange.Value2 = "コード レシピ Excel編";
Console.WriteLine(aRange.Value2);
}
wb.Close(true);
xlApp.Quit();
}
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿