相关疑难解决方法(0)

使用VB.NET配置Excel com对象的正确方法?

我有以下代码(从在线教程获得).代码正在运行,但我怀疑处理Excel com对象的方式有点不合适.我们真的需要调用GC.Collect吗?或者,处理此Excel com对象的最佳方法是什么?

Public Sub t1()
    Dim oExcel As New Excel.Application
    Dim oBook As Excel.Workbook = oExcel.Workbooks.Open(TextBox2.Text)

    'select WorkSheet based on name
    Dim oWS As Excel.Worksheet = CType(oBook.Sheets("Sheet1"), Excel.Worksheet)
    Try

        oExcel.Visible = False
        'now showing the cell value
        MessageBox.Show(oWS.Range(TextBox6.Text).Text)

        oBook.Close()
        oExcel.Quit()

        releaseObject(oExcel)
        releaseObject(oBook)
        releaseObject(oWS)
    Catch ex As Exception
        MsgBox("Error: " & ex.ToString, MsgBoxStyle.Critical, "Error!")
    End Try
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() …
Run Code Online (Sandbox Code Playgroud)

.net vb.net com excel

4
推荐指数
2
解决办法
3万
查看次数

如何让C#计时器在创建它的同一个线程上执行?

我想IMessageFilter在Excel Addin 上启用我必须写入excel.我从这里举了一些例子说:

