简体   繁体   English

C#如何使用Interop从Excel读取和写入数据,而不显示Excel?

[英]C# How to read and write data from Excel using Interop, without making Excel visible?

When opening a workbook from 2 .xls files, the workbooks become visible in the background. 从2 .xls文件打开工作簿时,工作簿在后台显示。 This is undesirable in general as it does not look nice, but it also slows down the program. 这通常是不可取的,因为它看起来不太好,但它也会减慢程序的速度。

Questions regarding this have been asked before, but none of the answers solved the problem. 之前已经提出过有关此问题的问题,但没有一个答案解决了这个问题。 The way I am reading data from the .xls file is as follows; 我从.xls文件中读取数据的方式如下;

xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkbook1 = xlApp.Workbooks.Open(path1);
xlWorksheet1 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook1.Worksheets.get_Item(1);
xlWorkbook2 = xlApp.Workbooks.Open(path2);
xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook2.Worksheets.get_Item(1);

I have tried the following ways to open the workbooks, while preventing them from becoming visible; 我尝试了以下方法来打开工作簿,同时防止它们变得可见;

1. 1。

xlApp = new Microsoft.Office.Interop.Excel.Application() { Visible = false };
xlWorkbook1 = xlApp.Workbooks.Open(path1);
xlWorksheet1 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook1.Worksheets.get_Item(1);
xlWorkbook2 = xlApp.Workbooks.Open(path2);
xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook2.Worksheets.get_Item(1);

2. 2。

xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
xlApp.DisplayAlerts = false;
xlWorkbook1 = xlApp.Workbooks.Open(path1);
xlWorksheet1 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook1.Worksheets.get_Item(1);
xlWorkbook2 = xlApp.Workbooks.Open(path2);
xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook2.Worksheets.get_Item(1);

3. 3。

xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkbook1 = xlApp.Workbooks.Open(path1);
xlWorksheet1 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook1.Worksheets.get_Item(1);
xlWorkbook2 = xlApp.Workbooks.Open(path2);
xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkbook2.Worksheets.get_Item(1);
xlApp.Visible = false;
xlApp.DisplayAlerts = false;

Method 3 does hide the workbooks, but they are opened first and therefore does not solve the problem. 方法3隐藏了工作簿,但它们首先打开,因此无法解决问题。 I have also tried to open the workbooks as read-only, but that does not seem to make a difference. 我也尝试以只读方式打开工作簿,但这似乎没有什么区别。 I need to be able to write to the workbooks as well, so opening them as read-only is no option. 我也需要能够写入工作簿,因此以只读方式打开它们是不可取的。 It did not fix the problem anyway, but I'm just stating it so it will not be suggested as the solution. 它无论如何都没有解决问题,但我只是说明它所以它不会被建议作为解决方案。

Does anyone know how to achieve the desired behavior? 有谁知道如何实现理想的行为?

NOTE: I am using Windows 10. 注意:我使用的是Windows 10。

I used the NetOffice.ExcelApi for reading Excel files. 我使用NetOffice.ExcelApi来读取Excel文件。 I created a class for this. 我为此创建了一个类。 This should not show the Excel file or Excel itself. 这不应显示Excel文件或Excel本身。 Don't forget to close and dispose the file, otherwise Excel will keep running in the background and that could crash your PC. 不要忘记关闭并处理文件,否则Excel将继续在后台运行,这可能会导致PC崩溃。

using NetOffice.ExcelApi;

public class ExcelFile
{
    private Application _excelApp;
    private readonly Workbook _workbook;
    private Range _range;

    private ExcelFile(string path)
    {
        _excelApp = new Application();
        _workbook = _excelApp.Workbooks.Open(path);
    }

    private void CloseFile()
    {
        _workbook.Close(0);
        _excelApp.Quit();
        _excelApp.Dispose();
    }
}

Apparently, the file was in sharing mode and this made the file show up. 显然,该文件处于共享模式,这使文件显示出来。 I have turned sharing mode off and now its fixed. 我已关闭共享模式,现在已修复。

xlApp.Visible = false;

尝试使用此代码

暂无
暂无

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

相关问题 C# COM Interop Excel:如何使用 Interop Excel 从 C# 写入单元格? - C# COM Interop Excel: How to write to cells from C# using Interop Excel? 如何在不使用 Microsoft.Office.Interop.Excel 库的情况下在 C# 中读取 excel 文件 - How to read an excel file in C# without using Microsoft.Office.Interop.Excel libraries 如何使用C#Excel Interop读取Excel列表元素(数据验证)? - How to read excel list elements (data validation) using C# Excel Interop? 如何使用 C# 中的 interop.excel 从 excel 文件中删除只读模式 - How to remove read-only mode from excel file using interop.excel in C# 1)如何将Excel写入memorystream…2)如何使用Micosoft.Office.Interop.Excel和c#从浏览器下载Excel文件 - 1) how to write excel to memorystream…2) how to download excel file from browser…using Micosoft.Office.Interop.Excel and c# 如何使用 Interop 在 C# 中读取受保护视图的 Excel 文件? - How to read Excel file that is protected view in C# using Interop? 如何在不使用互操作的情况下通过c#取消隐藏excel表 - How to unhide an excel sheet through c# without using interop C#如何使用互操作从多个工作表Excel中读取指定的单元格? - C# how to read specifed cells from multiple sheet excel using interop? C#Excel Interop:无法从Excel读取所有数据 - C# Excel Interop: can't read all data from excel 如何使用c#从XML文件读取特定数据并将其写入到现有Excel工作表 - How to Read a particular Data from XML file and Write it to an Existing Excel Sheet using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM