如何使用Microsoft.Office.Interop.dll 11.0版本dll在C#.Net中读取Office 2003 [英] How to Read Office 2003 exce in C#.Net using Microsoft.Office.Interop.dll 11.0 version dll

查看:118
本文介绍了如何使用Microsoft.Office.Interop.dll 11.0版本dll在C#.Net中读取Office 2003的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我们正在用c#.Net开发一个应用程序。

我们正在使用Office2003,VS 2010和4.0 Framework。

在我的应用程序Excel文件中阅读,编写和创建工作表。

我们在开发中遇到问题。

1.在阅读时阅读Excel文件。我们尝试手动打开另一个文件(不是读取文件)然后自动打开读取excel文件以及手动打开excel文件。如何防止app阅读excel文件打开。我们尝试了很多方法但我们没有找到解决方案。

2.处理我们使用Quit()方法的excel应用程序对象。这个方法关闭所有excel对象,甚至手动打开excel文件。但是我们不想关闭手动打开的excel文件。



任何人都可以帮助我们。解决这个问题。这对我们来说非常重要。



这是我们阅读excel文件的一种方法





Hi,

We are developing one Application in c#.Net.
we are using Office2003 , VS 2010 and 4.0 Framework.
In My Application Excel files reading ,Writing and creating sheets.
we have problems in development.
1. At the time of App Reading Excel file. We are trying to open another file (not reading file) by manually then Automatically opening reading excel file along with manually opening excel file. how to prevent app reading excel file opening. we tried so many ways but we did not find solution.
2. to dispose the excel application object we are using Quit() method. this method closing all the excel objects even manually opened excel files also. But we don''t wants close manually opened excel files.

Can any one help us . to resolve this issues. this is very important to us.

this is our one method to read excel file


public List serviceCall()
{
    List returnString = new List();
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Workbook xlWBook;
    Worksheet xlWSheet;
    Range xlRange;

    try
    {
        writer.WriteLine(" Step 1 --- Excel file reading started..........");
        xlWBook = xlApp.Workbooks.Open(@"E:\temp.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlApp.Visible = false;

        xlWSheet = (Worksheet)xlWBook.Worksheets.get_Item("sheet one");
        xlRange = xlWSheet.UsedRange;

        for (int r = 1; r <= xlRange.Rows.Count; r++)
        {

            object value = (xlRange.Cells[r, 3] as Range).Value2;

            returnString.Add(value == null ? "" : value.ToString());
        }

        releaseObject(xlWSheet);

        xlWSheet = null;

        releaseObject(xlWBook);

        xlWBook = null;

        releaseObject(xlApp);
    }
    catch (Exception ex)
    {
    }
    finally
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    return returnString;
}

 private void releaseObject(object obj)
 {
     try
     {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
     }
     catch (Exception ex)
     {
        obj = null;
     }
     finally
     {
        GC.Collect();
     }
 }




推荐答案

我想你想说的是你在Excel中打开temp.xls,而你试图再次打开文件而不使用只读模式? Excel会跟踪打开的文件和模式。它将允许在只读模式下多次打开文件,但只有一个文件可以在写入模式下打开。获得写访问权限的是第一个打开它的人。如果您需要具有该控件,则需要编写跟踪打开/关闭引用的代码。您还可以保持应用程序打开并包装Excel API,以便可以对同一个应用程序实例进行多次调用(这就是我所做的)。





我没看到你实际上在哪里.Quit()。因为每次调用新的Microsoft.Office.Interop.Excel.Application()时我们实际上都在创建一个新的Excel进程,所以我们必须关闭这些进程,否则它们将堆积在内存中。当我们将Visible设置为false时,这可能是一个特别大的问题。



在调用releaseObject(xlWSheet)之前尝试添加这三行;

I think what you are trying to say is that you have temp.xls open in Excel, and you are trying to open the file again without using read only mode? Excel keeps track of what files are open and in what modes. It will allow the file to be opened multiple times in read only mode, but only one can have it open in write mode. The one that gets write access will be the first one that opens it. If you need to have that control, you will need to write code that tracks references for open/closed. You could also keep the application open and wrap the Excel API so that multiple calls can be made to the same application instance (this is what I do).


I don''t see where you are actually calling .Quit(). Because we are actually creating a new Excel process every time we call new Microsoft.Office.Interop.Excel.Application(), we have to shut down those processes or they will stack up in memory. This can be an especially big problem when we are setting Visible to false.

Try adding these three lines before you call releaseObject(xlWSheet);
object missing = Type.Missing; // missing can replace of many of the API params
xlWBook.Close(missing, missing, missing); // close the workbook 
xlApp.Quit(); // close Excel


这篇关于如何使用Microsoft.Office.Interop.dll 11.0版本dll在C#.Net中读取Office 2003的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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