在vbscript中删除CSV文件中的空行
Delete empty rows in CSV files in vbscript
我正在尝试导出 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"
%>
如果您不想将空字段写入 CSV,请在写入之前测试该字段。例如,更改:
Response.Write Separator & Field
收件人:
If Len(Field) > 0 Then
Response.Write Separator & Field
End If
但是除非此特定 column/field 中的 所有 值都是空白,否则这样做会导致 CSV 对齐失败。
我正在尝试导出 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"
%>
如果您不想将空字段写入 CSV,请在写入之前测试该字段。例如,更改:
Response.Write Separator & Field
收件人:
If Len(Field) > 0 Then
Response.Write Separator & Field
End If
但是除非此特定 column/field 中的 所有 值都是空白,否则这样做会导致 CSV 对齐失败。