Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 批量读取excel文件数据_C# - Fatal编程技术网

C# 批量读取excel文件数据

C# 批量读取excel文件数据,c#,C#,我有一个excel文件,其中包含大约150万条记录。我的目的是将excel文件中的前100000条记录读取到我的datatable c datatable中,即dt对这些记录进行一些处理,然后读取接下来的100000条记录,依此类推,直到我获取excel文件中的所有记录。每次仅读取100000行 目前我正在使用下面的代码获取所有记录 public bool ReadDataFile(string filePath) { string strConnString

我有一个excel文件,其中包含大约150万条记录。我的目的是将excel文件中的前100000条记录读取到我的datatable c datatable中,即dt对这些记录进行一些处理,然后读取接下来的100000条记录,依此类推,直到我获取excel文件中的所有记录。每次仅读取100000行

目前我正在使用下面的代码获取所有记录

     public bool ReadDataFile(string filePath)
    {
        string strConnString = null;
        string sheetName = null;
        string ErrSheetName = null;

        DataSet dsEx = new DataSet();


        strConnString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + filePath + ";Mode=ReadWrite;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
        OleDbConnection conn = new OleDbConnection(strConnString);
        try
        {
            conn.Open();

            DataTable DtSheetName = new DataTable();
            DataTable dt = new DataTable();
            DtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            for (int i = 0; i <= DtSheetName.Rows.Count - 1; i++)
            {
                sheetName = DtSheetName.Rows[i]["TABLE_NAME"].ToString();
                if (sheetName.Length >= 4)
                {

                    conn.Close();

                    conn.Open();

                    using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "]", conn))
                    {

                        using (OleDbDataAdapter da = new OleDbDataAdapter())
                        {
                            da.SelectCommand = cmd;
                            try
                            {

                                da.Fill(dt);
                                dt.TableName = sheetName.Replace("$", "");

                            }
                            catch (Exception ex)
                            {
                                ErrSheetName = ErrSheetName + "," + sheetName.Replace("$", "");
                            }
                        }

                    }

                }
            }
            return true;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "ReadExcel()");
            return false;
        }
        finally
        {
            if (!string.IsNullOrEmpty(ErrSheetName))
            {
                 MessageBox.Show("Error in Data Of these files [" + ErrSheetName + "] Some Data may be lost !", "ReadExcel()");
            }

            conn.Close();
            GC.Collect();
        }
    }

请澄清问题的确切含义。我希望每次仅从我的应用程序中的excel文件中获取100000条记录。可能的副本