将excel表更快地复制到DataTable中

时间:2015-06-05 20:48:33

标签: c# .net excel datatable

我正在尝试将包含1百万条记录的Excel工作表复制到数据表中。不幸的是,完成此过程大约需要47秒。有没有更好的方法在更短的时间内复制这些信息?

以下是将信息移植到的代码: fflush

String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                            fileName +
                             ";Extended Properties='Excel 12.0 XML;';";

                        OleDbConnection con = new OleDbConnection(constr);
                        OleDbCommand oconn = new OleDbCommand("Select * From ["           + sheetName + "$]", con);
                        con.Open();

                        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);

                        dtTemp.Reset();
                        dtTemp.TableName = userUpload;

                        sda.Fill(dtTemp); //Puts imported table into the dtTemp table
                        con.Close();
                        string fields = "";
                        string tempString;

                        foreach (DataColumn col in dtTemp.Columns) //Generates SQL String from imported table
                        {
                            tempString = RemoveSpecialCharacters(col.ColumnName);
                            if (dtTemp.Columns.IndexOf(col) == dtTemp.Columns.Count - 1)
                            {
                                fields += " [" + tempString + "] varchar(255)";
                            }
                            else
                            {
                                fields += " [" + tempString + "] varchar(255)" + ",";
                            }

                        }

                        fields = fields.Trim();
                        // createSQLConn(fields, connection, connectionTempDB, dtTemp);
                    }

0 个答案:

没有答案