【问题标题】:Problems reading all the lines of an excel with OLEDB and C#使用 OLEDB 和 C# 读取 excel 的所有行时出现问题
【发布时间】:2017-06-21 04:49:44
【问题描述】:

我正在开发一个程序,用 C# 语言导入 excel 电子表格,使用 OLEDB 组件,当导入具有 100547 行的电子表格时,该程序只能读取 54046。 附上源码:

   public class ReadExcel
{

    public string ConnectionExcel(ExcelUpload excelUpload)
    {
        //connection String for xls file format.
        if (excelUpload.fileExtension == ".xls")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        //connection String for xlsx file format.
        else if (excelUpload.fileExtension == ".xlsx")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }else
        {
            excelUpload.excelConnectionString = "";
        }
        return excelUpload.excelConnectionString;
    }

    public DataTable readArqExcel(string excelConnectionString, DataSet ds)
    {
        //Create Connection to Excel work book and add oledb namespace
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        excelConnection.Open();
        DataTable dt = new DataTable();

        dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        if (dt == null)
        {
            return null;
        }

        //Numero de planilhas contidas no excel
        String[] excelSheets = new String[dt.Rows.Count];
        int count = 0;

        //excel data saves in temp file here.
        foreach (DataRow row in dt.Rows)
        {
            excelSheets[count] = row["TABLE_NAME"].ToString();
            count++;
        }

        OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
        string query = string.Format("Select * from [{0}]", excelSheets[0]);
        using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
        {
            dataAdapter.Fill(ds);
        }

        excelConnection.Close();

        return ds.Tables[0];
    }

我已经测试了 IIS 8(远程服务器)和 IIS Express(Visual Studio 本地服务器),我注意到在 IIS Express 服务器上代码运行良好,但在 IIS 8 中代码最终读取了一半的文件。 是某种网络服务器配置吗?

【问题讨论】:

  • 也许检查应用程序池中是否设置了内存限制
  • 我已签入应用程序池-> 单击池-> 高级设置-> 标签 CPU 属性:处理器关联掩码值得 4294967295 操作限制:KillW3wp 标签进程模型:属性标识:值得 localservice 属性最大值操作员进程数:21579 会是这个吗?
  • 我无法识别本地以将内存参数更改为 iis。可以发给我吗?

标签: c# iis oledb iis-8 oledbdataadapter


【解决方案1】:

问题已解决,我用 MsExcel 更改了连接字符串 OLEBD。 将参数 IMEX = 2 更改为 IMEX = 1,如下所示

if (excelUpload.fileExtension == ".xls")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
        }
        //connection String for xlsx file format.
        else if (excelUpload.fileExtension == ".xlsx")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\"";
        }

【讨论】:

    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    相关资源
    最近更新 更多