拷贝既存excel文件sheet方法

此代码示例展示了如何使用VBA批量复制并填充EXCEL模板中的特定工作表,并进行数据填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop

------------------------------------------------------------    

''' <summary>

    ''' EXCELファイルにテンプレートシートをコピーする
    ''' </summary>
    ''' <param name="sheetCnt">コピーシート数</param>
    ''' <remarks></remarks>
    Private Sub CopyExcelSheet(ByVal sheetCnt As Integer)
        Dim xlApp As Object
        xlApp = CreateObject("Excel.Application")

        Dim xlBooks As Object = xlApp.Workbooks

        ' fn:excel文件路径c:\xxx\aaa.xls

        Dim xlBook As Object = xlBooks.Open(fn)
        Dim xlSheet As Object = xlBook.Sheets

        Try
            xlBook = xlApp.Workbooks.Open(fn)
            xlApp.Visible = True
            xlApp.Displayalerts = False
            'xlSheet = xlBook.Worksheets(1)

            ' シートコピー
            Dim indexNew As Integer = 0
            Dim sheet2Index As Integer = 0
            For index As Integer = 0 To sheetCnt - 1
                xlSheet = xlBook.Worksheets(1)
                If index = 0 Then
                    indexNew = index
                End If
                If indexNew <= arr.Count - 1 Then
                    ' シートコピー1から
                    If index = 0 Then
                        xlSheet.Copy(After:=xlBook.Worksheets(2 + index))
                    Else
                        'xlSheet.Copy(After:=xlBook.Worksheets(2 + index + 1))
                        xlSheet.Copy(After:=xlBook.Worksheets(sheet2Index))
                    End If


                    ' シート選択0から
                    If index = 0 Then
                        xlSheet = xlBook.Worksheets(3 + index)
                    Else
                        xlSheet = xlBook.Worksheets(sheet2Index + 1)
                    End If
                    ' セル書く
                    Dim lineData As String() = arr(indexNew).ToString.Split(",")
                    '  郵便番号 
                    xlSheet.Cells(5, 41) = lineData(0)
                    '  住所
                    xlSheet.Cells(7, 41) = lineData(1)
                    ' 氏名
                    xlSheet.Cells(3, 41) = lineData(2)
                    If indexNew <= arr.Count - 2 Then
                        Dim lineData2 As String() = arr(indexNew + 1).ToString.Split(",")
                        ' 郵便番号 
                        xlSheet.Cells(35, 41) = lineData2(0)
                        ' 住所
                        xlSheet.Cells(37, 41) = lineData2(1)
                        ' 氏名
                        xlSheet.Cells(33, 41) = lineData2(2)
                    End If


                    ' シート2をコピー
                    xlSheet = xlBook.Worksheets(2)
                    If index = 0 Then
                        xlSheet.Copy(After:=xlBook.Worksheets(2 + index + 1))
                        sheet2Index = 2 + index + 1
                    Else
                        'xlSheet.Copy(After:=xlBook.Worksheets(2 + index + 2))
                        sheet2Index = sheet2Index + 1
                        xlSheet.Copy(After:=xlBook.Worksheets(sheet2Index))
                    End If
                    ' シート2にデータを書くTODO...
                End If


                'sheet2Index = 2 + index + 2
                sheet2Index = sheet2Index + 1
                indexNew = indexNew + 2
            Next
            '' テンプレートシートを削除
     'TODO

        Catch ex As Exception
            labOperStates.Visible = False
            MessageBox.Show("シートコピー中、エラー...")
        Finally
            If Not xlSheet Is Nothing Then
                Try
                Finally
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
                End Try
            End If


            If Not xlBook Is Nothing Then
                Try
                    xlBook.Close()
                Finally
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
                End Try
            End If


            If Not xlBooks Is Nothing Then
                Try
                Finally
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks)
                End Try
            End If
            If Not xlApp Is Nothing Then
                Try
                    xlApp.Quit()
                Finally
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
                End Try
            End If
            'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
            'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks)
            'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
            'xlBook.Close(True)
            'xlApp.Quit()
            'xlApp = Nothing


        End Try
    End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值