我正在尝试将数据从gridview导出到csv文件。我在谷歌搜索它,我也有一些很好的例子。但是当我在记事本中打开生成的csv文件时,我总是在每条记录的末尾添加额外的逗号。我想删除那个逗号......你能帮我吗......
这里是我点击csv按钮的代码
Protected Sub btnexptocsv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnexptocsv.Click
Try
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition",
"attachment;filename=Complaint_Details.csv")
Response.Charset = ""
Response.ContentType = "application/text"
grd_ComplaintDetails.AllowPaging = False
Dim sb As StringBuilder = New System.Text.StringBuilder()
For k As Integer = 0 To grd_ComplaintDetails.Columns.Count - 1
'add separator
sb.Append(grd_ComplaintDetails.Columns(k).HeaderText + ","c)
Next
'append new line
sb.Append(vbCr & vbLf)
For i As Integer = 0 To grd_ComplaintDetails.Rows.Count - 1
For k As Integer = 0 To grd_ComplaintDetails.HeaderRow.Cells.Count - 1
'add separator
'sb.Append(grd_ComplaintDetails.Rows(i).Cells(k).Text + ","c)
sb.Append(grd_ComplaintDetails.Rows(i).Cells(k).Text.Replace(",", "") + ",")
Next
'append new line
sb.Append(vbCr & vbLf)
Next
Response.Output.Write(sb.ToString())
Response.Flush()
Response.End()
Catch ex As Exception
div_Msg.InnerText = "Can not generate CSV file"
End Try
End Sub
提前致谢...:)
答案 0 :(得分:1)
你可以做两件事:
答案 1 :(得分:0)
只需检查k是否小于总列数:
For k As Integer = 0 To grd_ComplaintDetails.HeaderRow.Cells.Count - 1
If k < grd_ComplaintDetails.HeaderRow.Cells.Count - 1 Then
sb.Append(grd_ComplaintDetails.Rows(i).Cells(k).Text.Replace(",", "") + ",")
Else
sb.Append(grd_ComplaintDetails.Rows(i).Cells(k).Text.Replace(",", ""))
End If
Next
答案 2 :(得分:0)
你可以简单地尝试在StringBuilder上将长度减少1
sb.Length = sb.Length - 1
您可以在此处查看输出 - &gt; http://dotnetpad.net/ViewPaste/P9wkTEYE-UuquTeVUmwKug
答案 3 :(得分:0)
在代码中进行以下更改
Protected Sub btnexptocsv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnexptocsv.Click
Try
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=Complaint_Details.csv")
Response.Charset = ""
Response.ContentType = "application/text"
grd_ComplaintDetails.AllowPaging = False
Dim sbFile As StringBuilder = New System.Text.StringBuilder()
Dim sb As StringBuilder = New System.Text.StringBuilder()
For k As Integer = 0 To grd_ComplaintDetails.Columns.Count - 1
'add separator
sb.AppendFormat(",{0}", grd_ComplaintDetails.Columns(k).HeaderText)
Next
'append new line
sb.Remove(0, 1)
sb.Append(vbCr & vbLf)
sbFile.Append(sb.ToString())
For i As Integer = 0 To grd_ComplaintDetails.Rows.Count - 1
sb = New System.Text.StringBuilder()
For k As Integer = 0 To grd_ComplaintDetails.HeaderRow.Cells.Count - 1
'add separator
sb.AppendFormat(",{0}", grd_ComplaintDetails.Rows(i).Cells(k).Text.Replace(",", ""))
Next
sb.Remove(0, 1)
'append new line
sb.Append(vbCr & vbLf)
sbFile.Append(sb.ToString())
Next
Response.Output.Write(sbFile.ToString())
Response.Flush()
Response.End()
Catch ex As Exception
div_Msg.InnerText = "Can not generate CSV file"
End Try
End Sub
答案 4 :(得分:0)
首先,您应该使用csv字段转换器。最简单的形式:
"\"" " field.Replace("\"", "\"\") + "\""
引号是可选的,但它们允许您在字段中包含逗号和crs以及换行符。然后你不必更换它们。
将它们收集到一个数组中 - 使用linq函数在这里会非常棒 - 然后加入它们(ala Juann):
String.Join(",", field.select(m => "\"" + field.Replace("\"", "\"\") + "\"") + "\r\n";
显然将其转换为VB。