VB.Net 16行代码完成多个excel合并

本篇文章是昨日《VB.Net多少行代码完成多个excel合并》的续篇。
昨天在测试的时候感觉代码合并excel太慢了,今天早上醒来的时候又再思考了一下。
因为大多数情况下,需要合并的excel文件有着相同列数,那么使用range方法就是可行的。
于是立即打开电脑进行测试了思考的代码,并与昨天的代码运行时间进行了比较。

昨天的代码增加了检查简单运行时间(已经在Public Class Form1代码前加上了 Imports Microsoft.Office.Interop ):

    Dim maxcount As Integer = 0
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim startTime As DateTime = Now
        Dim path As String = "D:\lessons\mergexls"
        Dim files() As String = IO.Directory.GetFiles(path)
        Dim saveExcelfile As String = path & "\allexcel.xlsx"
        Dim xlsAll As New Excel.Application()
        Dim Wbook As Excel.Workbook = xlsAll.Workbooks.Add
        Dim Wsheet As Excel.Worksheet = Wbook.Worksheets.Add
        For Each singlexls As String In files
            writeExcel(singlexls, xlsAll, Wsheet)
        Next
        Wbook.SaveAs(saveExcelfile)
        xlsAll.Quit()
        Dim endTime As DateTime = Now
        TextBox1.Text = "方法1:" & (endTime - startTime).TotalMilliseconds.ToString
    End Sub
    Private Sub writeExcel(ByVal xlssinglefile As String, ByVal xlsapp As Excel.Application, ByVal allSheet As Excel.Worksheet)
        Dim Wsinglebook As Excel.Workbook = xlsapp.Workbooks.Open(xlssinglefile)
        Dim Wsinglesheet As Excel.Worksheet = Wsinglebook.Worksheets(1)
        For i As Integer = 1 To Wsinglesheet.UsedRange.Rows.Count
            maxcount += 1
            For j As Integer = 1 To Wsinglesheet.UsedRange.Columns.Count
                allSheet.Cells(maxcount, j).Value = Wsinglesheet.Cells(i, j).value
            Next
        Next
        Wsinglebook.Close()
    End Sub

运行结果如下图:

VB.Net 16行代码完成多个excel合并_第1张图片

今天思考出来的代码:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim startTime As DateTime = Now
        Dim maxcountAll As Integer = 1
        Dim path As String = "D:\lessons\mergexls"
        Dim files() As String = IO.Directory.GetFiles(path)
        Dim saveExcelfile As String = path & "\allexcel.xlsx"
        Dim xlsAll As New Excel.Application()
        Dim WbookAll As Excel.Workbook = xlsAll.Workbooks.Add
        Dim WsheetAll As Excel.Worksheet = WbookAll.Worksheets(1)   '.Add

        For Each singlexls As String In files
            Dim Wsinglebook As Excel.Workbook = xlsAll.Workbooks.Open(singlexls)
            Dim Wsinglesheet As Excel.Worksheet = Wsinglebook.Worksheets(1)
            Dim rangesingle As Excel.Range = Wsinglesheet.Range(Wsinglesheet.Cells(1, 1), Wsinglesheet.Cells(Wsinglesheet.UsedRange.Rows.Count, Wsinglesheet.UsedRange.Columns.Count))
            Dim rangeall As Excel.Range = WsheetAll.Range(WsheetAll.Cells(maxcountAll, 1), WsheetAll.Cells(maxcountAll + Wsinglesheet.UsedRange.Rows.Count - 1, Wsinglesheet.UsedRange.Columns.Count))
            rangeall.Value = rangesingle.Value
            maxcountAll += Wsinglesheet.UsedRange.Rows.Count
            Wsinglebook.Close()
        Next
        WbookAll.SaveAs(saveExcelfile)
        xlsAll.Quit()
        Dim endTime As DateTime = Now
        TextBox1.Text = "方法2:" & (endTime - startTime).TotalMilliseconds.ToString
    End Sub

运行结果如下图:

VB.Net 16行代码完成多个excel合并_第2张图片

测试使用的Excel文件共7个,每个文件80行,20列,数据取自NorthWind数据库中的订单表。
两段代码的执行时间相差了近50倍。可以看出,对Excel的COM组件中使用双重循环读取单元格,速度的确太大影响。
那么再将今天的代码精简一下,去除无关部分:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim maxcountAll As Integer = 1
        Dim path As String = "D:\lessons\mergexls"
        Dim files() As String = IO.Directory.GetFiles(path)
        Dim saveExcelfile As String = path & "\allexcel.xlsx"
        Dim xlsAll As New Excel.Application()
        Dim WbookAll As Excel.Workbook = xlsAll.Workbooks.Add
        Dim WsheetAll As Excel.Worksheet = WbookAll.Worksheets(1)
        For Each singlexls As String In files
            Dim Wsinglebook As Excel.Workbook = xlsAll.Workbooks.Open(singlexls)
            Dim Wsinglesheet As Excel.Worksheet = Wsinglebook.Worksheets(1)
            WsheetAll.Range(WsheetAll.Cells(maxcountAll, 1), WsheetAll.Cells(maxcountAll + Wsinglesheet.UsedRange.Rows.Count - 1, Wsinglesheet.UsedRange.Columns.Count)).Value = Wsinglesheet.Range(Wsinglesheet.Cells(1, 1), Wsinglesheet.Cells(Wsinglesheet.UsedRange.Rows.Count, Wsinglesheet.UsedRange.Columns.Count)).Value
            maxcountAll += Wsinglesheet.UsedRange.Rows.Count
            Wsinglebook.Close()
        Next
        WbookAll.SaveAs(saveExcelfile)
        xlsAll.Quit()
    End Sub


哦耶,除去按钮事件的sub代码,总共16行。如果加上Imports部分,17行,当然,也可以把Imports部分写在代码中,还是16行。

精简了时间,同时代码也精简了不少。

关于range部分,我会在即将发布的教程中讲到,大家敬请期待。

 

你可能感兴趣的:(vb.net,点滴,vb.net,合并,excel)