Visual Studio 2015 – 操纵Excel?
我有750个Excel文件,我想要
- 通过删除带有星号标题的数据列清理,
- 然后将其中的一些数据放到新的工作簿工作表中,并将其他数据放入同一工作簿工作表中,并将其他一些数据放入第二个新工作簿中。
我在Visual Studio 2015中创build了一个带有2个单选button的小对话框
- 清理数据,
- 产生新的文件。
这是我的VB代码:
Class MainWindow 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 Application.Worksheets.Count ws = Application.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 * 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:=xlOpenXMLWorkbook) 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
我得到3个错误,但不能解决如何解决它们 。
a)工作表不是应用程序的成员[第19行]
b)工作表不是应用程序的成员[第20行]
c)“xlOpenXMLWorkbook”未被声明。 由于其保护级别,可能无法访问。
对于a)和b),模式是:
Application.Workbooks.Worksheets
对于c),最简单的出路:
从Excel进入VBE(Alt + F11)
按F2显示对象浏览器
寻找xlOpenXMLWorkbook
结果: Const xlOpenXMLWorkbook = 51 (&H33)
所以,只需将它replace为值51!
这里是你的修改代码:
Class MainWindow 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 wbIndex 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 wbIndex = 1 To Application.Workbooks.Count For iIndex = 1 To Application.Workbooks(wbIndex).Worksheets.Count Ws = Application.Workbooks(wbIndex).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 * Ws.Columns(iCol).EntireColumn.Delete Ws.Columns(iCol + 1).EntireColumn.Delete Ws.Columns(iCol + 2).EntireColumn.Delete End If Next iCol Next iIndex Next wbIndex 'Const xlOpenXMLWorkbook = 51 (&H33) 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
要引用工作表,您可以使用ws = wb.Worksheets(1)
或ws = wb.Worksheets("Sheet1")
或ws = excelApp.ActiveWorkbook.Worksheets(1)
并使用xlOpenXMLWorkbook
使用相应的Enum XlFileFormat
以及: XlFileFormat.xlOpenXMLWorkbook
。
这个简化的示例打开工作簿Test.xlsx ,写入单元格A1中的文本并将其保存到新的文件夹。
Imports System.IO Imports Microsoft.Office.Interop.Excel Public Class MainWindow Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim excelApp As Application Dim wb As _Workbook Dim ws As _Worksheet Dim rng As Range Dim strPathOld = "c:\temp\old" Dim strPathNew = "c:\temp\new" ' get excel application reference excelApp = New Application excelApp.Visible = True excelApp.ScreenUpdating = True ' open the workbook wb = excelApp.Workbooks.Open(Path.Combine(strPathOld, "Test.xlsx")) ' set reference to the sheet with index 1 ws = wb.Worksheets(1) ' or use sheet name ' ws = wb.Worksheets("Sheet1") ' or use ActiveWorkbook if it exists ' ws = excelApp.ActiveWorkbook.Worksheets(1) ' write text in cell A1 rng = ws.Range("A1") rng.Formula = "Test123" ' save the workbook in new location wb.SaveAs(Filename:=Path.Combine(strPathNew, wb.Name), _ FileFormat:=XlFileFormat.xlOpenXMLWorkbook) excelApp.Quit() End Sub End Class
注意:为您的Excel版本添加对MS Office Interop的引用(这里是Excel 2007的示例)。