XLSX ファイルの作成と読み込み(ExcelCreator)

文書番号 : 28267     文書種別 : 使用方法     最終更新日 : 2009/11/27
文書を印刷する
対象製品
MultiRow for Windows Forms 6.0J
詳細
MultiRow には Excel の XLSX ファイルを直接取り扱う機能は提供されていません。ここでは、アドバンスソフトウェア株式会社の「ExcelCreator 6.0 for .NET」を使用して MultiRow のデータを XLSX ファイルとして入出力する例を紹介します。

※MultiRowにはExcelCreator 6.0 for .NETのライセンスは含まれません。別途アドバンスソフトウェア株式会社よりご購入ください。

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

[Visual Basic]
Imports ExcelCreator6
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
  Dim XlsxCreator1 As New XlsxCreator()

  XlsxCreator1.CreateBook("C:¥test.xlsx", 1, xlVersion.ver2007)
  For rowIndex As Integer = 0 To GcMultiRow1.RowCount - 1
    For cellIndex As Integer = 0 To GcMultiRow1.Template.Row.Cells.Count - 1
      XlsxCreator1.Pos(cellIndex, rowIndex).Str = GcMultiRow1.Rows(rowIndex).Cells(cellIndex).DisplayText
      ' 書式を設定することも可能です
      ' XlsxCreator1.Pos(cellIndex, rowIndex).Attr.BackColor = GcMultiRow1.Rows(rowIndex).Cells(cellIndex).Style.BackColor
      ' XlsxCreator1.Pos(cellIndex, rowIndex).Attr.ForeColor = GcMultiRow1.Rows(rowIndex).Cells(cellIndex).Style.ForeColor
    Next
  Next
  XlsxCreator1.CloseBook(True)
  XlsxCreator1.Dispose()
End Sub


[C#]
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();
}

private void button1_Click(object sender, EventArgs e)
{
  XlsxCreator xlsxCreator1 = new XlsxCreator();

  xlsxCreator1.CreateBook(@"C:¥test.xlsx", 1, xlVersion.ver2007);
  for (int rowIndex = 0; rowIndex < gcMultiRow1.RowCount; rowIndex++)
  {
    for (int cellIndex = 0; cellIndex < gcMultiRow1.Template.Row.Cells.Count; cellIndex++)
    {
      xlsxCreator1.Pos(cellIndex, rowIndex).Str = gcMultiRow1.Rows[rowIndex].Cells[cellIndex].DisplayText;
      // 書式を設定することも可能です
      // xlsxCreator1.Pos(cellIndex, rowIndex).Attr.BackColor = gcMultiRow1.Rows[rowIndex].Cells[cellIndex].Style.BackColor;
      // xlsxCreator1.Pos(cellIndex, rowIndex).Attr.ForeColor = gcMultiRow1.Rows[rowIndex].Cells[cellIndex].Style.ForeColor;
    }
  }
  xlsxCreator1.CloseBook(true);
  xlsxCreator1.Dispose();
}


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

[Visual Basic]
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  Dim XlsxCreator1 As New XlsxCreator()

  GcMultiRow1.RowCount = 1
  GcMultiRow1.RowCount = 5

  XlsxCreator1.ReadBook("C:¥test.xlsx")
  For rowIndex As Integer = 0 To XlsxCreator1.MaxData(xlMaxEndPoint.xarMaxPoint).Height
    For cellIndex As Integer = 0 To XlsxCreator1.MaxData(xlMaxEndPoint.xarMaxPoint).Width
      GcMultiRow1.SetValue(rowIndex, cellIndex, XlsxCreator1.Pos(cellIndex, rowIndex).Str)
    Next
  Next
  XlsxCreator1.CloseBook(True)
  XlsxCreator1.Dispose()
End Sub


[C#]
private void button2_Click(object sender, EventArgs e)
{
  XlsxCreator xlsxCreator1 = new XlsxCreator();

  gcMultiRow1.RowCount = 1;
  gcMultiRow1.RowCount = 5;

  xlsxCreator1.ReadBook(@"C:¥test.xlsx");
  for (int rowIndex = 0; rowIndex < xlsxCreator1.MaxData(xlMaxEndPoint.xarMaxPoint).Height + 1; rowIndex++)
  {
    for (int cellIndex = 0; cellIndex < xlsxCreator1.MaxData(xlMaxEndPoint.xarMaxPoint).Width + 1; cellIndex++)
    {
      gcMultiRow1.SetValue(rowIndex, cellIndex, xlsxCreator1.Pos(cellIndex, rowIndex).Str);
    }
  }
  xlsxCreator1.CloseBook(true);
  xlsxCreator1.Dispose();
}

関連情報

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