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에 추가 할 수 있습니까?
잊지 마세요 [COM 개체를 출시] (http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects)! –