2015-07-22 123 views
3

我有,我想Visual Studio 2015 - 操纵Excel?

  1. 清洁通过删除有星号的航向数据的列750个Excel文件,
  2. 然后采取一些数据,并把它放在一个新的工作簿的工作表,其他数据放入同一个工作簿工作表中,并将一些其他数据放入第二个新工作簿中。

我在Visual Studio 2015年创建的WPF项目与2个单选按钮一个小对话框

  1. 干净的数据,
  2. 产生新的文件。

这是我的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个错误,但无法工作,如何解决这些问题

一个)工作表是不应用[线19]

b)中的一员工作表是不应用的成员[线路20]

C) 'xlOpenXMLWorkbook' 未声明。由于其保护级别,它可能无法访问。

+1

我使用VB.Net和VBA。 VB.Net具有丰富的功能,其中包括访问以任何版本的Excel保存的工作簿,并且生成的可执行文件**可以比VBA宏快上百倍。我认为VB.Net是更好的语言,我比VBA更多地使用它。但是,从VB.Net访问工作簿的速度很慢。如果程序的唯一目标是操作工作簿,那么我会使用VBA。 –

回答

1

对于a)和b)中,图案是:

Application.Workbooks.Worksheets

对于c)所示,最简单的方法进行:

从走进VBE Excel(Alt + F11)

按F2显示对象浏览器

查找xlOpenXMLWorkbook

结果:Const xlOpenXMLWorkbook = 51 (&H33) 所以,仅仅由值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 
+0

嗨R3UK - thx不厌其烦地回复。我给你的修改后的代码加了保证,但是虽然解决了这个问题c)[thx btw],但是a)和b)的错误仍然存​​在:ActiveWorkbook不是Application的成员....任何想法? thx –

+0

@TomChambers:所以'ActiveWorkbook'可能不在Visual Studio中,所以我在'Workbooks'上添加了一个循环以避免使用它,唯一的问题是这可能会扫描所有打开的工作簿,您可以添加一个测试这个名字或者其他你想要的东西! – R3uK

1

要引用一个工作表油可以使用ws = wb.Worksheets(1)ws = wb.Worksheets("Sheet1")ws = excelApp.ActiveWorkbook.Worksheets(1)和使用xlOpenXMLWorkbook使用相应枚举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 

注:以MS Office的互操作添加引用您的Excel版本(这里例如为Excel 2007)。 enter image description here

+0

你好,谢谢你花时间回复。我把你的代码,但我不断收到错误,事情没有定义或不包含在应用程序...任何想法?? –

+0

@Tom我不知道问题出在哪里。但看到编辑的答案,添加了一张图片,我的示例项目中Excel-Interop dll参考的外观如何。 HTH – dee

+0

可能您的代码中的应用程序类型不是Excel应用程序?尝试使用完全限定名称,写入'''Microsoft.Office.Interop.Excel.Application'''。 – dee