我正在使用Microsoft.Office.Interop.Excel将本地数据库表导出到excel文件。有五列。
如果行为400,则大约需要20秒,
如果行是1200,则大约需要45秒,
如果行是5000,则需要250-300秒。
有没有一种方法可以减少出口时间?还是这是最大的表现?如果您可以建议在速度方面改进我的代码或提出其他选择,我将不胜感激。由于它是在后台工作的,因此调用是必要的。我的代码是
int rowCount = oLXTableDataGridView.RowCount;
if (rowCount == 1)
{
MessageBox.Show("No data to export.");
return;
}
this.Invoke((MethodInvoker)delegate
{
this.ExportFilepictureBox.Image = Properties.Resources.Animation;
labelexpoertolx.Text = "Preparing to export...";
});
object misValue = System.Reflection.Missing.Value;
conn = new SqlCeConnection(@"Data Source=|DataDirectory|\dontdelete.sdf");
String selectgroup = "SELECT * FROM OLXTable";
int namecol = 1;
int cellcol = 2;
int emailcol = 3;
int citycol = 4;
int categorycol = 5;
try
{
Excel.Application xlApp1;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlApp1 = new Excel.Application();
xlWorkBook = xlApp1.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
// MessageBox.Show("this is file");
xlWorkSheet.Cells[1, namecol].Value2 = "Name";
xlWorkSheet.Cells[1, cellcol].Value2 = "Cell No";
xlWorkSheet.Cells[1, emailcol].Value2 = "Email";
xlWorkSheet.Cells[1, citycol].Value2 = "City";
xlWorkSheet.Cells[1, categorycol].Value2 = "Category";
SqlCeDataReader reader = null;
//conn = new SqlCeConnection(selectnumbers);
conn.Open(); //open the connection
SqlCeCommand selecectnumberscmd = new SqlCeCommand(selectgroup, conn);
reader = selecectnumberscmd.ExecuteReader();
int i = 1;
while (reader.Read())
{
xlWorkSheet.Cells[i, namecol].Value2 = reader.GetString(1);
xlWorkSheet.Cells[i, cellcol].Value2 = reader.GetInt64(2);
xlWorkSheet.Cells[i, emailcol].Value2 = reader.GetString(3); ;
xlWorkSheet.Cells[i, citycol].Value2 = reader.GetString(4);
xlWorkSheet.Cells[i, categorycol].Value2 = reader.GetString(5);
i++;
}
conn.Close();
xlWorkBook.Close(true, misValue, misValue);
xlApp1.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp1);
this.Invoke((MethodInvoker)delegate
{
this.ExportFilepictureBox.Image = null;
labelexpoertolx.Text = "";
});
}
catch (Exception e13)
{
MessageBox.Show("Error Exporting data" + e13.Message);
conn.Close();
}
Cursor.Current = Cursors.Default;
和
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
如果您不愿意使用互操作,那么最好先创建一个值数组,然后一次臭味写出来,而不要遍历行-互操作在这方面很慢,这是众所周知的
个人,我建议放弃互操作,并使用开放的xml库(例如EPPlus)进行此类任务。我发现它更易于使用,而且性能更高。作为奖励,它摆脱了所有令人讨厌的杂乱发布com对象元帅的问题;-)
这将用以下内容替换try
块中的所有内容:
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add("Data");
ws.Cells[1, namecol].Value = "Name";
ws.Cells[1, cellcol].Value = "Cell No";
ws.Cells[1, emailcol].Value = "Email";
ws.Cells[1, citycol].Value = "City";
ws.Cells[1, categorycol].Value = "Category";
SqlCeDataReader reader = null;
conn.Open(); //open the connection
SqlCeCommand selecectnumberscmd = new SqlCeCommand(selectgroup, conn);
reader = selecectnumberscmd.ExecuteReader();
int i = 1;
while (reader.Read())
{
ws.Cells[i, namecol].Value = reader.GetString(1);
ws.Cells[i, cellcol].Value = reader.GetInt64(2);
ws.Cells[i, emailcol].Value = reader.GetString(3); ;
ws.Cells[i, citycol].Value = reader.GetString(4);
ws.Cells[i, categorycol].Value = reader.GetString(5);
i++;
}
conn.Close();
//Now you have options to export the file - just save someplace, get a byte array or get a reference to the output stream etc with some of the following:
package.SaveAs(someFilePathString);
someByteArrayVariable = package.GetAsByteArray();
package.Stream;
}
作为一种替代方案,您可以使用SwiftExcel库(免责声明:我写了该库),就像EPPlus一样,它不使用互操作,但是具有更好的性能,因为它直接将数据流传输到文件中,速度更快,并且没有任何内存影响大量采购。
这里是Nuget命令,将其添加到您的项目中:
PM> Install-Package SwiftExcel
这是如何使用它:
using (var ew = new ExcelWriter("C:\\temp\\test.xlsx"))
{
for (var row = 1; row <= 100; row++)
{
for (var col = 1; col <= 10; col++)
{
ew.Write($"row:{row}-col:{col}", col, row);
}
}
}