下面的代码采用类2500毫秒上的i7- * 3.4 GHz的窗口-7 64位计算机与25000行和5列读取的Excel工作表。 每个小区大致包括具有10个字符的字符串。 它是正常的吗? 我怎样才能阅读速度?
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);
我想提出我的研究结果作为答案,因为该行为是始终保持一致。
我抄你的代码,并把一个按钮单击事件中,只是改变了一下,以确保处置适配器和取得每个测试的连接。
// 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);
}
}
所以,这基本上是你的代码。 此代码执行500毫秒。 但.... 如果我保持文件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()