根据特定列导出csv

时间:2015-08-27 03:54:09

标签: html csv vbscript asp-classic

选择HTML列时,将列数据导出为CSV。此HTML由以下sms.asp页面生成。

 <%Option Explicit%>
<%
Dim strConn, strScriptName

strScriptName = Request.ServerVariables("Script_Name")
' RESPONSE.WRITE strScriptName
strConn = Application("eDSNSMS")

Dim strSQL, boolQuery
strSQL=""

strSQL = Request.QueryString("SQL")
boolQuery = Request.QueryString("boolQuery") 'apa ini

Dim objRS, objConn
set objConn = server.CreateObject("ADODB.Connection")
set objRS = server.CreateObject("ADODB.RecordSet")
%>
<html>
<head>
    <title>DB Admin</title>
    <style type="text/css">
        .button{
            width:70px;
            }
    </style>
    <script language="Javascript">
    function Submit_Query(){
        var SQL = SQL_Form.SQL.value;
        if (SQL=="") return false;
        window.location="<%=strScriptName%>?boolQuery=True&SQL=" + URLEncoding(SQL);
    }
    function Submit_Execute(){
        var SQL = SQL_Form.SQL.value;
        if (SQL=="") return false;
        if (confirm("Are you sure export to csv file?")){
            window.location="csv.asp?SQL=" + SQL;
        }
    }

    function URLEncoding(inputValue)
    {
    inputValue=escape(inputValue);
    re = /\+/gi;
    return inputValue.replace(re, "%2B");
    }

    var selectedIndex=0;
    function selectedValue(i){
        if (selectedIndex==i){
            document.getElementById("Row_" + selectedIndex).bgColor="#e0e0e0";
            document.getElementById("Row_" + selectedIndex).style.color="black";
            selectedIndex=0;
            return;
        }
        else{
            if (selectedIndex!=0){
                document.getElementById("Row_" + selectedIndex).bgColor="#e0e0e0";
                document.getElementById("Row_" + selectedIndex).style.color="black";
            }
            selectedIndex=i;
            document.getElementById("Row_" + selectedIndex).bgColor="darkblue";
            document.getElementById("Row_" + selectedIndex).style.color="white";

        }
    }
    </script>
</head>
<body topmargin=0 leftmargin=0 rightmargin=0>
<table width=100% border=0 cellpadding=2 cellspacing=0 bgColor="silver">
<tr style="font-weight:bold;font-size:16pt"><td>Database Administration</td></tr>
</table>
<%
On Error Resume Next

objConn.Open strConn

objConn.BeginTrans 'apa ini
MainProgram


If Err.Number = 0 Then  
    objConn.CommitTrans 
Else
    objConn.RollbackTrans 
    Response.Write "&nbsp;&nbsp;" & Err.Description
End If

%>
</body>
</html>
<%


If Not objRS Is Nothing Then
    If objRS.State=1 Then 
        objRS.close
    End If
    Set objRS=Nothing
End If


If Not objConn Is Nothing Then
    If objConn.State=1 Then
        objConn.close
    End If
    Set objConn=Nothing
