読者です 読者をやめる 読者になる 読者になる

C#からExcelを開く

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop;

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

namespace excel_starter
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var app = new Excel.Application();
                // Make the object visible.
                app.Visible = true;

                // Load My Addin 
                //app.RegisterXLL(Directory.GetCurrentDirectory() + "\\addin.xll");

                // Open My Workbook 
                Excel.Workbooks wbks = app.Workbooks;
                wbks.Open(Directory.GetCurrentDirectory() + "\\addin1.xlam");
                Excel.Workbook wbk = wbks.Open(Directory.GetCurrentDirectory() + "\\example.xlsx");

                // Release COM-Objects
                Marshal.ReleaseComObject(wbk);
                Marshal.ReleaseComObject(wbks);
                Marshal.ReleaseComObject(app);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
    }
}