2013-11-29 101 views
0

我試圖創建xlworksheet的數組以保存爲xlsx格式,並使用For Next結構。當我執行程序時,錯誤指向 xlWorkSheet(xlName).Name = "Judge " & xlName & " Score Summary" 並且錯誤消息是「對象引用未設置爲對象的實例」。使用數組創建excel工作表

我使用的是2010 vb.NET

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 「無法創建記錄」) 退出小組 結束子'

實際上,程序工作時,我用這個結構

 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結構縮短代碼嗎?爲每個裁判輸入每個代碼太令人沮喪。

謝謝!

回答

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 
相關問題