消息过滤器是每个线程,所以我们将此线程注册为消息过滤器(不是创建加载项的主线程 - 因为这是Excel的主线程

我的问题是,当一个计时器过去导致从ThreadPool线程调用写入方法时,我的系统写入excel ,因为它IMessageFilter不能访问RetryRejectedCall部分IMessageFilter,因为它存在于调用者线程而不是执行的生成者之间.计时器.

所以,我的问题是:有没有办法可以强制计时器的Elapsed事件在初始化计时器的同一个线程上运行?

编辑:

我的问题是,如何IMessageFilter在抛出拒绝/忙碌时捕获excel错误?

谢谢

.net c# multithreading timer

4
推荐指数
2
解决办法
1万
查看次数

打开具有c#完全访问权限的Excel工作表

我正在编写一个应用程序,必须将值写入Excel电子表格中的单元格.我可以打开工作簿,但它始终以只读模式打开.我的代码是

myExcelApp = new Excel.ApplicationClass();
myExcelApp.Visible = false;
Excel.Workbook myExcelWorkbooks = myExcelApp.Workbooks.Open(fileName, misValue, **false**, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
Run Code Online (Sandbox Code Playgroud)

第三个参数是只读的,我已将其指定为false.但仍然myExcelWorkbooks.readOnly只显示True.

有人可以帮我解决这个问题吗?

.net c# excel

4
推荐指数
1
解决办法
8617
查看次数

Interop Excel没有关闭流程

我已经在这个问题上苦苦挣扎了几天,我已经进行了研究并应用了我在各种论坛上发现的所有建议,但我仍然无法解决它.

我的问题是excel使用互操作库,我有一个excel文件用作模板,所以我打开它并保存在一个新名称的新位置.一切都很好,除了Excel进程在创建和关闭文件后继续运行.

这是我的代码

protected string CreateExcel(string strProjectID, string strFileMapPath)
{
    string strCurrentDir = HttpContext.Current.Server.MapPath("~/Reports/Templates/");
    string strFile = "Not_Created";

    Application oXL;
    Workbook oWB;        

    oXL = new Application();
    oXL.Visible = false;

    Workbooks wbks = oXL.Workbooks;
    //opening template file
    oWB = wbks.Open(strFileMapPath);        

    oXL.Visible = false;
    oXL.UserControl = false;
    strFile = strProjectID + "_" + DateTime.Now.Ticks.ToString() + ".xlsx";
    //Saving file with new name
   oWB.SaveAs(strCurrentDir + strFile, XlFileFormat.xlWorkbookDefault, null, null,    false, false, XlSaveAsAccessMode.xlExclusive, false, false, null, null);

    oWB.Close(false, strCurrentDir + strFile, Type.Missing);

    wbks.Close();

    oXL.Quit();


    System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net excel visual-studio-2010 office-interop

4
推荐指数
2
解决办法
8252
查看次数

关闭Excel工作簿

我有一些代码打开xls工作簿;

Excel.Workbooks workBooks;
workBooks = excelApp.Workbooks;
workbook = workBooks.Open(sourceFilePath + sourceFileName + ".xls");
Run Code Online (Sandbox Code Playgroud)

然后我得到工作表;

worksheets = workbook.Worksheets;
worksheet = worksheets.get_Item("Standard");
Run Code Online (Sandbox Code Playgroud)

然后我将文件保存为csv;

worksheet.SaveAs(sourceFilePath + sourceFileName + ".csv", Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
Run Code Online (Sandbox Code Playgroud)

然后我尝试关闭工作簿;

Marshal.FinalReleaseComObject(worksheet);
Marshal.FinalReleaseComObject(worksheets);
workbook.Close();
Marshal.FinalReleaseComObject(workbook);
Run Code Online (Sandbox Code Playgroud)

但是,每次进入line workbook.Close()时,系统都会停止.

如果我不执行SaveAs,那么工作簿就会关闭.

如何关闭工作簿?

编辑

查看任务管理器向我显示Excel.exe仍在运行.关闭它会在我的代码中产生错误.

编辑2

我已经看过引用的SO帖子,但它没有解决问题.

c# excel-interop

4
推荐指数
2
解决办法
1万
查看次数

为什么杀掉Excel进程是个坏事?

我已经看过很多文章和问题,关于如何确保Excel实际上在你想要的时候退出并且这个过程不能保持活力.这是一篇描述该问题的知识库文章和Microsoft推荐的解决方案.实质上:

'close files

'Quit Excel
xlApp.quit()

'Release and collect garbage
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
Run Code Online (Sandbox Code Playgroud)

很多人不建议杀死这个过程; 请参见 如何正确清理Excel互操作对象了解.net中的垃圾收集

另一方面,许多人不建议使用GC.Collect.请参阅使用GC.Collect()有什么错误?

根据我的经验,杀死该过程是确保Excel消失的最快速,最简单的方法.我的代码只会杀死它启动的确切进程,而不会杀死其他进程.我确保关闭所有打开的工作簿,退出应用程序并释放xlApp对象.最后,我检查过程是否仍然存在,如果是,那么杀死它.

<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
    End Function

Sub testKill()

    'start the application
    Dim xlApp As Object = CreateObject("Excel.Application")

    'do some work with Excel

    'close any open files

    'get the window handle
    Dim xlHWND As Integer = xlApp.hwnd

    'this will have the process ID …
Run Code Online (Sandbox Code Playgroud)

.net c# vb.net excel excel-interop

4
推荐指数
1
解决办法
2017
查看次数

无法转换"Microsoft.Office.Interop.Excel.WorksheetClass"类型的COM对象

我正在尝试从一堆聚合数据中创建并在C#中保存新的Excel文档.现在,我只是试图让基础知识工作到编辑表格单元格等等.这是我只是尝试设置一个单元格的内容:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Office.Interop.Excel;

namespace DebugReport
{
    class MonumentDebugReport
    {
        static void Main(string[] args)
        {    
            Worksheet spreadsheet = new Worksheet();

            spreadsheet.Cells[0, 0] = "stuff";
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

返回:

无法将"Microsoft.Office.Interop.Excel.WorksheetClass"类型的COM对象强制转换为接口类型"Microsoft.Office.Interop.Excel._Worksheet".此操作失败,因为由于以下错误,对IID为"{000208D8-0000-0000-C000-000000000046}"的接口的COM组件的QueryInterface调用失败:不支持此类接口(HRESULT异常:0x80004002(E_NOINTERFACE)) .

在我设置单元格的行上.对我来说,这看起来与我见过的大多数示例/教程完全一样,而且我在C#文档中找不到任何表明这不起作用的内容.

c# excel

4
推荐指数
1
解决办法
1万
查看次数

如何处置我的 Excel 应用程序

我的代码如下

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(file);

Excel.Worksheet xlSheet = xlWorkbook.Sheets[1]; // get first sheet
Excel.Range xlRange = xlSheet.UsedRange;
Run Code Online (Sandbox Code Playgroud)

这些是我的函数中使用的唯一变量

foreach (Excel.Worksheet XLws in xlWorkbook.Worksheets)
{
    // do some stuff 

    xlApp.UserControl = false;

    if (xlRange != null)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlRange);

    if (xlSheet != null)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlSheet);

    if (xlWorkbook != null)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkbook);

    xlRange = null;
    xlSheet = null;
    xlWorkbook = null;
    xlApp.Quit();

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp);
}
Run Code Online (Sandbox Code Playgroud)

但我仍然在任务管理器中得到EXCEL.EXE

请帮忙?

c# excel automation

3
推荐指数
2
解决办法
1万
查看次数

发布Excel COM对象

我有一些使用Excel COM对象的Excel自动化代码.从其他SO帖子我知道应该尽快释放对象:

Excel.Range rng = GetSomeRange();

// do something with rng...

Marshal.ReleaseComObject(rng);
Run Code Online (Sandbox Code Playgroud)

但是,如果我在一个范围内循环遍历单元格,则以下是正确的过程吗?

Excel.Range rng = GetSomeRange();
foreach (Excel.Range r in rng)
{
    // do something with r
    Marshal.ReleaseComObject(r); // release at earliest possible convenience
}

Marshal.ReleaseComObject(rng);
Run Code Online (Sandbox Code Playgroud)

什么我不能确定这里是,如果我每次释放rrng,然后我也释放rng我在有效释放rng两次或者释放正确其他参考资料rrngrng自己?

谢谢你!

编辑

我选择了后一种策略:

Excel.Range rng = GetSomeRange();
foreach (Excel.Range r in rng)
{
    // do something with r
    Marshal.ReleaseComObject(r); // release at earliest possible convenience
} …
Run Code Online (Sandbox Code Playgroud)

c# com-interop excel-addins excel-automation

3
推荐指数
1
解决办法
609
查看次数

使用 VB.NET 获取正在运行的 Excel 实例

我从这个答案中获取了以下工作代码:

Option Compare Binary
Option Explicit On
Option Infer On
Option Strict Off

Imports Microsoft.Office.Interop
Imports System.Collections.Generic
Imports System.Runtime.InteropServices

Friend Module Module1
    Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
    Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowsProc, ByVal lParam As IntPtr) As Boolean
    Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
    Private Delegate Function EnumWindowsProc(ByVal hwnd As …
Run Code Online (Sandbox Code Playgroud)

vb.net com excel exception excel-interop

3
推荐指数
1
解决办法
7430
查看次数