2012年12月15日土曜日

C# でExcelファイルに書き込む


C# でExcelファイルに書き込むサンプルです。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace cs_excel_write2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// string ExcelBookFileName = textBox3.Text;
// string ExcelBookFileName = "c:\\mdb\\temp2.xlsx";
// Microsoft.Office.Interop.Excel.Application ExcelApp
// = new Microsoft.Office.Interop.Excel.Application();
Excel.Application ExcelApp
= new Excel.Application();
ExcelApp.Visible = true;
Excel.Workbook wb = ExcelApp.Workbooks.Add();
Excel.Worksheet ws1 = wb.Sheets[1];
ws1.Select(Type.Missing);
for (int i = 1; i < 20; i++)
{
Excel.Range rgn = ws1.Cells[i, 1];
rgn.Value2 = i;
}
wb.SaveAs("c:\\mdb\\temp3.xlsx");
wb.Close(false);
ExcelApp.Quit();
}
}
}
view raw cs_excel_write hosted with ❤ by GitHub

参考にさせて頂いたサイト
http://www.ipentec.com/document/document.aspx?page=csharp-save-excel-new-file-and-write&culture=ja-jp

読み込むサンプルは以下です。
http://chaos-fractal.blogspot.jp/2012/12/c-excel.html

0 件のコメント:

コメントを投稿