当前位置: 代码迷 >> VB Dotnet >> vb 读写excel,该怎么处理
  详细解决方案

vb 读写excel,该怎么处理

热度:170   发布时间:2016-04-25 02:21:13.0
vb 读写excel

Imports Microsoft.Office.Interop

Public Class Form1
    Dim newxls As Excel.Application
    Dim newbook As Excel.Workbook
    Dim newsheet As Excel.Worksheet
    Dim newrange As Excel.Range


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim filename As String = "E:\test\test.xlsx"
        newxls = CreateObject("Excel.application")
        newbook = newxls.Workbooks.Open(filename)
        For i As Integer = 1 To newbook.Worksheets.Count
            newsheet = newbook.Worksheets(i)
            If newsheet.Name = "NHI" Then
                Exit For
            End If
        Next
        Dim bList As ArrayList = New ArrayList()
        Dim bNo As String = String.Empty
        Dim btable As Hashtable = New Hashtable
        Dim bkey As String = String.Empty
        Dim bvalue As String = String.Empty
        Dim nullRow As Integer = newsheet.Range("B65536").End(Excel.XlDirection.xlUp).Row
        For i As Integer = 1 To nullRow
            bNo = newsheet.Cells(i, 2).value
            bList.Add(bNo)
        Next

        bList.Sort()
        Dim bListNo As ArrayList = New ArrayList
        Dim claimNo As String = String.Empty
        Dim claimNoNew As String = String.Empty
        For m As Integer = 0 To bList.Count - 1
            claimNo = bList(m)
            If claimNoNew <> claimNo Then
                claimNoNew = claimNo
                For j As Integer = 1 To nullRow
                    If newsheet.Cells(j, 2).value = claimNo Then
                        bListNo.Add(j)
                    End If
                Next
            End If
        Next

        Dim X As Integer = 0
        For k As Integer = 0 To bListNo.Count - 1
            Dim a As String = "A" & bListNo(k)
            Dim b As String = "B" & bListNo(k)
            Dim c As String = "C" & (X + 1)
            Dim d As String = "D" & (X + 1)
            newsheet.Range(a & ":" & b).Copy()
            newbook.Sheets("Sheet2").Range(c & ":" & d).PasteSpecial()
            X = X + 1
        Next

        newxls.ActiveWorkbook.SaveAs("abc.xlsx")
        newxls.Workbooks.Close()
        newxls.Quit()
  相关解决方案