1: 用第三方控件的导出方法时速度很快,自己写的速度慢,后发现将数据写到数组,再将数组数据写入EXCEL的RANGE速度较快
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Dim objData(,) As Object = New Object(RowCount + 1, ColCount) {}
Dim cs As DataGridViewColumn
For Each cs In myDGV.Columns
objData(RowIndex, colIndex) = myDGV.Columns(colIndex).HeaderText
colIndex += 1
Next
For RowIndex = 0 To RowCount - 1
For colIndex = 0 To ColCount - 1
objData(RowIndex + 1, colIndex) = myDGV.Rows(RowIndex).Cells(colIndex).Value
Next
Next
range.Value2 = objData
System.Windows.Forms.Application.DoEvents()
Dim cs As DataGridViewColumn
For Each cs In myDGV.Columns
objData(RowIndex, colIndex) = myDGV.Columns(colIndex).HeaderText
colIndex += 1
Next
For RowIndex = 0 To RowCount - 1
For colIndex = 0 To ColCount - 1
objData(RowIndex + 1, colIndex) = myDGV.Rows(RowIndex).Cells(colIndex).Value
Next
Next
range.Value2 = objData
System.Windows.Forms.Application.DoEvents()
2: 导出EXCEL后,可能EXCEL会对显示格式进行自动转换,例如较长的数字可能会按科学计数法来显示,加一条语句后OK
range.NumberFormat = Microsoft.Office.Interop.Excel.XlParameterDataType.xlParamTypeUnknown
3: EXCEL资源释放的问题
微软推荐为
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
'ReleaseComObject(oCells)
ReleaseComObject(range)
ReleaseComObject(oSheet)
ReleaseComObject(oSheets)
ReleaseComObject(oBook)
ReleaseComObject(oBooks)
ReleaseComObject(oExcel)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
ReleaseComObject(range)
ReleaseComObject(oSheet)
ReleaseComObject(oSheets)
ReleaseComObject(oBook)
ReleaseComObject(oBooks)
ReleaseComObject(oExcel)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
注意声明的时候也要一层一层按顺序声明