我遇到了大麻烦...以下是我的程序运行良好...但是在复制新工作簿(copy.xlsx)后,数据库连接与(test.xlsx)的数据库连接相同......这是我不想要的...请在复制时帮助删除此类数据库连接,并且仅将内容复制到新工作区
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