OLEDB读取Excel的性能

以下代码在i7- * 3.4 GHz windows-7 64位计算机上需要2500毫秒的时间读取25000行和5列的Excel表格。 每个单元格大约包含一个有10个字符的string 这是正常的吗? 我怎样才能更快地读取它?

Stopwatch sw1 = Stopwatch.StartNew(); var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; " + "Extended Properties=Excel 12.0;", filename); var adapter = new OleDbDataAdapter("SELECT * FROM [roots$]", connectionString); var ds = new DataSet(); adapter.Fill(ds, "roots"); sw1.Stop(); Console.WriteLine("Time taken for excel roots: {0} ms", sw1.Elapsed.TotalMilliseconds); 

我想提出我的发现作为答案,因为行为总是一致的。

我已经复制你的代码,并把一个button点击事件,只是改变了一下,以确保configuration适配器和每个testing的连接。

 // test.xls contains 26664 rows by 5 columns. Average 10 char for column, file size is 2448kb // OS Windows 7 Ultimate 64 bit. CPU Intel Core2 Quad Q9550 2.83ghz // 8gb ram and disk C is an 256gb SSD cruzer private void button1_Click(object sender, EventArgs e) { string filename = "c:\\tmp\\test.xls"; Stopwatch sw1 = Stopwatch.StartNew(); var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; " + "Extended Properties=Excel 12.0", filename); using(var adapter = new OleDbDataAdapter("SELECT * FROM [roots$]", connectionString)) { var ds = new DataSet(); adapter.Fill(ds, "roots"); sw1.Stop(); Console.WriteLine("Time taken for excel roots: {0} ms", sw1.Elapsed.TotalMilliseconds); } } 

所以,这基本上是你的代码。 这段代码在500ms内执行。 但是…. 如果我保持文件test.xls在Excel 2010中打开,执行时间跳转到8000ms。

我也试过这个代码的变化,但最终的结果是一样的

  private void button1_Click(object sender, EventArgs e) { string filename = "c:\\tmp\\test.xls"; Stopwatch sw1 = Stopwatch.StartNew(); var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; " + "Extended Properties=Excel 12.0", filename); using(OleDbConnection cn = new OleDbConnection(connectionString)) { cn.Open(); using(var adapter = new OleDbDataAdapter("SELECT * FROM [roots$]", cn)) { var ds = new DataSet(); adapter.Fill(ds, "roots"); sw1.Stop(); Console.WriteLine("Time taken for excel roots: {0} ms", sw1.Elapsed.TotalMilliseconds); } } } 

不,它不是OleDbConnection的Open(),总是adapter.Fill()