具有n行和列的动态数组

时间:2016-02-04 18:51:11

标签: arrays excel escript

我必须读取一个excel,它可以有'n'行和'n'列,并将数据存储在一个数组中进行处理。

例如以下内容:

Author    Book    No    Value
ABC       A       1     100
DEF       D       2     20

我想将标题放在一个单独的数组中,将信息行放在另一个数组中。

这是我到目前为止所做的:

假设数据从Excel工作表中的第13行开始 -

var i, j, k ,l,m ,n;
i=13; j=1, k=0; m=0; n=0;  // i represents the row and j represents excel column(A)
while(EApp.ActiveSheet.Cells(12,j).Value!="")
{
    j=j+1; // Counting the Number of Columns (Header)
}
j=j-1; //decrementing the counter j by 1
k=j;
while(j!=0)
{
    ArrField[j]=EApp.ActiveSheet.Cells(12,j).Value;
    j=j-1;
}

这是我尝试的,输出是按预期的,但是有更好的方法来编码吗?最好是变量较小。

while(EApp.ActiveSheet.Cells(i,1).Value!="undefined") 
{
    m=k;
    j=0;
    ArrData[i]=new Array(m);
    while(m!=0)
    {                                       
        ArrData[n][j]=EApp.ActiveSheet.Cells(i,m).Value;
        m=m-1;
        j=j+1;
    }
    n=n+1;
    i=+1;
}

此外,我需要读取数组并将它们存储到另一个系统的相应字段中。我在这里有点迷失。

示例代码:(像这样的东西)

SetFieldValue(ArrField[0],ArrData[0][0]);

数组输出:

Value,No,Book,Author (Header got in reverse order)

100,1,A,ABC,20,2,D,DEF (2 rows of Data also got in reverse order)

任何建议专家?

1 个答案:

答案 0 :(得分:0)

你可以以一种非常好的方式使用csv作为动态数组。 这是我的示例代码。我建议根据您的需要重写它并添加一些错误处理。

以下是表格数据:

ID  AUTHOR  BOOK    NO  VALUE
1   ABC     A       1   100
2   DEFG    D       2   20
3   XYZ     Q       3   55

以下是代码:

Sub test1()

    Dim arrayname As String
    Dim mytestW As Variant
    Dim mytestR As Variant '-->your array you can work with
    'give your array a arrayname = authorbook (for example)
    arrayname = "authorbook"

    mytestW = dataintoarray(arrayname)
    mytestR = AsArray(arrayname, 2) '-->get the second row
End Sub

以下是功能:

  Function dataintoarray(myarray As String) As Variant

  Dim res As Variant
  Dim wb As Workbook, ws As Worksheet
  Dim LastRow As Long, LastCol As Long
  Dim iRow As Integer, jCol As Integer
  Dim OutputFileNum As Integer
  Dim PathName As String
  Dim Line As String
      Line = ""

  Set wb = ThisWorkbook
  Set ws = wb.Worksheets("Sheet1")

  PathName = Application.ActiveWorkbook.Path
  OutputFileNum = FreeFile

  Open PathName & "\" & myarray & ".csv" For Output Lock Write As #OutputFileNum

  LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

  For iRow = 1 To LastRow
      For jCol = 1 To LastCol
         Line = Line & ws.Cells(iRow, jCol).Value & ","
      Next jCol
      Print #OutputFileNum, Line
      Line = ""
  Next iRow

  Close OutputFileNum

  End Function


Function AsArray(myarray As String, index As Integer) As Variant

    Dim res As Variant
    Dim wb As Workbook, ws As Worksheet
    Dim objFSO As Object, objFile As Object
    Dim sLine As String

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet1")

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(Application.ActiveWorkbook.Path & "\" & myarray & ".csv", 1)
    Do Until objFile.AtEndOfStream
          sLine = objFile.ReadLine
          'the col of the ID = 0
          If Split(sLine, ",")(0) = index Then
              AsArray = Split(sLine, ",")
          End If
    Loop
    objFile.Close

 End Function

现在您将动态数组保存在mytestR中。 你可以这样玩:

 ?mytestR(0)
 2
 ?mytestR(1)
 DEFG
 ?mytestR(2)
 D
 ?mytestR(3)
 2
 ?mytestR(4)
 20
相关问题