我生成 csv 内容类型导出。
添加了“A”、“B”、“C”、“D”等列
columns = string.format("""{0}"",""{1}"",""{2}""","A","B","C")
流.writeline(列);
csv 文件已导出,但第一列没有双引号。我需要每列双引号。我尝试了很多方法,其中一种方法很好用,即 columns = string.format(" ""{0}"",""{1}"",""{2}"""," A","B","C") -- 在第一列之前放置空格。
但我需要没有空间的结果。
任何人都可以帮助解决这个问题。
示例代码:
Public Class CSVExporter
Public Shared Sub WriteToCSV(personList As List(Of Person))
Dim attachment As String = "attachment; filename=PerosnList.csv"
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ClearHeaders()
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.AddHeader("content-disposition", attachment)
HttpContext.Current.Response.ContentType = "text/csv"
HttpContext.Current.Response.AddHeader("Pragma", "public")
WriteColumnName()
For Each item As Person In personList
WriteUserInfo(item)
Next
HttpContext.Current.Response.[End]()
End Sub
Private Shared Sub WriteUserInfo(item As Person)
Dim strb As New StringBuilder()
AddComma(String.Format("""{0}""", item.Name), strb)
AddComma(String.Format("""{0} """, item.Family), strb)
AddComma(String.Format("""{0} """, item.Age.ToString()), strb)
AddComma(String.Format("""{0:C2}""", item.Salary), strb)
HttpContext.Current.Response.Write(strb)
HttpContext.Current.Response.Write(Environment.NewLine)
End Sub
Private Shared Sub AddComma(item As String, strb As StringBuilder)
strb.Append(item.Replace(",", " "))
strb.Append(" ,")
End Sub
Private Shared Sub WriteColumnName()
Dim str As String = """Name"", ""Family"", ""Age"", ""Salary"""
HttpContext.Current.Response.Write(str)
HttpContext.Current.Response.Write(Environment.NewLine)
End Sub
End Class