asp.net com excel利用數組寫入excel 效率提高

來源:互聯網
上載者:User

 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

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.