异常抛出:EPPlus.dll中的'System.IndexOutOfRangeException'

时间:2017-09-25 08:08:01

标签: indexoutofrangeexception

使用Visual Studio Community 2017和AdvancedHMI创建基于PC的HMI应用程序。我有几个没有问题的运行,所以转向新的东西。 在每一行我有7个数组(每个数组500个实数),并希望每天捕获这些数据并保存到Excel。 使用EPPlus和AdvancedHMI我有以下代码。

Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
    If e.ErrorId = 0 AndAlso e.Values.Count > 0 AndAlso e.Values(0) = "True" Then
        Console.WriteLine("About to read the data")
        Dim MyValues() As String = EthernetIPforCLXCom1.Read("VCell_1A_FES_Cycle_Average[0]", 500)
        Console.WriteLine(MyValues.Length & "elements read.")

        '* Transfer the values to Excel
        Using ExcelPackage As New OfficeOpenXml.ExcelPackage(New System.IO.FileInfo("c:\Data.xlsx"))
            For I = 0 To MyValues.Length - 1
                Console.WriteLine("Element " & I & "=" & MyValues(I))
                ExcelPackage.Workbook.Worksheets(1).Cells(1, I + 1).Value = MyValues(I)

            Next
        End Using
    End If
End Sub

运行它并触发我的标签vale来执行数据编写器我得到以下内容。

  

即将阅读数据

     

500元阅读。

     

元素0 = 87.945

     

抛出异常:EPPlus.dll中的'System.IndexOutOfRangeException'

一切看起来都应该有效但我对VB或任何类型的编码都很新。我的强项是PLC阶梯逻辑。

感谢。

1 个答案:

答案 0 :(得分:0)

这是我的工作。 Datascriber在PLC中有一个触发器标签。一旦它被触发,它就会收集所有500个元素的数组数据。创建一个新的Excel文件并将数据写入A列,单元格1-500

Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged

    If e.ErrorId = 0 AndAlso e.Values.Count > 0 AndAlso e.Values(0) = "True" Then
        Console.WriteLine("About to read the data")
        Dim MyValues() As String = EthernetIPforCLXCom1.Read("VCell_1A_FES_Cycle_Average[0]", 500)
        Console.WriteLine(MyValues.Length & "elements read.")

        Transfer the values to Excel

        Using ExcelPackage As New OfficeOpenXml.ExcelPackage(New IO.FileInfo("C:\Data.xlsx"))
            ExcelPackage.Workbook.Worksheets.Add("test")
            For Index = 0 To MyValues.Length - 1
                Console.WriteLine("Element " & Index & "=" & MyValues(Index))
                Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
                Console.WriteLine("Worksheet OK")
                Dim CellNum As String = "A" & (Index + 1)
                ws.Cells(CellNum).Value = MyValues(Index)
            Next
            ExcelPackage.Save()
        End Using
    End If
End Sub