WinForms COM异常打开Excel工作簿 [英] WinForms COM Exception opening Excel workbook

查看:134
本文介绍了WinForms COM异常打开Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB.Net编写一个非常基本的WinForms应用程序,一切运行顺利,直到尝试打开通过Stream对象写入tempLocation的Excel文件为止(工作簿在我的项目中)资源并将构建操作设置为嵌入式资源")

I'm writing a very basic WinForms app using VB.Net, everything is going smoothly until I try and open an Excel file that I've written to the tempLocation via a Stream object (The workbook is in my project resources and the build action is set to "Embedded Resource")

以下是导致错误的代码:

Here's the code that causes the error:

Dim xlWBTemp As Excel.Workbook
Dim xlApp As Excel.Application
'// Below resolves to "C:\Users\MacroMan\LockTemplate.xlsm"
Dim tempLocation As String = Environ("USERPROFILE") & "\LockTemplate.xlsm"
...
xlWBTemp = xlApp.Workbooks.Open(tempLocation) '<~~ error here.

我得到的错误是:

未处理System.Runtime.InteropServices.COMException
ErrorCode = -2146827284
HResult = -2146827284
Message =来自HRESULT的异常:0x800A03EC

System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HResult=-2146827284
Message=Exception from HRESULT: 0x800A03EC

我已经检查了文件并确定已创建,Excel正在运行,并且目前已经成功打开了工作簿.我完全无法弄清楚,因此任何指针都值得欢迎.

I've checked and the file definitely gets created, Excel is running and has already successfully opened a workbook at this point. I can't figure this out at all so any pointers truly welcome.

更新:

Update:

文件(tempLocation)确实存在于正确的位置,但是当我在应用程序外部打开文件时,Excel提示我发现无法读取的内容"错误.该文件仍然可以成功打开,并且一旦打开就没有问题.我现在可以通过在Workbooks.Open()方法中使用CorruptLoad参数来解决此错误,但是我觉得这是盲目地忽略了这个问题.

The file (tempLocation) does exist in the correct location, but when I open it outside of the application, Excel gives me the "found unreadable content" error. The file does still open successfully and has no problems once opened. I can now get around this error by using the CorruptLoad argument in the Workbooks.Open() method but I feel this is blindly ignoring the issue.

推荐答案

如果有用,这是我很久以前编写的一个简单VB.NET应用程序的示例,用于测试VB vs C#的速度.这是创建工作簿并写入工作簿的另一种方式.

If it helps here is an example of a simple VB.NET app that I wrote a long time ago to test speed of VB vs C#. It is a different way of going about creating a workbook and writing to it.

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim xlApp As New Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkBook.Application.Visible = True
        xlWorkBook.Application.ScreenUpdating = False

        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.SaveAs("C:\\vbexcel.xlsx")

        Dim l As Long
        l = 1
        Do While l < 500
            'xlWorkSheet.Cells(l, 1) = l
            xlWorkSheet.Range("A" & l).Value = l
            'xlWorkSheet.Cells(l, 2) = l
            'xlWorkSheet.Cells(l, 3) = l
            l = l + 1
        Loop

        xlWorkBook.Application.ScreenUpdating = True
        xlWorkBook.RefreshAll()
        xlWorkBook.Save()

        'xlWorkBook.Close()
        'xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Done")
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

这篇关于WinForms COM异常打开Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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