首页 > 解决方案 > 导出到另一台电脑时 vb.net 中数组的索引超出范围

问题描述

我使用 Access 2007 作为数据库为股票创建了一个 vb.net 项目。该项目在我的 PC 上运行良好,但是当我将项目导出到另一台 PC 时,一切正常,除了出现错误消息:- 索引超出数组范围。

Public Sub showresults(ByVal whatisit As String, ByVal sheetnum As Integer)
    dbConnection = New System.Data.OleDb.OleDbConnection(path)
    Dim data_for_excel As New List(Of store_data)
    Dim forselect As New List(Of String)
    whatisit = " status = 'P' "
    Try
        dbConnection.Open()
        sqlString = "SELECT DISTINCT (bf & '$' & gsm & '$' & desc & '$' & size_paper) AS everh from purchased_paper where " & whatisit & ""
        dbCommand = New OleDbCommand(sqlString, dbConnection)
        dbReader = dbCommand.ExecuteReader()
        While (dbReader.Read())
            forselect.Add(dbReader("everh"))
        End While

    Catch ex As Exception
        MsgBox(" ERROR WHILE SELECTING DISTICT " & sqlString & " " & ex.Message)

    End Try
    Label1.Text = forselect.Count
    Dim storagecell As store_data
    Try
        For i = 0 To forselect.Count - 1
            Dim words As String() = forselect.ElementAt(i).Split(New Char() {"$"c})
            sqlString = "SELECT * FROM purchased_paper where bf = " & words(0) & " and gsm = " & words(1) & " and desc = '" & words(2) & "' and size_paper = " & words(3) & "and " & whatisit
            dbCommand = New OleDbCommand(sqlString, dbConnection)
            dbReader = dbCommand.ExecuteReader()

            storagecell = New store_data(words(0), words(1), words(2), words(3))
            While (dbReader.Read())

                If sheetnum = 4 Then
                    storagecell.changedesc(dbReader("status"))
                End If

                storagecell.setWeight(dbReader("weight"))
                storagecell.setrate(dbReader("rate"))
                storagecell.setdateofc(dbReader("dateofc"))
                getcolorfordfont(dbReader("dateofc"))
            End While
            data_for_excel.Add(storagecell)
        Next

    Catch ex As Exception
        MsgBox(" ERROR WHILE SELECTING DISTICT " & sqlString & " " & ex.Message)

    End Try
    Dim val As Integer
    Dim storecell As store_data
    Dim j As Double = 0
    For i = 1 To data_for_excel.Count - 1
        val = data_for_excel.ElementAt(i).getsum
        storecell = data_for_excel.ElementAt(i)
        j = i - 1
        While (j >= 0 And data_for_excel.ElementAt(j).getsum > val)
            data_for_excel.RemoveAt(j + 1)
            data_for_excel.Insert(j + 1, data_for_excel.ElementAt(j))
            j = j - 1
        End While
        data_for_excel.RemoveAt(j + 1)
        data_for_excel.Insert(j + 1, storecell)
    Next
    Dim inn As Integer = data_for_excel.Count



    exlworksheet = exlworkbook.Worksheets(sheetnum)

    If sheetnum = 1 Then
        exlworksheet.Cells(2, 3).Value = "IN STOCKS"
        exlworksheet.Cells(1, 1).Value = " Same Month"
        exlworksheet.Cells(1, 3).Value = " 1 Month old "
        exlworksheet.Cells(1, 5).Value = " 2 Months old"
        exlworksheet.Cells(1, 7).Value = " 3 Months old"
        exlworksheet.Cells(1, 9).Value = " 4 Months old"
        exlworksheet.Cells(1, 11).Value = " 5 Months old "
        exlworksheet.Cells(1, 13).Value = " 6 or More Months old "

        exlworksheet.Cells(1, 1).Font.color = RGB(100, 100, 100)
        exlworksheet.Cells(1, 3).Font.color = RGB(0, 100, 255)
        exlworksheet.Cells(1, 5).Font.color = RGB(0, 0, 255)
        exlworksheet.Cells(1, 7).Font.color = RGB(125, 0, 125)
        exlworksheet.Cells(1, 9).Font.color = RGB(150, 125, 200)
        exlworksheet.Cells(1, 11).Font.color = RGB(100, 25, 250)
        exlworksheet.Cells(1, 13).Font.color = RGB(255, 0, 0)

    ElseIf sheetnum = 2 Then
        exlworksheet.Cells(1, 3).Value = "CONSUMED IN PERIOD FROM " & TextBox1.Text & " TO " & TextBox2.Text
    ElseIf sheetnum = 3 Then
        exlworksheet.Cells(1, 3).Value = "SOLD IN PERIOD FROM " & TextBox1.Text & " TO " & TextBox2.Text
    ElseIf sheetnum = 4 Then
        exlworksheet.Cells(1, 3).Value = "SEND FOR JOB WORK IN PERIOD FROM " & TextBox1.Text & " TO " & TextBox2.Text
    End If

    exlworksheet.Cells(3, 1).Value = " BF / GSM"
    exlworksheet.Cells(3, 2).Value = " SIZE "
    exlworksheet.Cells(3, 3).Value = " REEL DETAILS --------------->"
    exlworksheet.Cells(3, 52).Value = "TOTAL WEIGHT "
    exlworksheet.Cells(3, 53).Value = "AVG. RETE"
    exlworksheet.Cells(3, 54).Value = " TOTAL AMOUNT "
    exlworksheet.Cells(3, 55).Value = " SIZE"
    row = 4
    col = 0
    Dim rowcountmax As Integer = data_for_excel.Count
    For index = 0 To data_for_excel.Count - 1
        exlworksheet.Cells(row, col + 1).Value = data_for_excel.ElementAt(index).getbdg
        exlworksheet.Cells(row, col + 2).Value = data_for_excel.ElementAt(index).getSize
        exlworksheet.Cells(row, col + 53).Value = data_for_excel.ElementAt(index).getavgrate
        exlworksheet.Cells(row, col + 55).Value = data_for_excel.ElementAt(index).getSize
        exlworksheet.Cells(row, col + 52).Value = "=sum(C" & row.ToString & ":AX" & row.ToString & ")"
        exlworksheet.Cells(row, col + 54).Value = "=AZ" & row.ToString & "*BA" & row.ToString & ""
        Dim arrayofweight As New List(Of Double)
        Dim arrayofdate As New List(Of String)
        col = 3
        arrayofweight = data_for_excel.ElementAt(index).getweightarray
        arrayofdate = data_for_excel.ElementAt(index).getdateaar
        For ind = 0 To data_for_excel.ElementAt(index).getweinum - 1
            exlworksheet.Cells(row, col).Value = arrayofweight.ElementAt(ind)
            Dim forcol As Integer = getcolorfordfont(arrayofdate.ElementAt(ind))

            If sheetnum = 1 Then
                If forcol = 0 Then
                    exlworksheet.Cells(row, col).Font.color = RGB(100, 100, 100)
                ElseIf forcol = 1 Then
                    exlworksheet.Cells(row, col).Font.color = RGB(0, 100, 255)
                ElseIf forcol = 2 Then
                    exlworksheet.Cells(row, col).Font.color = RGB(0, 0, 255)
                ElseIf forcol = 3 Then
                    exlworksheet.Cells(row, col).Font.color = RGB(125, 0, 125)
                ElseIf forcol = 4 Then
                    exlworksheet.Cells(row, col).Font.color = RGB(150, 125, 200)
                ElseIf forcol = 5 Then
                    exlworksheet.Cells(row, col).Font.color = RGB(100, 25, 250)
                Else
                    exlworksheet.Cells(row, col).Font.color = RGB(255, 0, 0)
                End If
            End If


            'exlworksheet.Cells(row, col).Font.color = RGB((getcolorfordfont(arrayofdate.ElementAt(ind) Mod 3)) * 2, (arrayofdate.ElementAt(ind) Mod 5) * 10, (arrayofdate.ElementAt(ind) / 5))
            col = col + 1
        Next
        col = 0
        row = row + 1
    Next

    rowcountmax = rowcountmax + 1

    dbConnection.Close()
    '  exlworksheet.Cells(rowcountmax, 52).Value = "=sum(AZ4:AZ" & rowcountmax.ToString
    ' exlworksheet.Cells(rowcountmax, 54).Value = "=sum(BB4:BB" & rowcountmax.ToString
    'exlworksheet.Cells(rowcountmax, 53).Value = "=BB" & rowcountmax.ToString & "/AZ" & rowcountmax.ToString
End Sub

标签: vb.net

解决方案


推荐阅读