1

私は大きな問題を抱えています...以下は正常に動作する私のプログラムです...しかし、コピーされた後、新しいワークブック(copy.xlsx)は(test.xlsx)と同じデータベース接続を持っています。 ... plsは、コピー中にそのようなdb接続を削除するのに役立ち、コンテンツだけが新しいワークにコピーされますb

        Dim filePath As String = directory & "\test.xlsx"
        Dim filePath1 As String = directory & "\copy.xlsx"
        Dim xlobj As New Microsoft.Office.Interop.Excel.Application()
        Dim w As Workbook
        Dim w1 As Workbook
        Dim s As Worksheet
        Dim s1 As Worksheet
        Dim ws As Worksheet
        Dim wsCount As Integer = 0

        Dim FileToDelete As String = directory & "\copy.xlsx"
        If System.IO.File.Exists(FileToDelete) = True Then
            System.IO.File.Delete(FileToDelete)
            ''MsgBox("File Deleted")
        End If

        ' ''To Create a new copy.xlsx file
        Dim xlWB As Workbook
        Dim xlWS As Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        xlWB = xlobj.Workbooks.Add(misValue)
        xlWS = xlWB.Sheets("Sheet1")
        xlWS.SaveAs(directory & "\copy.xlsx")
        xlWB.Close()

        w = xlobj.Workbooks.Open(filePath)
        w1 = xlobj.Workbooks.Open(filePath1)
        ''it will count the number of sheets in source file
        For Each ws In w.Worksheets
            wsCount = wsCount + 1
        Next
        For i As Integer = 1 To wsCount
            s = w.Worksheets.Item(i)
            s1 = w1.Worksheets.Item(i)

            ' it will copy and paste sheet  from one to another with formula
            s.UsedRange.Copy()
            s1.PasteSpecial()
            ''to remove filter
            If s1.AutoFilterMode = True Then
                s1.AutoFilterMode = False
            End If
            ''to delete connections
            If w1.Connections.Count > 0 Then
                For j As Integer = 1 To w1.Connections.Count
                    w1.Connections.Item(j).Delete()
                Next j
            End If
            ''s1.UsedRange.Formula = s.UsedRange.Formula
        Next

        w.Save()
        w1.Save()
        w.Close()
        w1.Close()
    Catch ex As Exception
        Hndl_Exception("Error accessing Excel: " + ex.Message)
    End Try
4

0 に答える 0