実用的アプリケーションの開発(Excel編)

何を作るか?

カスタム作業ウインドウでボタンを押すと、Excelブック内のシートに帳票フォーマットを生成する、ドキュメントレベルのVSTOアプリケーションを作成します。

プロジェクトの作成

[ファイル]⇒[新規作成]⇒[プロジェクト]から、[Excel 2007 ブック]を作成します。


ドキュメントは新規に作成します。


[プロジェクト]⇒[ユーザーコントロールの追加]⇒[新しい項目の追加]⇒[ユーザーコントロール]を選択し、追加を押します。


ユーザーコントロールのデザイナが開きますが、ここでは何もしません。
ThisWorkBook.csのコードを表示し、Startupイベントハンドラ(ThisWorkbook_Startup)内に、カスタム作業ウインドウを表示するためのコードを記述します。
実際のコードは以下となります。

UserControl1 uc = new UserControl1();
this.ActionsPane.Controls.Add(uc);
this.Application.CommandBars["Task Pane"].Position
    = Microsoft.Office.Core.MsoBarPosition.msoBarRight;

カスタム作業ウインドウの編集

ユーザーコントロールのデザイナを開き、「帳票作成」、「クリア」、「全てクリア」のボタンを作成します。


「全てクリア」ボタンをダブルクリックし、イベントハンドラを生成させます。
ここでは、全てのシートをクリアする必要があります。追加するコードは以下となります。

private void button3_Click(object sender, EventArgs e)
{
    Globals.ThisWorkbook.ClearAllSeets(sender, e);
}

実際にクリア作業を行うのは、ThisWorkbookのClearAllSeetsです。以下のように実装します。

public void ClearAllSeets(object sender, System.EventArgs e)
{
    foreach (Excel.Worksheet workSheet in this.Worksheets)
    {
        workSheet.Cells.Clear();
    }
}

ユーザーコントロールのデザイナを開き、「クリア」ボタンをダブルクリックして、イベントハンドラを生成させます。
ここでは、アクティブなシートのみクリアする必要があります。追加するコードは以下となります。

private void button2_Click(object sender, EventArgs e)
{
    Globals.ThisWorkbook.ClearActiveSeets(sender, e);
}

実際にクリア作業を行うのは、ThisWorkbookのClearActiveSeetsです。以下のように実装します。

public void ClearActiveSeets(object sender, System.EventArgs e)
{
    ((Excel.Worksheet)this.ActiveSheet).Cells.Clear();
}

帳票の作成

ユーザーコントロールのデザイナを開き、「帳票作成」ボタンをダブルクリックして、イベントハンドラを生成させます。追加するコードは以下となります。

private void button1_Click(object sender, EventArgs e)
{
    Globals.ThisWorkbook.MakeFormatedSeet(sender, e);
}

帳票の作成そのものは、ThisWorkbookのMakeFormatedSeetが行います。
まずはひな形を示します。

public void MakeFormatedSheet(object sender, EventArgs e)
{
}

作成する帳票は、以下のようなものにします。


まずは、外枠部分を描画します。
記述したコードは以下となります。

#region 外枠の描画

// 1ページ目外枠
Excel.Worksheet activeSheet = (Excel.Worksheet)this.ActiveSheet;
Excel.Range temp = ((Excel.Range)activeSheet.get_Range(
    activeSheet.Cells[1, 1], activeSheet.Cells[59, 9]));
temp.BorderAround(missing, Excel.XlBorderWeight.xlThin
    , Excel.XlColorIndex.xlColorIndexAutomatic, missing);

// 2ページ目外枠
temp = ((Excel.Range)activeSheet.get_Range(
    activeSheet.Cells[60, 1], activeSheet.Cells[118, 9]));
temp.BorderAround(missing, Excel.XlBorderWeight.xlThin
    , Excel.XlColorIndex.xlColorIndexAutomatic, missing);

#endregion

次に、タイトル周りの表部分を描画します。
記述したコードは以下となります。

