【C#】【Excel】Excel を PDF化する

Excel を PDF化するには

 * Workbook.ExportAsFixedFormat()を使うといい。
http://msdn.microsoft.com/ja-jp/library/microsoft.office.tools.excel.workbook.exportasfixedformat.aspx

■ 使用上の注意

環境

  [1] .NET3.5以上
  [2] Excel2007 SP2以上がインストールされている

エラーが発生したら

 * ひとまず、以下を確認する

  [1] 実行環境にExcel2007以上が入っているか?
  [2] 入っていてもUpdateしているか?

サーバ側で使うことへの注意

 * デスクトップアプリケーション以外でのOfficeオートメーションは推奨されていないらしい。

■ サンプル

using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

private void button22_Click(object sender, EventArgs e)
{
    var excel = new Excel.Application();
    if (excel == null)
    {
        MessageBox.Show("Excelないよ");
        return;
    }

    Workbook workbook = null;
    Worksheet worksheet = null;
    try
    {
        workbook = excel.Workbooks.Open(
          @"C:\sample.xls",  // オープンするExcelファイル名
          Type.Missing, // (省略可能)UpdateLinks (0 / 1 / 2 / 3)
          Type.Missing, // (省略可能)ReadOnly (True / False )
          Type.Missing, // (省略可能)Format
            // 1:タブ / 2:カンマ (,) / 3:スペース / 4:セミコロン (;)
            // 5:なし / 6:引数 Delimiterで指定された文字
          Type.Missing, // (省略可能)Password
          Type.Missing, // (省略可能)WriteResPassword
          Type.Missing, // (省略可能)IgnoreReadOnlyRecommended
          Type.Missing, // (省略可能)Origin
          Type.Missing, // (省略可能)Delimiter
          Type.Missing, // (省略可能)Editable
          Type.Missing, // (省略可能)Notify
          Type.Missing, // (省略可能)Converter
          Type.Missing, // (省略可能)AddToMru
          Type.Missing, // (省略可能)Local
          Type.Missing);// (省略可能)CorruptLoad
        if (workbook == null || workbook.Sheets.Count <= 0)
        {
            MessageBox.Show("Sheetがない!?");
            return;
        }
        worksheet = (Worksheet)workbook.Sheets[1];

        // 設定方法1:セル指定
        worksheet.Cells[1, 1] = "Sample1! [A,1]";

        // 設定方法2:範囲指定
        var rangeA2 = worksheet.get_Range("A2", Missing.Value);
        rangeA2.Value2 = "Sample2! [A, 2]";

        // PDF化
        workbook.ExportAsFixedFormat(
            XlFixedFormatType.xlTypePDF,
            @"C:\sample.xls",
            XlFixedFormatQuality.xlQualityStandard,
            true,
            true,
            Type.Missing,
            Type.Missing,
            false,
            Type.Missing);
    }
    catch
    {
        MessageBox.Show("何かでエラー!?");
    }
    finally
    {
        // 開放処理
        // [Worksheet] -> [Worksheets] -> [Workbook] -> [Workbooks] -> [Excel Application]
        // の順に開放すること

        if (worksheet != null)
        {
            Marshal.ReleaseComObject(worksheet);
        }
        if (workbook != null)
        {
            workbook.Close(true, Type.Missing, Type.Missing);
            Marshal.ReleaseComObject(workbook);
        }
        if (excel != null)
        {
            excel.Quit();
            Marshal.ReleaseComObject(excel);
        }
    }
}


関連記事

Excel

Excelファイルを扱う ~ 入門編 ~
https://blogs.yahoo.co.jp/dk521123/29610792.html
Excel を出力する際の注意事項
https://blogs.yahoo.co.jp/dk521123/29462017.html