尝试打开我的Excel文件使用C#并得到一个错误 [英] Try to open my Excel file using C# and get an error

查看:158
本文介绍了尝试打开我的Excel文件使用C#并得到一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从代码中读取我的Exel文件,并收到 System.InvalidCastException

I try to read my Exel file from code and received System.InvalidCastException:

其他信息:无法将类型为System.__ ComObject的COM对象转换为接口类型Microsoft.Office.Tools.Excel.Worksheet。此操作失败,因为由于以下错误,IID'{297DC8D9-EABD-45A1-BDEF-68AB67E5C3C3}接口的COM组件上的QueryInterface调用失败:不支持此类接口(HRESULT异常:0x80004002(E_NOINTERFACE))

此错误发生在 objsheet = appExcel.ActiveWorkbook.ActiveSheet; 中,所以我尝试将其转换为 objsheet =(Worksheet)appExcel.ActiveWorkbook.ActiveSheet; ,但此错误仍然存​​在

This error occurs in objsheet = appExcel.ActiveWorkbook.ActiveSheet; so I try to cast it into objsheet = (Worksheet)appExcel.ActiveWorkbook.ActiveSheet; but this error still exist

using Microsoft.Office.Interop.Excel;

private static Microsoft.Office.Interop.Excel._Application appExcel;
private static Microsoft.Office.Interop.Excel.Workbook newWorkbook = null;
private static Microsoft.Office.Interop.Excel.Worksheet objsheet = null;
private static string file = @"D:\file.xlsx";

      //Method to initialize opening Excel
    static void excel_init(String path)
    {
        //appExcel = new Microsoft.Office.Interop.Excel.Application();

        if (System.IO.File.Exists(path))
        {
            // then go and load this into excel
            newWorkbook = appExcel.Workbooks.Open(file, true, true);

            int count = newWorkbook.Worksheets.Count;
            if (count > 0)
            {
                objsheet = (Microsoft.Office.Interop.Excel.Worksheet)newWorkbook.Worksheets[1];
            }
        }
        else
        {
            MessageBox.Show("Unable to open file!");
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
            appExcel = null;
            System.Windows.Forms.Application.Exit();
        }
    }

newWorkbook为null。

newWorkbook is null.

推荐答案

尝试下面的代码,看看它是否有效,如果不让我们知道你有什么问题:

Try the below code and see if it works, if not let us know what problems you have:

using Excel = Microsoft.Office.Interop.Excel;

namespace Excel_Sample
{
    public partial class YourClass
    {
        Excel.Application appExcel;
        Excel.Workbook newWorkbook;
        Excel.Worksheet objsheet;
        string file = @"D:\file.xlsx";

        static void excel_init()
        {
            if (System.IO.File.Exists(file))
            {
                //Start Excel and get Application object.
                appExcel = new Excel.Application();
                //Get a workbook.;
                newWorkbook = (Excel.Workbook)(appExcel.Workbooks.Open(file));

                int count = newWorkbook.Worksheets.Count;
                if (count > 0)
                {
                    //Get Your Worksheet
                    objsheet = (Excel.Worksheet)newWorkbook.ActiveSheet;
                }
            }
            else
            {
                MessageBox.Show("Unable to open file!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                appExcel = null;
                System.Windows.Forms.Application.Exit();
            }
        }
    }
}

这篇关于尝试打开我的Excel文件使用C#并得到一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