通过减less导出时间来加快db 2 Excel代码

我的table.Rows.Count = 8000.在C#vs05上工作

DataSet ds = null; DataTable dtTable = null; ds = oBOGeneralReport.GetsBoardExcelPreview(_oGeneralReport);//.GetsBoardPreview(_oGeneralReport); if (ds == null) return; if (ds.Tables.Count > 0) dtTable = ds.Tables[0]; if (dtTable == null) return; string connectionString = null; string sql = null; string data = null; int i = 0; int j = 0; Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int nRow = 1; int p = 0; int k = 0; string sFileName = null; for (k = 0; k < ds.Tables[0].Columns.Count; k++) { xlWorkSheet.Cells[nRow, p + 1] = ds.Tables[0].Columns[k].ColumnName; p++; } for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++) { data = ds.Tables[0].Rows[i].ItemArray[j].ToString(); xlWorkSheet.Cells[i + 2, j + 1] = data; } } SaveFileDialog oDialog = new SaveFileDialog(); oDialog.Filter = "Excel files | *.xls"; if (oDialog.ShowDialog() == DialogResult.OK) { sFileName = oDialog.FileName; } if (sFileName != null) { xlWorkBook.SaveAs(sFileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); MessageBox.Show("Report saved with file: " + sFileName, "To Excel", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Cannot export to excel...", "Can't export", MessageBoxButtons.OK, MessageBoxIcon.Error); } releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); 

如何减less出口时间?

SpreadsheetGear for .NET在我近乎2年的超频英特尔QX6850上,在0.039秒内创build了一个8000行×10列(你没有说多less列)工作簿。

我跑的代码在下面。 如果您想亲自尝试,可以在这里下载免费试用版。

免责声明:我自己的SpreadsheetGear LLC。

 using System; using System.Diagnostics; using SpreadsheetGear; using SpreadsheetGear.Advanced.Cells; namespace WriteRows { class Program { static void Main(string[] args) { // "Warm up" the code. WriteRows(100, 10, false); // Do the test. WriteRows(8000, 10, false); } static void WriteRows(int rows, int cols, bool openXML) { Stopwatch timer = Stopwatch.StartNew(); IWorkbook workbook = Factory.GetWorkbook(); IWorksheet worksheet = workbook.Worksheets[0]; IValues values = (IValues)worksheet; for (int row = 0; row < rows; row++) { values.SetText(row, 0, "Row " + (row + 1)); for (int col = 1; col < cols; col++) values.SetNumber(row, col, row * cols + col); } string filename = @"c:\tmp\WriteRows" + (openXML ? ".xlsx" : ".xls"); FileFormat fileFormat = openXML ? FileFormat.OpenXMLWorkbook : FileFormat.Excel8; workbook.SaveAs(filename, fileFormat); Console.WriteLine("Created {0} with {1}x{2} rows / cols in {3} seconds", filename, rows, cols, timer.Elapsed.TotalSeconds); } } } 

SpreadsheetGear.NET也是我最喜欢的解决scheme,但是假设你没有钱来授权它,最大的速度问题是你发送给Excel的命令的数量 – 每个命令的Excel OLE自动化开销是往往大于实际的执行。 写入单元格是最有说服力的情况。

要加速写入单元格,请将数据准备为数组,然后将整个范围写入一次(而不是将值分配给单元格,可以将数组分配给范围)。 用这个简单的步骤,你会注意到速度的巨大提升。

由于您正在读取ADO表,因此您可能还需要查看CopyFromRecordset命令,该命令是范围对象的一种方法(在旧版本的Excel自动化界面中不可用)。 这使您可以将logging集的全部内容一次性发送到一个单元格范围内,速度非常快。

 xlWorkSheet.Range("A1").CopyFromRecordset ds 

(这可能不是100%正确的,因为我一起做了VB和Excel自动化很长一段时间)

几个月前我有类似的问题,我从Excel放弃了…

我build议你使用Excel Jetcell .NET组件 ,它有一个免费版本,有一些格式限制。 它更快,而且使用起来非常简单…而且客户端不需要安装Office …您可以在以下位置下载此组件:

http://www.devtriogroup.com/ExcelJetcell