2014-09-27 69 views
0

如何使用vbs在csv文件中創建列標題,用戶以後可以通過單擊列標題進行排序?例如使用vbs爲csv文件創建列標題

USername  Password 
Texas   123456 
Mariot   abcdef 
andres   abc123 

我創建了一個csv文件,但仍然無法創建列標題。 在此先感謝!

+0

通過將其寫入文件。如果你不能做這麼基本的事情,你在問什麼?編寫你的頭文件,寫下文件的其餘部分。 – Noodles 2014-09-27 10:05:00

+0

對不起,我已經編輯了這個問題。我可以創建一個可以創建標題列的csv文件,因此當用戶單擊USername時,它將按升序或降序對名稱進行排序。那可能嗎? – user3696022 2014-09-27 11:55:08

+0

編號CSV只是具有特定結構的純文本。任何類型的點擊或排序都取決於您打開文件的程序。 – 2014-09-27 16:14:01

回答

0

程序文件Music.hta

<html> 
<head></head> 
<body> 
<OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83" 
    ID=dsoMacro5 WIDTH=0 HEIGHT=0> 
    <PARAM NAME="DataURL" VALUE="music.txt"> 
    <PARAM NAME="UseHeader" Value="True"> 
    <PARAM NAME="FieldDelim" VALUE="&#09;"> 
    <PARAM NAME="Sort" Value="Title"> 
</OBJECT> 
<h3>My Music Database</h3> 
<h4>Select a button to filter list</h4> 
<p>To search for a word in the Title field use <i>* word *</i>. To search for the first word in a field use <i>Word *</i> or the last word use <i>* word</i>. To search for a string within a word or word use <i>*partialword*</i>. Searches are case sensitive.</i></p> 
<p><INPUT Name=tb1 TYPE=Text Value=""> <INPUT ID=cmdNavFirst TYPE=BUTTON VALUE="  Search  " onclick="dsoMacro5.object.filter='Title=' + tb1.value;dsoMacro5.reset()"></p> 
<p><INPUT ID=cmdNavFirst TYPE=BUTTON VALUE=" Sort Book " onclick="dsoMacro5.object.sort='Book';dsoMacro5.reset()"></p> 
<hr class="body"> 
<TABLE ID=tblMacro2 DATASRC=#dsoMacro5 OnRowEnter=Alert(tblMacro2.row)> 
<THEAD> 
<TR> 
<TD WIDTH="20%"><b>Number</b></TD> 
<TD WIDTH="60%"><b>Title</b></TD> 
<TD WIDTH="20%"><b>Book</b></TD> 
</TR> 
</THEAD> 
<TBODY> 
<TR> 
<TD WIDTH="20%"><SPAN DATAFLD=Number></SPAN></TD> 
<TD WIDTH="60%"><SPAN DATAFLD=Title></SPAN></TD> 
<TD WIDTH="20%"><SPAN DATAFLD=Book></SPAN></TD> 
</TR> 
</TBODY> 
</TABLE> 
</body> 
</html> 

數據文件Music.txt

Number Title Book 
1 One A song 
2 Two A another song 
3 Three A yet another song 
4 Four Yes it's a song 
0

這個程序排序

Set Arg = WScript.Arguments 
set WshShell = createObject("Wscript.Shell") 
Set Inp = WScript.Stdin 
Set Outp = Wscript.Stdout 

Sub Sort 
    Set rs = CreateObject("ADODB.Recordset") 
    If Arg(1) = "n" then 
    With rs 
     .Fields.Append "SortKey", 4 
     .Fields.Append "Txt", 201, 5000 
     .Open 
     Do Until Inp.AtEndOfStream 
      Lne = Inp.readline 
      SortKey = Mid(Lne, Arg(3), Arg(4) - Arg(3)) 
      If IsNumeric(Sortkey) = False then 
       Set RE = new Regexp 
       re.Pattern = "[^0-9\.,]" 
       re.global = true 
       re.ignorecase = true 
       Sortkey = re.replace(Sortkey, "") 
      End If 
      If IsNumeric(Sortkey) = False then 
       Sortkey = 0 
      ElseIf Sortkey = "" then 
       Sortkey = 0 
      ElseIf IsNull(Sortkey) = true then 
       Sortkey = 0 
      End If 
      .AddNew 
      .Fields("SortKey").value = CSng(SortKey) 
      .Fields("Txt").value = Lne 
      .UpDate 
     Loop 
     If Arg(2) = "a" then SortColumn = "SortKey ASC" 
     If Arg(2) = "d" then SortColumn = "SortKey DESC" 
     .Sort = SortColumn 
     Do While not .EOF 
      Outp.writeline .Fields("Txt").Value 
      .MoveNext 
     Loop 
    End With 
    ElseIf Arg(1) = "t" then 
    With rs 
     .Fields.Append "SortKey", 201, 260 
     .Fields.Append "Txt", 201, 5000 
     .Open 
     Do Until Inp.AtEndOfStream 
      Lne = Inp.readline 
      SortKey = Mid(Lne, Arg(3), Arg(4) - Arg(3)) 
      .AddNew 
      .Fields("SortKey").value = SortKey 
      .Fields("Txt").value = Lne 
      .UpDate 
     Loop 
     If Arg(2) = "a" then SortColumn = "SortKey ASC" 
     If Arg(2) = "d" then SortColumn = "SortKey DESC" 
     .Sort = SortColumn 
     Do While not .EOF 
      Outp.writeline .Fields("Txt").Value 
      .MoveNext 
     Loop 
    End With 
    ElseIf Arg(1) = "tt" then 
    With rs 
     .Fields.Append "SortKey", 201, 260 
     .Fields.Append "Txt", 201, 5000 
     .Open 
     Do Until Inp.AtEndOfStream 
      Lne = Inp.readline 
      SortKey = Trim(Mid(Lne, Arg(3), Arg(4) - Arg(3))) 
      .AddNew 
      .Fields("SortKey").value = SortKey 
      .Fields("Txt").value = Lne 
      .UpDate 
     Loop 
     If Arg(2) = "a" then SortColumn = "SortKey ASC" 
     If Arg(2) = "d" then SortColumn = "SortKey DESC" 
     .Sort = SortColumn 
     Do While not .EOF 
      Outp.writeline .Fields("Txt").Value 
      .MoveNext 
     Loop 
    End With 
    End If 
End Sub 

排序

濾波器排序{N | T | TT} {A | d} STARTCOLUMN ENDCOLUMN

濾波器NA 0 8

排序文件

n - extracts a number from the columns specified. Looks for the first number. 
t - extracts a text string including spaces from the columns specified. 
tt - extracts a text string discarding leading and trailing spaces from the columns specified. 
a - sorts acending 
d - sorts decending 
startcolumn - the starting column 
endcolumn - the ending column 

過濾器排序n a 1 10 <「%windir%\ win.ini」