在vbscript中删除CSV文件中的空行

在vbscript中删除CSV文件中的空行,vbscript,asp-classic,Vbscript,Asp Classic,我正在尝试导出CSV文件,这是我的代码。 Excel CSV文件有一些空单元格,我想删除它们,所以我添加了一些代码(我在注释中标记了这些代码)。 程序没有任何错误,但空单元格仍然存在。 希望有人能告诉我出了什么问题 <%Option Explicit%> <% Dim strConn, strScriptName,strSQL strConn = Application("eDSNSMS") strSQL = Request.querystring("SQL") sub

我正在尝试导出CSV文件,这是我的代码。 Excel CSV文件有一些空单元格,我想删除它们,所以我添加了一些代码(我在注释中标记了这些代码)。 程序没有任何错误,但空单元格仍然存在。 希望有人能告诉我出了什么问题

<%Option Explicit%>
<%
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

    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

            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
        Response.Write vbNewLine
        RS.MoveNext
    loop
end sub

Dim objRS, objConn, objFile, objFSO, strNewContents

 ' Const ForReading = 1
 ' Const ForWriting = 2

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

 ' Set objFSO = CreateObject("Scripting.FileSystemObject")
 ' Set objFile = objFSO.OpenTextFile("export.csv", ForReading)

 ' Do Until objFile.AtEndOfStream
     ' strLine = objFile.Readline
     ' strLine = Trim(strLine)
     ' If Len(strLine) > 0 Then
         ' strNewContents = strNewContents & strLine & vbCrLf
     ' End If
 ' Loop

 ' objFile.Close

 ' Set objFile = objFSO.OpenTextFile("export.csv", ForWriting,true)
 ' objFile.Write strNewContents
 ' objFile.Close

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

0那么
'strNewContents=strNewContents&strLine&vbCrLf
"完"
'循环
'objFile.Close
'设置objFile=objFSO.OpenTextFile(“export.csv”,用于写入,true)
'objFile.Write strNewContents
'objFile.Close
从\u记录集objRS写入\u CSV\u
Response.ContentType=“text/csv”
Response.AddHeader“内容处置”、“附件;文件名=export.csv”
%>

如果您不想将空字段写入CSV,请在写入前测试该字段。例如,更改:

Response.Write Separator & Field
致:


但是,除非此特定列/字段中的所有值都为空,否则执行此操作将使CSV对齐无效。

谢谢,这很有帮助。我已尝试过,但单元格仍然为空。。我在
字段=“”
(空手机)中创建了一些不需要的电话号码,因此我希望删除我创建的空手机…@bond我看到你添加了一个屏幕截图。您是否试图阻止写入空行?是的,我已将不需要的字段设置为空,这就是单元格/行为空的原因。因此,我想删除空单元格并将记录向上移动到下面。@bondOK,这会改变情况。你不能一次写一个字段。你需要一次写一行。因此,您需要将字段值连接到表示CSV中一行的字符串中,而不是
Response.Write Separator&Field
。类似于:
strow=strow&Separator&Field
。然后你可以写你的行(
Response.write strow&vbNewline
),只要它包含数据。然后记录就变成了我现在在问题中的屏幕截图。是否可以使格式与以前相同?
If Len(Field) > 0 Then
    Response.Write Separator & Field
End If