C#读/写.xlsm文件

时间:2016-03-30 09:16:50

标签: c# excel office-interop

您好我尝试使用Excel文件和C#。现在我可以使用xlsx文件,可以打开它等等。但是,当我将其更改为xlsm文件时,我总是得到一个异常,即找不到的文件,我不知道为什么。这是我的代码:

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);
        }

    }
}

这是例外:

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 .

但文件就在那里。

使用.xlsx文件可以使用但不能使用xlsm。所以任何帮助都会很棒。所以我试着谷歌和一些博客,但没有任何对我有用。 Mabey有一些我不明白的东西......

感谢您的时间,对不起我的英语。

1 个答案:

答案 0 :(得分:0)

您的路径必须

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

而不是

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

\

之后添加Debug

修正此行:

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

到此:

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

或者更好,使用常数值