0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

オフライン環境でExcel操作マクロを作る

Posted at

やりたいこと

インターネットにつながらない環境(Windows)でExcelを操作したい!
でもエクセルマクロは使いたくない!

結果

できたけど注意点あり。

環境

  • インターネットにはつながってない
  • .NetFrameworkがインストールされている
  • Officeがインストールされている

やりかた

.NetFrameworkがインストールされていれば一緒に入っているC#のコンパイラを使います。
c:\Windows\Microsoft.NETの下に入っています。

次にエクセルを利用するライブラリ(Microsoft.Office.Interop.Excel)を探します。

> dir /S /B C:\Windows\ | findstr Interop.Excel.dll
C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
....

C#のソースを書きます。(とりあえずシート一覧を表示するだけのサンプル)
参考:C#: Excelファイルを読み書きする (COM)

excel.cs
using System.IO;
using System;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace excel_app
{
    class Program
    {
        static int Main( string[] args )
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Excelファイルを指定してください");
                return 1;
            }
            var path = args[0];

            // Excelのオブジェクトはすべて変数に入れて、使用し終わったら解放を忘れないようにしてください。
            // また、excel.Workbooks.Open() などのようにドットを2回以上続けないでください。
            // (解放漏れが発生して、Excelのプロセスが残り続けるため)
            Microsoft.Office.Interop.Excel.Application excel = null;
            Microsoft.Office.Interop.Excel.Workbooks books = null;
            Microsoft.Office.Interop.Excel.Workbook book = null;
            Microsoft.Office.Interop.Excel.Sheets sheets = null;
            Microsoft.Office.Interop.Excel.Worksheet sheet = null;
            try
            {
                excel = new Microsoft.Office.Interop.Excel.Application();
                excel.DisplayAlerts = false;
                books = excel.Workbooks;
                book = books.Open(path);

                sheets = book.Worksheets;
                for(int i=1 ; i<=sheets.Count;i++)
                {
                    sheet = (Worksheet)sheets[i];
                    Console.WriteLine(sheet.Name);
                }
                book.Close(SaveChanges: false);
                excel.Quit();

            }
            catch (Exception)
            {
                if (book != null) book.Close(SaveChanges: false);
                if (excel != null) excel.Quit();

            }
            finally
            {
                FinalReleaseComObject(sheet);
                FinalReleaseComObject(sheets);
                FinalReleaseComObject(book);
                FinalReleaseComObject(books);
                FinalReleaseComObject(excel);
            }
            return 0;
        }

        static private void FinalReleaseComObject(object o)
        {
            if (o != null) Marshal.FinalReleaseComObject(o);
        }
    }
}

さっき探したMicrosoft.Office.Interop.Excel.dllをソースと同じフォルダにコピーしてビルドする。

> C:\Windows\Microsoft.NET\Framework64\v4.0.xxxx\csc.exe /nologo /reference:Microsoft.Office.Interop.Excel.dll excel.cs

exeができるので実行する!

問題発生

アンチウイルスソフトがexeを検知して消されてしまう。
運用部門から連絡があって注意されました。
検知される理由はわかりませんし、これ以上試すわけにもいかない。
せっかく作ったのに残念。。。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?