XLS ファイルの作成と読み込み(OLEDB)

文書番号 : 28176     文書種別 : 使用方法     最終更新日 : 2009/11/17
文書を印刷する
対象製品
MultiRow for Windows Forms 6.0J
詳細
MultiRow には Excel で出力された XLS ファイルを直接取り扱う機能は提供されていませんが、Microsoft Jet 4.0 OLE DB プロバイダを使用する一般的な方法によって XLS ファイルの取り扱いが可能です。

XLS ファイルの作成
次のコードは、サンプルとして使用する XLS ファイルを出力します。

[Visual Basic]
Imports System.Data.OleDb
Imports GrapeCity.Win.MultiRow

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  ' サンプルのグリッドを作成します

  Dim textBoxCell1 As New TextBoxCell()
  textBoxCell1.Name = "textBoxCell1"
  Dim textBoxCell2 As New TextBoxCell()
  textBoxCell2.Name = "textBoxCell2"

  GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {textBoxCell1, textBoxCell2})
  GcMultiRow1.RowCount = 5
  GcMultiRow1.SuspendLayout()

  For i As Integer = 0 To GcMultiRow1.RowCount - 1
    GcMultiRow1.SetValue(i, "textBoxCell1", "ABC")
    GcMultiRow1.SetValue(i, "textBoxCell2", "日本語")
  Next

  GcMultiRow1.ResumeLayout()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  ' XLS ファイルを出力します
  
  Dim fileName As String = "C:¥MyDocuments¥test.xls"
  Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'"
  Using oleDbConnection As New OleDbConnection(String.Format(connectionString, fileName))
    oleDbConnection.Open()
    ' 新しいテーブルを作成する
    Dim oleDbCommand As New OleDbCommand("CREATE TABLE [Sheet1] ([Column1] string, [Column2] string)", oleDbConnection)
    oleDbCommand.ExecuteNonQuery()
    ' 値を追加する
    For i As Integer = 0 To GcMultiRow1.RowCount - 1
      Dim column1Value As String = GcMultiRow1.GetValue(i, "textBoxCell1").ToString()
      Dim column2Value As String = GcMultiRow1.GetValue(i, "textBoxCell2").ToString()

      oleDbCommand = New OleDbCommand("INSERT INTO [Sheet1$] (Column1, Column2) VALUES ('" & column1Value & "', '" & column2Value & "')", oleDbConnection)
      oleDbCommand.ExecuteNonQuery()
    Next
  End Using
End Sub


[C#]
using System.Data.OleDb;
using GrapeCity.Win.MultiRow;

private void button1_Click(object sender, EventArgs e)
{
  string fileName = @"C:¥MyDocuments¥test.xls";
  string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'";

  using (OleDbConnection oleDbConnection = new OleDbConnection(string.Format(connectionString, fileName)))
  {
    oleDbConnection.Open();
    // 新しいテーブルを作成する
    OleDbCommand oleDbCommand = new OleDbCommand("CREATE TABLE [Sheet1] ([Column1] string, [Column2] string)", oleDbConnection);
    oleDbCommand.ExecuteNonQuery();
    for (int i = 0; i < gcMultiRow1.RowCount; i++)
    {
      string column1Value = gcMultiRow1.GetValue(i, "textBoxCell1").ToString();
      string column2Value = gcMultiRow1.GetValue(i, "textBoxCell2").ToString();

      oleDbCommand = new OleDbCommand("INSERT INTO [Sheet1$] (Column1, Column2) VALUES ('" + column1Value + "', '" + column2Value + "')", oleDbConnection);
      oleDbCommand.ExecuteNonQuery();
    }
  }
}

private void Form1_Load(object sender, EventArgs e)
{
  // サンプルのグリッドを作成します

  TextBoxCell textBoxCell1 = new TextBoxCell();
  textBoxCell1.Name = "textBoxCell1";
  TextBoxCell textBoxCell2 = new TextBoxCell();
  textBoxCell2.Name = "textBoxCell2";

  gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { textBoxCell1, textBoxCell2 });
  gcMultiRow1.RowCount = 5;
  gcMultiRow1.SuspendLayout();

  for (int i = 0; i < gcMultiRow1.RowCount; i++)
  {
    gcMultiRow1.SetValue(i, "textBoxCell1", "ABC");
    gcMultiRow1.SetValue(i, "textBoxCell2", "日本語");
  }

  gcMultiRow1.ResumeLayout();
}


