目前我有这个代码:
SqlConnection connection = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true");
SqlCommand Command = connection.CreateCommand();
SqlDataReader SQLRD;
Command.CommandText = "Select * from Attendance";
connection.Open();
SQLRD = Command.ExecuteReader();
//string data = "";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (SQLRD.Read())
{
sb.Append(String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}\n",
SQLRD[0], SQLRD[1], SQLRD[2], SQLRD[3], SQLRD[4], SQLRD[5], SQLRD[6], SQLRD[7]));
}
SQLRD.Close();
connection.Close();
byte[] ar = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content.Type", "application/octet-stream");
Response.AddHeader("Content-Length", ar.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=download.csv");
Response.BinaryWrite(ar);
Response.Flush();
Response.End();
目前,如果我导出它,并且字符串中有逗号,它将在csv文件的另一列中,与其他列不同,我该如何解决?希望有人可以帮助我!
答案 0 :(得分:2)
CSV“spec”(如果可以调用它)允许文本限定符。因此,您可以执行以下操作:
test,test,"this, and this", test
这是四列数据,第三列包含值[this,and this]。
编辑:将格式字符串修改为以下内容:
"\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\"\n"