Return to Tech/dotnet

Office Automation

.NET(C#)ぷろぐらみんぐ

C#からMicrosfot OfficeのCOMを利用し
Excel, Wordを操作する例を記します。

Visual Studioもしくは
.NET Framework SDKのcscで作成します

1. 事前準備
Microsoft Officeがインストールされていること
以下のアセンブリファイルが存在することを確認します。
Microsoft.Office.Interop.Excel.dll
Microsoft.Office.Interop.Word.dll
上記dllを作業ディレクトリへ複製し、csc利用時に利用します。

例 csc pia01.cs /r:Microsoft.Office.Interop.Excel.dll,Microsoft.Office.Interop.Word.dll 


2. コード編集を行います。
file:pia01.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

using Excel = Microsoft.Office.Interop.Excel;
using Word  = Microsoft.Office.Interop.Word;

namespace pia01 {
 class Program {
  private static string fileName = "c:\work\test.xls";

  static void Main(string[] args) {
   // Excel自身を利用するためのオブジェクトを宣言します。
   Excel.Application objExcel = null;

   // xls bookを操作するためのオブジェクトを宣言します。
   Excel.Workbook objExcelWorkbook = null;

   // グラフを操作するためのオブジェクトを宣言します。
   Excel.ChartObject objChart = null;

   try {
    objExcel = new Excel.Application();

    // 警告ダイアログ表示を有効とします。
    objExcel.DisplayAlerts = true;

    // 実行時にExcelを表示します。
    objExcel.Visible = true;

    objExcelWorkbook = (Excel.Workbook)(objExcel.Workbooks.Open(
     fileName, // ファイル名 test.xls
     Type.Missing, // UpdateLinks
     Type.Missing, // ReadOnly
     Type.Missing, // Format
     Type.Missing, // パスワード
     Type.Missing, // WriteResPassword
     Type.Missing, // IgnoreReadOnlyRecommended
     Type.Missing, // Origin
     Type.Missing, // Delimiter
     Type.Missing, // Editable
     Type.Missing, // Notify
     Type.Missing, // Converter
     Type.Missing, // Unknown
     Type.Missing, // Local
     Type.Missing  // CorruptLoad
     )
    );

    // アクティブ化されたExcel内シートのオブジェクトを取得
    Excel.WorkSheet objWorkSheet = (Excel.Worksheet)objExcelWorkbook.ActiveSheet;

    //
    Console.WriteLine("セルA1の値:" + objWorkSheet.Cells[1,1].Value);

    // セルA1 センタリング
    objWorkSheet.Cells[1,1].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;

    // 現在時刻を取得し、セットします。
    objWorkSheet.Cells[2,1] = DateTime.Now.ToString("HH時MM分ss秒");

    // 範囲指定、フォント、サイズ、色等設定
    Excel.Range objRange = objWorkSheet.Cells[2,1];
    objRange.Font.Size = 8;
    objRange.Font.Name = "MS ゴシック";
    objRange.Font.Color = "0xFF0000";
    objRange.Interior.Color = "0x000000";

    // グラフを追加します
    objChart = objWorkSheet.ChartObjects().Add(250, 20, 300, 250);
    var chartPage = objChart.Chart;
    var chartRange = objWorkSheet.Range[objWorkSheet.Cells[4,2], objWorkSheet.Cells[14,4]];
    chartPage.SetSourceData(chartRange);
    chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

    // セル結合
    var mergeRange = objWorkSheet.Range[objWorkSheet.Cells[4,1], objWorkSheet.Cells[14,1]];
    mergeRange.Merge();

    // 文字列表示位置を調整します
    objWorkSheet.Cells[4,1].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
    objWorkSheet.Cells[4,1].VerticalAlignemtn   = Excel.XlVAlign.xlVAlignTop;

    objExcelWorkbook.CheckCompatibility = false;
    objExcelWorkbook.SaveAs(fileName);

    Console.WriteLine("End");
   } catch (FileNotFoundException fne) {
    Console.WriteLine(fne.Message);
   } catch (System.Runtime.InteropServices.COMException comex) {
    Console.WriteLine(comex.Message);
   } finally {
    if (objExcelWorkbook != null) {
     objExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
     objExcel.Quit();
    }
   }
  }
 }
}

3. コンパイル と プログラム実行
C:>csc Program.cs /r:Microsoft.Office.Interop.Excel,Microsoft.Office.Interop.Word.dll

実行結果

変更前のExcelブック


変更後のExcelブック



Return to Tech/dotnet