使用Microsoft.Office.Interop.Excel将速度性能导出到excel文件

我正在使用Microsoft.Office.Interop.Excel将本地数据库表导出为ex​​cel文件。 有五列。

如果行数是400,大约需要20秒,

如果行是1200,则需要大约45秒,

如果行数是5000,则需要250-300秒。

有没有办法减less出口时间? 或者这是最高性能? 如果你可以build议改善我的代码速度或build议一些替代scheme,我将不胜感激。 因为它在后台工作,所以调用是必要的。 我的代码是

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(); } } 

如果你已经习惯使用互操作,那么最好先创build一个值的数组,然后把它写出来,而不是循环行 – 互操作在这个过程中是非常慢的

就个人而言,我build议开沟互操作,并使用一个开放的XML库,如EPPlus这种types的任务。 我觉得它更容易使用,通常性能更好。 作为一个奖励,它摆脱了所有令人厌烦的凌乱发布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; }