我正在使用Visual Studio 2015.我在一个文件夹中有750个csv文件。我需要删除它们中的某些列(有星号的地方)。当用户在对话框中选择该选项时,我创建了一个项目wpf应用程序。下面是按钮单击时激活的代码。
Class MainWindow
Dim xl As Microsoft.Office.Interop.Excel._Application
Dim wb As Microsoft.Office.Interop.Excel._Workbook
Dim ws As Microsoft.Office.Interop.Excel._Worksheet
Dim iCol As Integer
Dim strName As String
Dim iIndex As Integer
Dim strPath As String
Dim strFile As String
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
If cleanRadioButton.IsChecked = True Then
strPath = "c:\test\old\"
strFile = Dir(strPath & "*.csv")
Do While strFile <> ""
wb = wb.Open(Filename:=strPath & strFile)
'Loop through the sheets.
For iIndex = 1 To xl.ActiveWorkbook.Worksheets.Count
ws = xl.ActiveWorkbook.Worksheets(iIndex)
'Loop through the columns.
For iCol = 1 To ws.UsedRange.Columns.Count
'Check row 1 of this column for the char of *
If InStr(ws.Cells(10, iCol).Value, "*") > 0 Then
'We have found a column with the char of *
xl.DisplayAlerts = False
ws.Columns(iCol).EntireColumn.Delete
ws.Columns(iCol + 1).EntireColumn.Delete
ws.Columns(iCol + 2).EntireColumn.Delete
End If
Next iCol
Next iIndex
wb.SaveAs(Filename:="C:\test\new\" & wb.Name, FileFormat:=51)
wb.Close(SaveChanges:=False)
strFile = Dir()
Loop
MessageBox.Show("The csv files have now been cleaned. Congrats.")
Else inputRadioButton.IsChecked = True
MessageBox.Show("The data has now been split into Trajectory and ForcePlate input files. High 5.")
End If
End Sub End Class
我在编译时没有错误但是在运行时我得到NullReferenceException在第wb = wb.Open(Filename:=strPath & strFile)
行未处理。这是因为wb是null并且我相信还没有实例化。我无法解决如何做到这一点,因为你不能在界面上使用New关键字......
有人可以解释一下吗? THX
答案 0 :(得分:0)
感谢您的回复。 Rory是正确的 - WorkbookS是调用Open的对象而不是Workbook。
Matteo NNZ在某种程度上也是正确的。下面是实际适合我的代码(密钥在Imports语句中,并在Microsoft.Office.Interop.Excel的引用属性中设置Embed Interop Types = FALSE)
Imports Excel = Microsoft.Office.Interop.Excel
Class MainWindow Dim xl As Excel.Application = New Excel.ApplicationClass() Dim wb作为Excel.Workbook Dim ws As Excel.Worksheet 昏暗的iCol作为整数 Dim strName As String Dim iIndex As Integer Dim strPath As String Dim strFile As String
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
If cleanRadioButton.IsChecked = True Then
strPath = "c:\test\old\"
strFile = Dir(strPath & "*.csv")
Do While strFile <> ""
wb = xl.Workbooks.Open(Filename:=strPath & strFile)
'Loop through the sheets.
For iIndex = 1 To xl.ActiveWorkbook.Worksheets.Count
ws = xl.ActiveWorkbook.Worksheets(iIndex)
'Loop through the columns.
For iCol = 1 To ws.UsedRange.Columns.Count
'Check row 10 of this column for the char of *
If InStr(ws.Cells(10, iCol).Value, "*") > 0 Then
'We have found a column with the char of *
xl.DisplayAlerts = False
ws.Columns(iCol).EntireColumn.Delete
ws.Columns(iCol).EntireColumn.Delete
ws.Columns(iCol).EntireColumn.Delete
iCol = iCol - 3
End If
Next iCol
Next iIndex
wb.SaveAs(Filename:="C:\test\new\" & wb.Name, FileFormat:=51)
wb.Close(SaveChanges:=False)
strFile = Dir()
Loop
MessageBox.Show("The csv files have now been cleaned. Congrats.")
Else inputRadioButton.IsChecked = True
MessageBox.Show("The data has now been split into Trajectory and ForcePlate input files. High 5.")
End If
End Sub
结束班
希望能帮助下一个人 解: 导入Excel = Microsoft.Office.Interop.Excel Dim xl As Excel.Application = New Excel.ApplicationClass()