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

时间:2018-04-02 22:06:17

标签: c# excel ado.net oledb oledbconnection

我们有一些代码可以打开并加载Excel(XLSX和XLS)文件中的数据。我们将OleDbConnection与Microsoft Access数据库引擎(ACE)一起使用。

当有问题的Excel文件位于网络共享上且用户在Excel中打开文件时,此代码偶尔会抛出异常。我着手试图解决这个问题,我的假设是我可以添加一个设置来连接字符串以配置只读访问。

在我的研究中,我发现这个问题多次提出,并回答了各种推荐的解决方案。不幸的是,我发现它们都不起作用,我找不到关于Excel的连接字符串设置的任何官方Microsoft文档。

我开始认为我想做的事情是不可能的,并且会感激任何帮助。

这是我的测试代码:

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();
    }
}

当目标文件在网络共享上的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.

0 个答案:

没有答案
相关问题