#region タイトル周りの描画

// 配布先
temp = ((Excel.Range)activeSheet.get_Range(
    activeSheet.Cells[1, 1], activeSheet.Cells[4, 2]));
temp.BorderAround(missing, Excel.XlBorderWeight.xlThin
    , Excel.XlColorIndex.xlColorIndexAutomatic, missing);
temp.Merge(true);
temp.Merge(false);
temp.VerticalAlignment = Excel.Constants.xlTop;

// タイトル
temp = ((Excel.Range)activeSheet.get_Range(
    activeSheet.Cells[1, 3], activeSheet.Cells[4, 7]));
temp.BorderAround(missing, Excel.XlBorderWeight.xlThin
    , Excel.XlColorIndex.xlColorIndexAutomatic, missing);
temp.Merge(true);
temp.Merge(false);
temp.HorizontalAlignment = Excel.Constants.xlCenter;
temp.VerticalAlignment = Excel.Constants.xlCenter;

// 文書番号、日付、所属、氏名
for (int i = 0; i < 4; ++i)
{
    temp = ((Excel.Range)activeSheet.get_Range(
        activeSheet.Cells[1 + i, 8], activeSheet.Cells[1 + i, 9]));
    temp.BorderAround(missing, Excel.XlBorderWeight.xlThin
        , Excel.XlColorIndex.xlColorIndexAutomatic, missing);
    temp.Merge(true);
    temp.HorizontalAlignment = Excel.Constants.xlCenter;
}

#endregion

更新履歴表を描画します。
記述したコードは以下となります。

#region 更新履歴表の描画

for (int i = 0; i < 3; ++i)
{
    for (int j = 0; j < 3; ++j)
    {
        temp = (Excel.Range)activeSheet.Cells[7 + i, 2 + j];
        temp.BorderAround(missing, Excel.XlBorderWeight.xlThin
            , Excel.XlColorIndex.xlColorIndexAutomatic, missing);
        temp.HorizontalAlignment = Excel.Constants.xlCenter;
    }
    temp = ((Excel.Range)activeSheet.get_Range(
        activeSheet.Cells[7 + i, 5], activeSheet.Cells[7 + i, 8]));
    temp.BorderAround(missing, Excel.XlBorderWeight.xlThin
        , Excel.XlColorIndex.xlColorIndexAutomatic, missing);
    temp.Merge(true);
}

#endregion

各部分の文字列を描画します。
記述したコードは以下となります。

#region 文字列を書く

// Excel.Range.Valueは未サポート、Excel.Range.set_Valueは引数が不明で使えず。

((Excel.Range)activeSheet.Cells[1, 1]).Value2 = "配布先";
((Excel.Range)activeSheet.Cells[1, 3]).Value2 = "Excel仕様書テンプレート";
((Excel.Range)activeSheet.Cells[1, 8]).Value2 = "IJ-XXXXXX";
((Excel.Range)activeSheet.Cells[2, 8]).Value2 = "2008/5/21";
((Excel.Range)activeSheet.Cells[3, 8]).Value2 = "情報技術開発部";

((Excel.Range)activeSheet.Cells[6, 2]).Value2 = "改版履歴";
((Excel.Range)activeSheet.Cells[7, 2]).Value2 = "バージョン";
((Excel.Range)activeSheet.Cells[7, 3]).Value2 = "日付";
((Excel.Range)activeSheet.Cells[7, 4]).Value2 = "作成者";
((Excel.Range)activeSheet.Cells[7, 5]).Value2 = "内容";

#endregion

[デバッグ]⇒[デバッグの開始]を行うと、カスタム作業ウインドウが表示された状態で起動します。カスタム作業ウインドウにある[帳票作成]ボタンを押すと、以下のように枠線などが描画されます。


Sheet2、Sheet3に切り替えても、同様のことが行えます。
最後に、[全てクリア]ボタンを押すと、全てのシートが白紙に戻ります。