asp.net com excel利用数组写入excel 效率提高
2008-09-10 15:34
274 查看
1.利用数组直接赋值给range,速度明显快多了。
Public Sub WriteToExcelFile(ByVal FileName As String, ByVal intRows As Integer, ByVal StrSql As String)
Dim FilePath As String = "F:\System\SCS\Programes\SCSWeb\UploadKeepQty\Download\"
Dim FileNamePathType As String = FilePath + FileName + ".xls"
Dim FileNamePath As String = FilePath + FileName
FileExist(FileNamePathType, True)
Dim ExcelApp As New Excel.Application
ExcelApp.Visible = False
Dim ExcelWorkBook As Excel.Workbook = ExcelApp.Workbooks.Add()
' Dim ExcelSheet As Excel.Worksheet = ExcelWorkBook.Worksheets.Add()
Dim ExcelSheet As Excel.Worksheet = ExcelWorkBook.Worksheets(1)
CreateHeader(ExcelSheet)
Dim i As Integer
' "A" & 2 & " 是从哪里开始写入,"C" 结束的列," intRows + 1"是数据结束的行数
Dim ObjRange As Object = ExcelSheet.Range("A" & 2 & ":" & "C" & intRows + 1)
'定义一个数组
Dim a(intRows + 1, 3) As String
'生成數據
Dim dr As SqlDataReader = DALDB.GetDataReader(StrSql)
For i = 0 To intRows - 1
dr.Read()
Dim strSNO As String = dr("SNO").ToString.Trim
Dim StrPN As String = dr("MaterialPN").ToString.Trim
Dim strQty As String = dr("Qty").ToString.Trim
'ExcelSheet.Range("A" + CType(i, String)).Value = strSNO
'ExcelSheet.Range("B" + CType(i, String)).Value = StrPN
'ExcelSheet.Range("C" + CType(i, String)).Value = strQty
Dim j As Integer
For j = 0 To 2
If j = 0 Then
a(i, 0) = strSNO
End If
If j = 1 Then
a(i, 1) = StrPN
End If
If j = 2 Then
a(i, 2) = strQty
End If
Next
Next
ObjRange.Value = a
'調整單元格的寬度:自動適應
ExcelApp.Cells.Columns.AutoFit()
ExcelWorkBook.SaveAs(FileNamePath)
ExcelWorkBook.Close(DBNull.Value, DBNull.Value, DBNull.Value)
ExcelApp.Workbooks.Close()
ExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook)
ExcelApp = Nothing
ExcelWorkBook = Nothing
ExcelSheet = Nothing
GC.Collect()
End Sub
Private Sub CreateHeader(ByVal ExcelSheet As Microsoft.Office.Interop.Excel.Worksheet)
ExcelSheet.Range("A1").Value = "SNO"
ExcelSheet.Range("B1").Value = "MaterialPN"
ExcelSheet.Range("C1").Value = "Qty"
ExcelSheet.Range("D1").Value = "KeepQty/ReSell"
End Sub
Public Sub WriteToExcelFile(ByVal FileName As String, ByVal intRows As Integer, ByVal StrSql As String)
Dim FilePath As String = "F:\System\SCS\Programes\SCSWeb\UploadKeepQty\Download\"
Dim FileNamePathType As String = FilePath + FileName + ".xls"
Dim FileNamePath As String = FilePath + FileName
FileExist(FileNamePathType, True)
Dim ExcelApp As New Excel.Application
ExcelApp.Visible = False
Dim ExcelWorkBook As Excel.Workbook = ExcelApp.Workbooks.Add()
' Dim ExcelSheet As Excel.Worksheet = ExcelWorkBook.Worksheets.Add()
Dim ExcelSheet As Excel.Worksheet = ExcelWorkBook.Worksheets(1)
CreateHeader(ExcelSheet)
Dim i As Integer
' "A" & 2 & " 是从哪里开始写入,"C" 结束的列," intRows + 1"是数据结束的行数
Dim ObjRange As Object = ExcelSheet.Range("A" & 2 & ":" & "C" & intRows + 1)
'定义一个数组
Dim a(intRows + 1, 3) As String
'生成數據
Dim dr As SqlDataReader = DALDB.GetDataReader(StrSql)
For i = 0 To intRows - 1
dr.Read()
Dim strSNO As String = dr("SNO").ToString.Trim
Dim StrPN As String = dr("MaterialPN").ToString.Trim
Dim strQty As String = dr("Qty").ToString.Trim
'ExcelSheet.Range("A" + CType(i, String)).Value = strSNO
'ExcelSheet.Range("B" + CType(i, String)).Value = StrPN
'ExcelSheet.Range("C" + CType(i, String)).Value = strQty
Dim j As Integer
For j = 0 To 2
If j = 0 Then
a(i, 0) = strSNO
End If
If j = 1 Then
a(i, 1) = StrPN
End If
If j = 2 Then
a(i, 2) = strQty
End If
Next
Next
ObjRange.Value = a
'調整單元格的寬度:自動適應
ExcelApp.Cells.Columns.AutoFit()
ExcelWorkBook.SaveAs(FileNamePath)
ExcelWorkBook.Close(DBNull.Value, DBNull.Value, DBNull.Value)
ExcelApp.Workbooks.Close()
ExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook)
ExcelApp = Nothing
ExcelWorkBook = Nothing
ExcelSheet = Nothing
GC.Collect()
End Sub
Private Sub CreateHeader(ByVal ExcelSheet As Microsoft.Office.Interop.Excel.Worksheet)
ExcelSheet.Range("A1").Value = "SNO"
ExcelSheet.Range("B1").Value = "MaterialPN"
ExcelSheet.Range("C1").Value = "Qty"
ExcelSheet.Range("D1").Value = "KeepQty/ReSell"
End Sub
相关文章推荐
- 利用asp.net来访问excel文档
- 利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM
- ASP.Net使用缓存技术提高效率
- ASP.NET 2.0,C#----利用GridView控件导出其他文件(导出Excel,导出Word文件)
- .net实例:在ASP.NET Atlas中调用Web Service—批量调用以提高效率
- Asp.Net :写入 、读取 、 删除、追加cookie数组?
- ASP.NET 利用 Microsoft.Office.Interop.Excel 版本导出Excel数据
- ASP.NET利用数据流导入到Excel
- 利用ASP.NET来访问Excel文档
- ASP.Net MVC利用NPOI导入导出Excel
- ASP.NET 缓存分析和实践浅析提高运行效率
- 如何利用缓存提高ASP.NET网站速度
- 利用ASP.NET 导出excel(2007版)
- asp.net中利用XML描述文件和Excel模板相结合的方法生成Excel报表
- 多年以前提高asp.net分页查询效率的一个实例
- ASP.Net使用缓存技术提高效率
- 利用缓存提高ASP.NET网站速度
- ASP实例:利用缓存提高数据显示效率
- ASP.NET网页生成EXCEL并下载(利用DataGrid或GridView等)
- ASP.NET操作EXCEL时出现的错误 Retrieving the COM class factory for component with CLSID(转)