Search code examples
c#exceloffice-interop

C# read/write .xlsm Files


Hi i try to work with Excel files and C#. Right now i can work with xlsx files and can open it and so on. But when i change it to xlsm Files i always get an exception that the files where not found and i have no clue why. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop;
using System.Reflection;
using System.Windows.Forms;
using System.Diagnostics;

namespace WorkWithExcel
{
    class reportingController
    {

        public void createExcelFile()
        {
            Excel.Application oXL = new Excel.Application();

            Excel.Workbook oWB = oXL.Workbooks.Add(Missing.Value);

            oWB.SaveAs(Application.StartupPath + "\\PROJEKTSTATUS_GESAMT_neues_Layout.xlsm", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            oWB.Close(true, Missing.Value, Missing.Value);
        }

        public void openExcelFile()
        {
            Excel.Application oXL = new Excel.Application();

            Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "PROJEKTSTATUS_GESAMT_neues_Layout.xlsm", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            //read Excel sheets 
            foreach (Excel.Worksheet ws in oWB.Sheets)
            {
                MessageBox.Show(ws.Name);
            }

            //save as separate copy 
            oWB.SaveAs(Application.StartupPath + "\\PROJEKTSTATUS_GESAMT_neues_Layout_neu.xlsm", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            oWB.Close(true, Missing.Value, Missing.Value);
        }

        public void writeExcelFile()
        {
            Excel.Application oXL = new Excel.Application();
            Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "\\PROJEKTSTATUS_GESAMT_neues_Layout.xlsm", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            Excel.Worksheet oWS = oWB.Worksheets[1] as Excel.Worksheet;

            //rename the Sheet name 
            oWS.Name = "Excel Sheet";

            for (int i = 1; i < 10; i++)
            {
                oWS.Cells[i, 1] = "Cell " + i.ToString();
            }
            oWB.SaveAs(Application.StartupPath + "\\PROJEKTSTATUS_GESAMT_neues_Layout.xlsm", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            Process.Start(Application.StartupPath + "\\PROJEKTSTATUS_GESAMT_neues_Layout.xlsm ");
        }

        public void readExcelFile()
        {
            Excel.Application oXL = new Excel.Application();

            Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "\\PROJEKTSTATUS_GESAMT_neues_Layout.xlsm", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            Excel.Worksheet oWS = oWB.Worksheets[1] as Excel.Worksheet;

            Excel.Range range;

            range = oWS.UsedRange;

            //read first row, first cell value 
            MessageBox.Show((string)(range.Cells[1, 1] as Excel.Range).Value2);
        }

    }
}

And here is the exeption:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in WorkWithExcel.exe .

Additional information: ' C : \ Users \ AAN \ Documents \ Visual Studio 2015 \ Projects \ Work With Excel \ Work With Excel \ bin \ DebugPROJEKTSTATUS_GESAMT_neues_Layout.xlsm ' was not found. Check the spelling of the filename , and verify that the file location is correct .

But the file is there.

With an .xlsx file it works but not with an xlsm. So any help would be great. So i tried to google and some blogs but nothing worked for me. Mabey there is something i dont get....

Thanks for your time and sorry for my english.


Solution

  • Your path need to be

    "C : \ Users \ AAN \ Documents \ Visual Studio 2015 \ Projects \ Work With Excel \ Work With Excel \ bin \ Debug \ PROJEKTSTATUS_GESAMT_neues_Layout.xlsm"
    

    and not

    "C : \ Users \ AAN \ Documents \ Visual Studio 2015 \ Projects \ Work With Excel \ Work With Excel \ bin \ DebugPROJEKTSTATUS_GESAMT_neues_Layout.xlsm"
    

    Add \ after Debug

    Fix this line:

    Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "PROJEKTSTATUS_GESAMT_neues_Layout.xlsm"...
    

    to this:

    Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "\\PROJEKTSTATUS_GESAMT_neues_Layout.xlsm"...
    

    Or better, use a constant value