2013-11-29 4 views
0

Я пытаюсь создать массив xlworksheets для сохранения в формате xlsx и использовать структуру For Next. при выполнении программы ошибка указывает на xlWorkSheet(xlName).Name = "Judge " & xlName & " Score Summary" и сообщение об ошибке «Ссылка на объект не установлена ​​в экземпляр объекта».создание таблиц excel с использованием массива

Я использую VB.NET 2010

Private Sub bgwSaveFiles_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwSaveFiles.DoWork 
    Dim xlApp As Excel.Application 
    Dim xlWorkBook As Excel.Workbook 
    ' Dim xlworksheet(), xlWorkSheetTotal As Object Excel.Worksheet 
    Dim misValue As Object = System.Reflection.Missing.Value 
    Dim oRng As Excel.Range 
    Dim R, C As Integer 
    xlApp = New Excel.ApplicationClass 
    xlWorkBook = xlApp.Workbooks.Add(misValue) 
    Dim xlWorkSheet() As Excel.Worksheet 
    Dim xlWorkSheetTotal As Excel.Worksheet 
    For xli As Integer = 15 To 1 
     xlworksheet(xli) = xlWorkBook.Worksheets.Add() 
    Next 
    xlWorkSheetTotal = xlWorkBook.Worksheets.Add() 
    xlWorkBook.Worksheets("Sheet1").Delete() 
    For xlName As Integer = 1 To 15 
     xlWorkSheet(xlName).Name = "Judge " & xlName & " Score Summary" 
    Next 
    xlWorkSheetTotal.Name = "Final Score Summary" 
    Dim Titles(9) As String 
    Titles(0) = "Contestant Name" 
    Titles(1) = "Portion" 
    Titles(2) = "Contest Piece" 
    Titles(3) = "Voice Quality" 
    Titles(4) = "Musicality" 
    Titles(5) = "Rhythm and Timing" 
    Titles(6) = "Stage Performance" 
    Titles(7) = "Total Score" 

    '####### TOTAL SCORE SUMMARY ####### 
    For HT As Integer = 0 To 7 
     xlWorkSheetTotal.Cells(1, HT + 1) = Titles(HT) 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      xlWorkSheetTotal.Cells(R + 2, C + 1) = SummaryScoreSheet.Rows.Item(R).Cells(C).Value 
     Next 
    Next 
    xlWorkSheetTotal = xlWorkBook.Sheets("Final Score Summary") 
    For HT As Integer = 0 To 7 
     xlWorkSheetTotal.Cells(10, HT + 1) = Titles(HT) 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      xlWorkSheetTotal.Cells(R + 11, C + 1) = SummaryScoreSheet.Rows.Item(R + 7).Cells(C).Value 
     Next 
    Next 

    oRng = xlWorkSheetTotal.Range("A1", "H1") 
    oRng.EntireColumn.AutoFit() 

    Dim judges() As Object 
    judges(1) = "j1ScoreSheet" 
    judges(2) = "j2ScoreSheet" 
    judges(3) = "j3ScoreSheet" 
    judges(4) = "j4ScoreSheet" 
    judges(5) = "j5ScoreSheet" 
    judges(6) = "j6ScoreSheet" 
    judges(7) = "j7ScoreSheet" 
    judges(8) = "j8ScoreSheet" 
    judges(9) = "j9ScoreSheet" 
    judges(10) = "j10ScoreSheet" 
    judges(11) = "j11ScoreSheet" 
    judges(12) = "j12ScoreSheet" 
    judges(13) = "j13ScoreSheet" 
    judges(14) = "j14ScoreSheet" 
    judges(15) = "j15ScoreSheet" 


    '####### START ####### 
    For HT As Integer = 0 To 7 
     For jNo As Integer = 1 To 15 
      xlworksheet(jNo).Cells(1, HT + 1) = Titles(HT) 
     Next 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      For jNo As Integer = 1 To 15 
       xlworksheet(jNo).Cells(R + 2, C + 1) = judges(jNo).Rows.Item(R).Cells(C).Value 
      Next 
     Next 
    Next 
    For jNoLabel As Integer = 1 To 15 
     xlworksheet(jNoLabel) = xlWorkBook.Sheets("Judge " & jNoLabel & " Score Summary") 
    Next 
    For HT As Integer = 0 To 7 
     For jNo As Integer = 1 To 15 
      xlworksheet(jNo).Cells(10, HT + 1) = Titles(HT) 
     Next 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      For jNo As Integer = 1 To 15 
       xlworksheet(jNo).Cells(R + 11, C + 1) = judges(jNo).Rows.Item(R + 7).Cells(C).Value 
      Next 
     Next 
    Next 
    For jNo As Integer = 1 To 15 
     oRng = xlworksheet(jNo).Range("A1", "H1") 
    Next 
    oRng.EntireColumn.AutoFit() 


    With xlApp 
     .DisplayAlerts = False 
     On Error GoTo Err_Hndler 
     Dim savePath As String = "C:\test\test.xlsx" 
     For xli As Integer = 15 To 1 
      xlworksheet(xli).SaveAs(savePath) 
     Next 
     xlWorkSheetTotal.SaveAs(savePath) 
     .DisplayAlerts = True 
    End With 
    xlWorkBook.Close() 
    xlApp.Quit() 
    releaseObject(xlApp) 
    releaseObject(xlWorkBook) 
    releaseObject(xlWorkSheetTotal) 
    MsgBox("Excel file created , you can find the file c:\") 

Err_Hndler: MsgBox ("Невозможно создать файл записи Excel Пожалуйста, убедитесь, что файл не открыт в другом приложении", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly "не удается создать запись") Exit Sub End Sub ''

на самом деле, программа работает, когда я использую такую ​​структуру

 Dim xlApp As Excel.Application 
    Dim xlWorkBook As Excel.Workbook 
    Dim xlworksheets() As Excel.Worksheet 
    Dim xlWorkSheetTotal, xlWorkSheet1, xlWorkSheet2, xlWorkSheet3, xlWorkSheet4, xlWorkSheet5, xlWorkSheet6, xlWorkSheet7, xlWorkSheet8, xlWorkSheet9, xlWorkSheet10, xlWorkSheet11, xlWorkSheet12, xlWorkSheet13, xlWorkSheet14, xlWorkSheet15 As Excel.Worksheet 
    Dim misValue As Object = System.Reflection.Missing.Value 
    Dim oRng As Excel.Range 
    Dim R, C As Integer 
    xlApp = New Excel.ApplicationClass 
    xlWorkBook = xlApp.Workbooks.Add(misValue) 
    xlWorkSheet15 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet14 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet13 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet12 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet11 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet10 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet9 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet8 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet7 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet6 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet5 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet4 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet3 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet2 = xlWorkBook.Worksheets.Add() 
    xlWorkSheet1 = xlWorkBook.Worksheets.Add() 
    xlWorkSheetTotal = xlWorkBook.Worksheets.Add() 
    xlWorkBook.Worksheets("Sheet1").Delete() 
    xlWorkSheet1.Name = "Judge 1 Score Summary" 
    xlWorkSheet2.Name = "Judge 2 Score Summary" 
    xlWorkSheet3.Name = "Judge 3 Score Summary" 
    xlWorkSheet4.Name = "Judge 4 Score Summary" 
    xlWorkSheet5.Name = "Judge 5 Score Summary" 
    xlWorkSheet6.Name = "Judge 6 Score Summary" 
    xlWorkSheet7.Name = "Judge 7 Score Summary" 
    xlWorkSheet8.Name = "Judge 8 Score Summary" 
    xlWorkSheet9.Name = "Judge 9 Score Summary" 
    xlWorkSheet10.Name = "Judge 10 Score Summary" 
    xlWorkSheet11.Name = "Judge 11 Score Summary" 
    xlWorkSheet12.Name = "Judge 12 Score Summary" 
    xlWorkSheet13.Name = "Judge 13 Score Summary" 
    xlWorkSheet14.Name = "Judge 14 Score Summary" 
    xlWorkSheet15.Name = "Judge 15 Score Summary" 
    xlWorkSheetTotal.Name = "Final Score Summary" 
    'xlWorkSheetTotal = xlWorkBook.Sheets("Final Score Summary") 
    'xlWorkSheet1 = xlWorkBook.Sheets("Judge 1 Score Summary") 
    Dim Titles(9) As String 
    Titles(0) = "Contestant Name" 
    Titles(1) = "Portion" 
    Titles(2) = "Contest Piece" 
    Titles(3) = "Voice Quality" 
    Titles(4) = "Musicality" 
    Titles(5) = "Rhythm and Timing" 
    Titles(6) = "Stage Performance" 
    Titles(7) = "Total Score" 

    '####### TOTAL SCORE SUMMARY ####### 
    For HT As Integer = 0 To 7 
     xlWorkSheetTotal.Cells(1, HT + 1) = Titles(HT) 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      xlWorkSheetTotal.Cells(R + 2, C + 1) = SummaryScoreSheet.Rows.Item(R).Cells(C).Value 
     Next 
    Next 
    xlWorkSheetTotal = xlWorkBook.Sheets("Final Score Summary") 
    For HT As Integer = 0 To 7 
     xlWorkSheetTotal.Cells(10, HT + 1) = Titles(HT) 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      xlWorkSheetTotal.Cells(R + 11, C + 1) = SummaryScoreSheet.Rows.Item(R + 7).Cells(C).Value 
     Next 
    Next 

    oRng = xlWorkSheetTotal.Range("A1", "H1") 
    oRng.EntireColumn.AutoFit() 


    '####### JUDGE 1 ####### 
    For HT As Integer = 0 To 7 
     xlWorkSheet1.Cells(1, HT + 1) = Titles(HT) 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      xlWorkSheet1.Cells(R + 2, C + 1) = j1ScoreSheet.Rows.Item(R).Cells(C).Value 
     Next 
    Next 
    xlWorkSheet1 = xlWorkBook.Sheets("Judge 1 Score Summary") 
    For HT As Integer = 0 To 7 
     xlWorkSheet1.Cells(10, HT + 1) = Titles(HT) 
    Next 
    For R = 0 To 6 
     For C = 0 To 7 
      xlWorkSheet1.Cells(R + 11, C + 1) = j1ScoreSheet.Rows.Item(R + 7).Cells(C).Value 
     Next 
    Next 

    oRng = xlWorkSheet1.Range("A1", "H1") 
    oRng.EntireColumn.AutoFit() 

Что я пытаюсь сделать здесь, могу ли я сократить код, используя For Next structure? слишком сложно печатать каждый код для каждого судьи.

спасибо!

ответ

0

Попробуйте заменить это:

Dim xlWorkSheet() As Excel.Worksheet 
For xli As Integer = 15 To 1 
    xlworksheet(xli) = xlWorkBook.Worksheets.Add() 
Next 

For xlName As Integer = 1 To 15 
    xlWorkSheet(xlName).Name = "Judge " & xlName & " Score Summary" 
Next 

с этим:

Dim xlWorkSheet As Excel.Worksheet 
With xlWorkBook 
    For xli As Integer = 1 To 15 
     set xlWorkSheet = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count)) 
     xlWorkSheet.Name = "Judge " & xli & " Score Summary" 
    Next 
End With 
Смежные вопросы