简体   繁体   English

C#读/写.xlsm文件

[英]C# read/write .xlsm Files

Hi i try to work with Excel files and C#. 嗨,我尝试使用Excel文件和C#。 Right now i can work with xlsx files and can open it and so on. 现在,我可以使用xlsx文件并可以打开它,依此类推。 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. 但是,当我将其更改为xlsm Files时,我总是会得到一个例外,即找不到文件的地方,我也不知道为什么。 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. 使用.xlsx文件可以使用,但不适用于xlsm。 So any help would be great. 因此,任何帮助都会很棒。 So i tried to google and some blogs but nothing worked for me. 因此,我尝试使用Google和一些博客,但没有任何帮助。 Mabey there is something i dont get.... 玛比,我有些不明白。

Thanks for your time and sorry for my english. 感谢您的时间,对不起我的英语。

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 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 或者更好,使用一个恒定值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM