使用Interop从DataGrid导出到Excel太慢了

我即将编写一个WPF应用程序,它从数据库中获取数据并在DataGrid中显示它。 然后使用按钮创建Excel文件并填充数据。 如20000行的大量数量,Excel中的填充需要太长时间。 有人知道为什么吗? 谢谢

private void copyAlltoClipboard() { Clipboard.Clear(); DataGrid1.SelectAllCells(); DataGrid1.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader; ApplicationCommands.Copy.Execute(null, DataGrid1); } private void Button_Click(object sender, RoutedEventArgs e) { copyAlltoClipboard(); Excel.Application xlexcel; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlexcel = new Excel.Application(); xlexcel.Visible = true; xlWorkBook = xlexcel.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); } 

这是我目前使用的方法。
但这与您的代码非常相似。 但我正在使用数据对象将内容复制到剪贴板。 这对我来说真的很好。 但是像20000这样的一些线路无论如何都会影响性能。

  private void btnExportToExcel_Click(object sender, EventArgs e) { copyDataGridToClipboard(); Microsoft.Office.Interop.Excel.Application xlexcel; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.Visible = true; xlWorkBook = xlexcel.Workbooks.Add(misValue); xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); } private void copyDataGridToClipboard() { YourDataGridView.MultiSelect = true; yourDataGridView.SelectAll(); DataObject dataObj = yourDataGridView.GetClipboardContent(); if (dataObj != null) { Clipboard.SetDataObject(dataObj); } yourDataGridView.MultiSelect = true; }