XLS ファイルの読み込み
次のコードは、上記のサンプルで作成した XLS ファイルの内容を GcMultiRow コントロールに表示します。

[Visual Basic]
Imports System.Data.OleDb
Imports GrapeCity.Win.MultiRow

Private _dataSet As New DataSet()

Dim fileName As String = "C:¥MyDocuments¥test.xls"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'"
Dim query As String = "SELECT * FROM [Sheet1$]"

Using oleDbConnection As New OleDbConnection(String.Format(connectionString, fileName))
  Using oleDbDataAdapter As New OleDbDataAdapter(query, oleDbConnection)
    oleDbDataAdapter.Fill(_dataSet)
  End Using
End Using

' セルとデータフィールドの対応を指定する
Dim textBoxCell1 As New TextBoxCell
textBoxCell1.Name = "textBoxCell1"
textBoxCell1.DataField = "Column1"
Dim textBoxCell2 As New TextBoxCell
textBoxCell2.Name = "textBoxCell2"
textBoxCell2.DataField = "Column2"

GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {textBoxCell1, textBoxCell2})

' データバウンドと同じ方法で XLS ファイルに接続する
GcMultiRow1.DataSource = _dataSet
GcMultiRow1.DataMember = _dataSet.Tables(0).TableName

' データバウンドしない場合、次のコードで XLS ファイルの値を読み出せる
Console.WriteLine(Me._dataSet.Tables(0).Rows(0)("Column1").ToString())

[C#]
using System.Data.OleDb;
using GrapeCity.Win.MultiRow;

private DataSet _dataSet = new DataSet();

string fileName = @"C:¥MyDocuments¥test.xls";
string connectionStringTemplate = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;'";
string query = "SELECT * FROM [Sheet1$]";

using (OleDbConnection oldDbConnection = new OleDbConnection(string.Format(connectionStringTemplate, fileName)))
{
  using (OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(query, oleDbConnection))
  {
    oleDbDataAdapter.Fill(this._dataSet);
  }
}

// セルとデータフィールドの対応を指定する
Template template = Template.Default;
template.Row.Cells["textBoxCell1"].DataField = "Column1";
template.Row.Cells["textBoxCell2"].DataField = "Column2";
this.gcMultiRow1.Template = template;

// セルとデータフィールドの対応を指定する
TextBoxCell textBoxCell1 = new TextBoxCell();
textBoxCell1.Name = "textBoxCell1";
textBoxCell1.DataField = "Column1";
TextBoxCell textBoxCell2 = new TextBoxCell();
textBoxCell2.Name = "textBoxCell2";
textBoxCell2.DataField = "Column2";

gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { textBoxCell1, textBoxCell2 });

// データバウンドと同じ方法で XLS ファイルに接続する
gcMultiRow1.DataSource = this._dataSet;
gcMultiRow1.DataMember = this._dataSet.Tables[0].TableName;

// データバウンドしない場合、次のコードで XLS ファイルの値を読み出せる
Console.WriteLine(this._dataSet.Tables[0].Rows[0]["Column1"].ToString());

参考
より詳細な使用方法については、Microsoft 社が提供する次の情報を参照してください。
Visual Basic.NET と ADO.NET を使用して Excel ブックのレコードの取得と変更を行う方法
関連情報

この文書は、以前は次のFAQ IDで公開されていました : 12631