OleDB连接在Windows Server 2012R中引发异常

时间:2016-01-14 13:46:08

标签: c# xls oledbconnection

我正在使用一个函数来打开包含多个工作表的.xls文件,并将整个内容复制到.csv文件中。 在我的本地机器上一切正常:没有例外,没有错误等。

但是当我在Windows Server 2012R上运行它时,我在连接打开时遇到异常。

以下是我尝试打开OleDB连接然后通过文件查询的代码:

static void ConvertExcelToCsv(string excelFilePath, string csvOutputFile, int worksheetNumber)
    {
        // connection string

        var cnnStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=\"Excel 8.0;HDR=no;Format=xls\"");

        var cnn = new OleDbConnection(cnnStr);

        // get schema, then data
        var dt = new DataTable();
        try
        {
            cnn.Open();
            var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            if (schemaTable.Rows.Count < worksheetNumber) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");

            string worksheet = schemaTable.Rows[worksheetNumber - 1]["table_name"].ToString().Replace("'", "");
            string sql = String.Format("select * from [{0}]", worksheet);
            var da = new OleDbDataAdapter(sql, cnn);
            da.Fill(dt);

...

excelFilePath是我的源excel文件(.xls),而csvOutputFile是要传递给内容的文件。

有没有人知道为什么我会得到这个例外?

0 个答案:

没有答案