如何在经典asp中为csv文件标记图表数据

时间:2011-12-23 08:43:39

标签: scripting asp-classic vbscript

下面是我从URL读取的图表数据的数据样本

ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8

现在,我希望将此数据保存到csv文件中,如下所示

Time    A   B   c
25      3   1   8
75      8   9   7
100     6   7   3
125     7   8   9
150     5   4   56
175     3   7   9
200     2   2   111
225     7   5   8

实际上我必须首先将数据读入一个数组,其中每个元素将包含一行数据文件。现在我将不得不按空格分割每个元素并将其存储在二维数组中,现在每个每个数组的索引我必须创建一个逗号分隔线,然后在行尾添加“\ n”,最后将其保存到csv文件。

我不太了解vb-script和经典asp的语法就是我遇到这个问题的原因。请帮帮我

1 个答案:

答案 0 :(得分:1)

为了帮助您入门:

Sub doReOrder(sFSpecI, sFSpecO, sCol1)
  Dim oTS    : Set oTS = goFS.OpenTextFile(sFSpecI)
  Dim sData  : sData   = oTS.ReadLine() ' ARRAY ' ' 3 8
  Dim aParts : aParts  = Split(sData, " ")
  Dim nCols  : nCols   = CByte(aParts(3)) ' Count vs UBound vs Data!
  Dim nRows  : nRows   = CByte(aParts(4))
  Dim nRows2 : nRows2  = nRows + 1
  oTS.SkipLine ' Y
  ' get table in one string, prepend col1 name, clean '
  sData  = "'" & sCol1 & "' " & Replace(oTS.ReadAll(), vbCrLf, " ")
  sData  = Trim(Replace(sData, "'", """"))
  ' get table in flat array
  aParts = Split(sData, " ")
  oTS.Close
  Set oTS = goFS.CreateTextFile(sFSpecO, True)
' WScript.Echo Join(aParts, "|")
  ReDim aData(nCols) ' hold one (new) row to prep for Join
  Dim nRow
  For nRow = 0 To nRows
      Dim nCol
      For nCol = 0 To nCols
          ' magic column hopping
          aData(nCol) = aParts(nRow + nCol * nRows2)
      Next
      oTS.WriteLine Join(aData, ",")
  Next
  oTS.Close
End Sub

测试:

  Dim sFSpecI : sFSpecI = "..\Data\f1.txt"
  Dim sFSpecO : sFSpecO = "..\Data\f1.csv"
  Dim sCol1   : sCol1   = "Time" ' dangerous - possibly reserved in SQL
  WScript.Echo goFS.OpenTextFile(sFSpecI).ReadAll()
  doReOrder sFSpecI, sFSpecO, sCol1
  WScript.Echo goFS.OpenTextFile(sFSpecO).ReadAll()

输出:

ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8

"Time","A","B","C"
25,3,1,8
75,8,9,7
100,6,7,3
125,7,8,6
150,5,4,56
175,3,7,9
200,2,2,111
225,7,5,8