0
    string _filePath = @"C:\Users\szhao\Downloads\USER_UploadCSV.xlsx";
    FileInfo fi = new FileInfo(_filePath);
    string _connectionString = "Provider=Microsoft.JET.OLEDB.4.0;Mode=Read;Data Source=" + _filePath + "; Extended Properties=\"Excel 8.0;HDR=Yes;\"";
    if (fi.Extension.Equals(".xlsx") || fi.Extension.Equals(".xls"))
    {
        // For Excel 2007 File  Format
        _connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Data Source=" + _filePath + " ;Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
    }
    OleDbConnection _conn = new OleDbConnection(_connectionString);
    _conn.Open();
    Console.WriteLine("Connect!");

in connectionstring 应该表明这Mode=Read是只读模式,这将允许多应用程序读取单个 excel 而无需写入。但它会抛出异常_conn.Open();,所以我只是想可能是我的连接字符串是错误的?

以下是异常消息

Exception Message:
External table is not in the expected format.

Stack Trace:
   at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
   at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.OleDb.OleDbConnection.Open()
   at TIDEAPI.BLL.AExcelFile..ctor(TestAdministration pTestAdministration, String pFilePath, UploadFileSpecification pUploadFileSpecification) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\Abstraction\AExcelFile.cs:line 41
   at TIDEAPI.BLL.ExcelFile..ctor(TestAdministration pTestAdministration, String pFilePath, UploadFileSpecification pUploadFileSpecification) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\ExcelFile.cs:line 14
   at TIDEAPI.BLL.AcceptableFileFactory.GetAcceptableFile(TestAdministration pTestAdministration, String pFilePath, UploadFileSpecification pUploadFileSpecification) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\AcceptableFileFactory.cs:line 22
   at TIDEAPI.BLL.AFileProcessor..ctor(Int64 pTestAdministartionKey, String pFilePath, UploadFileSpecification pUploadFileSpecification, FrequencyDistributionLogger fdLogger, DataExchangeErrorLogger errorLogger, IDictionary`2 dataExchangeProperties) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\Abstraction\AFileProcessor.cs:line 108
   at TIDEAPI.BLL.UserFileProcessor..ctor(Int64 pTestAdministrationKey, String pTideFile, UploadFileSpecification pUploadFileSpecification, FrequencyDistributionLogger fdLogger, DataExchangeErrorLogger errorLogger, IDictionary`2 dataExchangeProperties) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\UserFileProcessor.cs:line 32
   at TIDEAPI.BLL.FileProcessorFactory.GetFileProcessor(Int64 pTestAdministrationKey, String pFilePath, UploadFileSpecification pUploadFileSpecification, FrequencyDistributionLogger fdLogger, DataExchangeErrorLogger errorLogger, FileProcessorTypes pFileProcessorType, IDictionary`2 dataExchangeProperties) in c:\Projects\PreProcessing\TIDE\Online\TIDEAPI\BLL\FileUpload\FileProcessorFactory.cs:line 42
   at PreIdService.PreIdFile.Load(Object pWorkerThreadParameters) in c:\Projects\PreProcessing\TIDE\Online\Tools\TideBatchUploadService\PreIdFile.cs:line 62

如果我删除 Mode=Read,它将不允许多个应用程序读取,但单个用户可以工作。

4

1 回答 1

1

xls 和 xlsx 的 ConnectionString 是不同的。

if (fi.Extension.Equals(".xls"))
{
   _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
       _filePath + " ;Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";
}
else if (fi.Extension.Equals(".xlsx"))
{
   _connectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + 
       _filePath + " ;Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\"";
}
于 2013-09-25T23:04:39.650 回答