End If


 Private Sub MainProgram ()
    <form name="SQL_Form" onSubmit="return false;">
        <table>
            <tr>
                <td style="font-weight:bold">SQL Statement: </td>
            </tr>
            <tr >
                <td><textarea rows=5 cols=70 name="SQL"><%=Server.HTMLEncode(strSQL)%></textarea><br>
                <input class="button" type=button value="Query" onClick="Submit_Query();">&nbsp;
                <input class="button" type=button value="Export"  onClick="Submit_Execute();">&nbsp;
                <input type=button style="cursor:hand;width:75px" value="Cancel" onClick="window.location='ConsultantMenu.asp'"></td>
            <tr>
        </table>
    </form>
    <%
        If strSQL="" Then
            Exit Sub
        End If


        If boolQuery = "True" Then
            Dim intTotalRecord, intTotalFields, intRow
            Dim x,i
            objRS.CursorLocation = 3  'adUseClient 
            objRS.CursorType = 3      'adOpenStatic
            objRS.LockType = 1         'adLockReadOnly 
            objRS.Open strSQL, objConn
            Set objRS.ActiveConnection = Nothing


            intTotalRecord=objRS.RecordCount

            If intTotalRecord=0 Then
                Response.Write "&nbsp;&nbsp;No Record Found."
            Else

                Response.Write "&nbsp;&nbsp;" & intTotalRecord & " Record(s) Found."
                Response.Write "<br><table border=0 cellpadding=1 cellspacing=1 style=""font-size:10pt"">"

                intTotalFields = objRS.Fields.Count - 1 

                Response.Write "<tr>"
                For Each x in objRS.Fields
                   Response.Write "<td class=""outrim"">" & x.Name & "</td>"
                Next
                Response.Write "</tr>"

                objRS.MoveFirst
                intRow=1
                Do Until objRS.EOF

                    Response.Write "<tr class=""Row_Class"" id=""Row_" & intRow & """ bgColor=""#e0e0e0"" onclick=""selectedValue(" & intRow & ")"">"
                    For i=0 to intTotalFields
                        Response.Write "<td nowrap>" & objRS(i) & "&nbsp;&nbsp;</td>"
                    Next
                    Response.Write "</tr>"

                    intRow=intRow+1

                    objRS.MoveNext
                Loop

                Response.Write "</table>"
            End If

        Else
            Dim intTotalAffectRecord
            objConn.Execute strSQL, intTotalAffectRecord
            Response.Write "&nbsp;&nbsp;" & intTotalAffectRecord & " Record(s) affected."
        End If


End Sub

%>

enter image description here

我想让用户在点击导出按钮时将他们的选择查询导出到csv文件,因此我创建了一个页面调用csv.asp。但是现在我只想要用户选择mtel列然后只有if else语句(标记为'start here'作为注释)工作。我怎么能这样做?

Dim strConn, strScriptName,strSQL

strConn = Application("eDSNSMS")

strSQL = Request.querystring("SQL")

sub Write_CSV_From_Recordset(RS)
  if RS.EOF then
        exit sub
    end if

    dim RX
    set RX = new RegExp
        RX.Pattern = "\r|\n|,|"""

    dim i
    dim Field
    dim Separator,strRow

    do until RS.EOF
        Separator = ""
        for i = 0 to RS.Fields.Count - 1
            Field = RS.Fields(i).Value & ""
            if RX.Test(Field) then
                Field = """" & Replace(Field, """", """""") & """"
            end if

            'start here
            If Left(Field, 2) = "01" and  InStr(Field, "-") <> 0 Then
                if Len(Field) = 11 Then
                 Field = "6" & Field
                 Field = """" & Replace(Field, "-", "") & """"
                else
                 Field = ""
                end if
            elseif Left(Field, 2) = "01"  and  InStr(Field, "-") = 0 then
                if Len(Field) = 10 Then
                 Field = "6" & Field
                else
                 Field = ""
                end if
            elseif Left(Field, 3) = "011"  and  InStr(Field, "-") <> 0 then
                if Len(Field) = 12 Then
                 Field = "6" & Field
                 Field = """" & Replace(Field, "-", "") & """"
                else
                 Field = ""
                end if
            elseif Left(Field, 3) = "011"  and  InStr(Field, "-") = 0 then
                if Len(Field) = 11 Then
                 Field = "6" & Field
                else
                 Field = ""
                end if
            elseif Left(Field, 2) <> "01" and IsNumeric(Field) = true then
                 Field = ""
            elseif Left(Field, 2) <> "01" and InStr(Field, "-") <> 0 then
                 Field = ""
            end if
            Response.Write Separator & Field
            Separator = ","
        next
        if (Field <> "") then
        Response.Write vbNewLine
        end if
        RS.MoveNext
    loop
end sub

Dim objRS, objConn

set objConn = server.CreateObject("ADODB.Connection")
objConn.ConnectionString = strConn
objConn.Open
set objRS = server.CreateObject("ADODB.RecordSet")
objRS.Open strSQL, strConn, 0, 1

Write_CSV_From_Recordset objRS
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "attachment;filename=export.csv"
%>

1 个答案:

答案 0 :(得分:-1)

您可以通过各种方式跟踪列的选择。您可以使用隐藏字段来跟踪asp。当用户选择#include <iostream> #include <string> #include <fstream> using namespace std; int main() { string name ; int score; fstream scoreSheet; scoreSheet.open("path to ur file", fstream::out | fstream::app ); string stayOpen = "y"; while(stayOpen == "y") { cout<<"Enter new entry for name and score"<<endl; cin >> name >> score; scoreSheet << name << score; cout << "Do you want to add another entry? (y/n) "; cin >> stayOpen;} scoreSheet.close(); return 0; } 列时,例如,将隐藏字段的值设置为1.在控制语句中读取此值(如果根据您的话),并做出决定。

例如,当您使用JavaScript选择列时,将隐藏字段isColumnSelected值设置为1.

  • 请记住在asp / HTML中添加hiddend字段。

  • 在asp中读取mtel字段的值并做出决定: