【C#】Excel操作:Excel入力


 フォーム


Excel操作:Excel情報の取得に機能追加して、指定したExcelブック名 と Excelシート名に文字を入力します。



追加機能は、【X座標】【Y座標】を入力すると、(1, 1)から(X, Y)まで1を足した結果を出力します。(罫線付き)
例えば、(5, 3)とインプットしたら、下記結果となるようにします。





 変数


セルの範囲を取り扱う場合は Rangeオブジェクトを使います。

/* Excelレンジ */
Microsoft.Office.Interop.Excel.Range ExcelRNG;

/* 対象ファイル、対象シート */
public String TargetFile, TargetSheet;

/* X座標、Y座標 */
public int LineX, LineY;



 Excelファイルの情報取得


処理の流れとしては、下記となります。
①コンボボックスの情報からExcelブック名を取得する
②リストボックスの情報からExcelシート名を取得する
③Excelオブジェクトの取得
  ・Excelブック
  ・Excelシート
④X座標の情報取得(String型からint型へ変換)
⑤Y座標の情報取得(String型からint型へ変換)
⑥Excelセルへの書き込み (注)セルの初期値は1から開始
  => レスポンスを向上させるにはこちら参照
⑦罫線を付ける

/**********************************************
* Excel入力
**********************************************/

private void cmdInput_Click(object sender, EventArgs e)
{
  /* ①コンボボックスの情報からExcelブック名を取得する */
  TargetFile = cmbExcelBook.Text;

  /* ②リストボックスの情報からExcelシート名を取得する */
  TargetSheet = lstExcelSheet.SelectedItem.ToString();

  /* ③Excelオブジェクトの取得 */
  ExcelWB = ExcelApp.Workbooks.Item[TargetFile];
  ExcelWS = (Worksheet)ExcelWB.Sheets[TargetSheet];

  /* ④X座標の情報取得(String型からint型へ変換) */
  LineX = int.Parse(txtLineX.Text);

  /* ⑤Y座標の情報取得(String型からint型へ変換) */
  LineY = int.Parse(txtLineY.Text);

  /* ⑥Excelセルへの書き込み */
  for (int i = 1; i <= LineX; i++)
  {
    for (int j = 1; j <= LineY; j++)
    {
      ExcelWS.Cells[i, j] = i + j - 1;
    }
  }

  /* ⑦罫線を付ける */
  ExcelRNG = ExcelWS.get_Range(ExcelWS.Cells[1, 1], ExcelWS.Cells[LineX, LineY]);

  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous;
}

ただし、上記はエラー処理が全く考慮されていないので、もし丁寧に記述するなら(抜け漏れがあるかもしれませんが)下記の様になります。

/**********************************************
* Excel入力
**********************************************/

private void cmdInput_Click(object sender, EventArgs e)
{
  /* コンボボックスの情報からExcelブック名を取得する */
  TargetFile = cmbExcelBook.Text;

  /* リストボックスの情報からExcelシート名を取得する */
  if(lstExcelSheet.SelectedItems.Count == 0)
  {
    MessageBox.Show("書き込むシートを選択して下さい");
    return;
  }
  TargetSheet = lstExcelSheet.SelectedItem.ToString();

  /* Excelオブジェクトの取得 */
  ExcelWB = ExcelApp.Workbooks.Item[TargetFile];
  ExcelWS = (Worksheet)ExcelWB.Sheets[TargetSheet];

  /* X座標の情報取得(String型からint型へ変換) */
  try
  {
    LineX = int.Parse(txtLineX.Text);
    if (LineX < 1)
    {
      MessageBox.Show("X座標は1以上の整数で入力して下さい");
      return;
    }
  }
  catch (System.Exception ErrMSG)
  {
    MessageBox.Show("X座標:" + txtLineX.Text
      + "の入力内容に誤りがあります。1以上の整数で入力して下さい。("
      + ErrMSG.Message.ToString() + ")");
    return;
  }

  /* Y座標の情報取得(String型からint型へ変換) */
  try
  {
    LineY = int.Parse(txtLineY.Text);
    if (LineY < 1)
    {
      MessageBox.Show("Y座標は1以上の整数で入力して下さい");
      return;
    }
  }
  catch (System.Exception ErrMSG)
  {
    MessageBox.Show("Y座標:" + txtLineX.Text
      + "の入力内容に誤りがあります。1以上の整数で入力して下さい。("
      + ErrMSG.Message.ToString() + ")");
    return;
  }

  /* Excelセルへの書き込み */
  for (int i = 1; i <= LineX; i++)
  {
    for (int j = 1; j <= LineY; j++)
    {
      try
      {
        ExcelWS.Cells[i, j] = i + j - 1;
      }
      catch (System.Exception ErrMSG)
      {
        MessageBox.Show("(" + i + ", " + j + ") で " + (i + j - 1).ToString()
          + "をインプット中にエラーが発生しました。("
          + ErrMSG.Message.ToString() + ")");
        return;
      }
    }
  }

  /* 罫線を付ける */
  ExcelRNG = ExcelWS.get_Range(ExcelWS.Cells[1, 1], ExcelWS.Cells[LineX, LineY]);
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous;
  ExcelRNG.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous;
}

管理人 について

趣味:映画鑑賞・音楽鑑賞・ゲーム・旅行 仕事:会社員(IT関連)
カテゴリー: C#, Excel操作, システム開発 パーマリンク

コメントを残す