2014-09-03 6 views
1

Excel InterOp를 사용하여 여러 시트와 여러 통합 문서를 만들려면 다음 패턴을 따라 왔습니다. 이제는 원본 통합 문서에서 작성한 시트 중 일부를 포함하는 마스터 통합 문서를 호출해야합니다. 예를 들자면 WorkBook1에는 Sheet1과 Sheet2가 있고 Workbook2에는 sheet3과 sheet4가 있다고 가정 해 봅시다. 마스터 통합 문서에 Sheet1과 Sheet3이 있어야합니다. 나는 시트 (sheet1, sheet2, sheet3, sheet4)를 만드는 방법을 찾고 있는데, 이는 WorkBook1과 WorkBook2를 위해 만들었지 만 최소한의 코드 반복으로 sheet1과 sheet3을 마스터 통합 문서에 추가하는 것이다. 어떤 도움을 주시면 감사하겠습니다. Excel 시트를 작성하여 Excel의 여러 WorkBooks에 추가하는 방법 InterOp

Dim MasterWorkBook As Workbook 
MasterWorkBook = xlApp.Workbooks.Add 

10 개 다른 통합 문서를 만들려면 10 번 반복 루프 전에 정의 : 내 마스터 통합 문서가있는 경우

For i = 1 To 10 
    Dim xlApp As Application = New Application 
    Dim xlWorkBook As Workbook 
    xlWorkBook = xlApp.Workbooks.Add 
    Dim xlWorkSheet As Worksheet 
    Dim xlSheets As Sheets = xlWorkBook.Sheets 
    Dim xlNewSheet As Worksheet 
    Dim sheetCount As Integer = 1 

    ' So I repeat the following block to add multiple sheets with different content to a WorkBook 
    xlNewSheet = xlSheets.Add(xlSheets(sheetCount), Type.Missing, Type.Missing, Type.Missing) 
    sheetCount += 1 
    xlNewSheet.Name = SomeName 
    xlWorkSheet = xlWorkBook.Sheets(SomeName) 
    AddContentToSheet(xlNewSheet) ' A Sub that adds real content to the sheet 
    . 
    . 
    . 
    . 

    xlWorkBook.SaveAs(...) 
    xlWorkBook.Close() 
    xlApp.Quit() 
Next i 

그래서 지금은 내 질문이다. 최소한의 코드 반복으로 어떻게 선택지를 MasterWorkBook에 추가 할 수 있습니까?

+0

잊지 마세요 [COM 개체를 출시] (http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects)! –

답변

1
Dim xlApp As Application = New Application 
    Dim masterWb As Workbook 
    masterWb = xlApp.Workbooks.Add 

    For i = 1 To 3 
     Dim xlWorkBook As Workbook 
     xlWorkBook = xlApp.Workbooks.Add 
     Dim ws As Worksheet 
     'Dim xlSheets As Sheets = xlWorkBook.Sheets 
     'Dim xlNewSheet As Worksheet 
     For j = 1 To 2 
      Try 
       ws = xlWorkBook.Sheets.Add(, xlWorkBook.Sheets(xlWorkBook.Sheets.Count)) 
       ws.Name = i + j 
       AddContentToSheet(ws) 
       If j = 1 Then 
        ws.Copy(, masterWb.Sheets(masterWb.Sheets.Count)) 
       End If 
      Catch 
       ws = Nothing 
       xlWorkBook.Close() 
       xlWorkBook = Nothing 
       xlApp.Quit() 
       xlApp = Nothing 
       Console.WriteLine("error") 
       Exit Sub 
      End Try 
     Next j 

     'AddContentToSheet(xlNewSheet) ' A Sub that adds real content to the sheet 
     ws = Nothing 
     xlWorkBook.SaveAs("C:\Users\Documents\Visual Studio 2008\Project\" + Str(i) + ".xlsx") 
     xlWorkBook.Close() 
     xlWorkBook = Nothing 
    Next i 

    masterWb.Sheets("Sheet1").Delete() 
    masterWb.Sheets("Sheet2").Delete() 
    masterWb.Sheets("Sheet3").Delete() 
    masterWb.SaveAs("C:\Users\Documents\Visual Studio 2008\Project\master.xlsx") 
    masterWb.Close() 
    masterWb = Nothing 
    xlApp.Quit() 
    xlApp = Nothing 
End Sub 

Sub AddContentToSheet(ByVal a As Worksheet) 
    a.Cells(1, 1) = "abc" 
    a.Cells(1, 2) = "abc" 
End Sub 

코드는 3 개의 통합 문서를 만들고 각 통합 문서 끝에 2 개의 워크 시트를 추가합니다. 그리고 각 통합 문서의 첫 번째 추가 된 워크 시트를 마스터 통합 문서로 복사합니다. 사용 후에는 객체 참조를 해제해야합니다. 희망이 도움이 :)