简体   繁体   English

使用 OleDb 以只读模式打开 Excel 文件?

[英]Open an Excel file in read-only mode using OleDb?

We have some code that opens and loads data from Excel (XLSX and XLS) files.我们有一些代码可以打开 Excel(XLSX 和 XLS)文件并从中加载数据。 We use an OleDbConnection with Microsoft Access Database Engine (ACE).我们将 OleDbConnection 与 Microsoft Access 数据库引擎 (ACE) 结合使用。

Occasionally this code throws an exception when the Excel file in question is on a network share and a user has the file open in Excel.当有问题的 Excel 文件位于网络共享上并且用户在 Excel 中打开该文件时,有时此代码会引发异常。 I set about trying to fix this, and my assumption was that there was a setting I could add to the connection string to configure read-only access.我着手尝试解决这个问题,我的假设是我可以将一个设置添加到连接字符串以配置只读访问。

In my research I found this question posed many times, and answered with various recommended solutions.在我的研究中,我发现这个问题多次提出,并用各种推荐的解决方案进行了回答。 Unfortunately, I find that none of them work, and I can't find any official Microsoft documentation on connection string settings for Excel.不幸的是,我发现它们都不起作用,而且我找不到任何关于 Excel 连接字符串设置的官方 Microsoft 文档。

I'm starting to think that want I want to do is not possible, and would appreciate any help.我开始认为我想做的事情是不可能的,并且会很感激任何帮助。

Here is my test code:这是我的测试代码:

const string excelFile = @"\\server\folder\file.xlsx";

var connStrings = new[] {
    // Base, no "read only" configuration
    $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={excelFile};Extended Properties=\"Excel 12.0;IMEX=1\"",

    // Mode=Read
    //
    // C# ace oledb 12 read-only file
    // https://stackoverflow.com/questions/45165570/c-sharp-ace-oledb-12-read-only-file
    //
    // OleDbConnection Read Only Mode
    // https://social.msdn.microsoft.com/Forums/office/en-US/498cd52a-b0ee-4c8d-8943-2b76055b4130/oledbconnection-read-only-mode?forum=accessdev
    $"Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Data Source={excelFile};Extended Properties=\"Excel 12.0;IMEX=1\"",

    // READONLY=TRUE (and variations) in Extended Properties
    //
    // Excel source read only?
    // https://social.msdn.microsoft.com/Forums/sqlserver/en-US/d03e4b1a-6be0-4b3c-8b31-42d6fc79bf39/excel-source-read-only?forum=sqlintegrationservices
    //
    // Working with MS Excel(xls / xlsx) Using MDAC and Oledb
    // https://www.codeproject.com/Articles/37055/Working-with-MS-Excel-xls-xlsx-Using-MDAC-and-Oled
    $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={excelFile};Extended Properties=\"Excel 12.0;IMEX=1;READONLY=TRUE\"",
    $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={excelFile};Extended Properties=\"Excel 12.0;IMEX=1;ReadOnly=true;\"",
    $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={excelFile};Extended Properties=\"Excel 12.0;IMEX=1;MODE=READ;READONLY=TRUE\"",

    // Wild guesses
    $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={excelFile};Extended Properties=\"Excel 12.0;IMEX=1;READONLY=1\"",
    $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={excelFile};Extended Properties=\"Excel 12.0;IMEX=1;MODE=READ;READONLY=1\""
};

for ( var i = 0; i < connStrings.Length; i++ ) {
    var conn = new OleDbConnection( connStrings[i] );

    try {
        conn.Open();
        Console.WriteLine( $"{i}: Success" );
        conn.Close();
    }
    catch ( OleDbException ex ) {
        Console.WriteLine( $"{i}: FAIL: {ex.Message}" );
    }
    finally {
        conn.Dispose();
    }
}

When the target file is open in Excel on a network share, all connection string variations fail like so:当目标文件在网络共享的 Excel 中打开时,所有连接字符串变体都会失败,如下所示:

0: FAIL: The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.
1: FAIL: The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.
2: FAIL: The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.
3: FAIL: The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.
4: FAIL: The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.
5: FAIL: The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.
6: FAIL: The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.

You can create a copy of the file to bypass the read-only access.您可以创建该文件的副本以绕过只读访问。

Try File.Copy() method from System.IO namespace to Copy your Excel to another location like C:/temp.尝试使用System.IO命名空间中的File.Copy()方法将 Excel 复制到另一个位置,例如 C:/temp。

And then use OLEDB to Open it to read and Close/Delete it然后使用 OLEDB 打开它来读取和关闭/删除它

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM