0
目前我有這個代碼:如何在導出包含逗號的csv文件時避免錯誤?
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文件中的另一列,並且不一樣的人,怎麼樣我能解決這個問題嗎?希望可以有人幫幫我!
那麼我suppossed辦?因爲我不知道如何克服這個錯誤,任何例子?對不起=/ –
@Terence - 如果你用保羅所說的用雙引號括住所有的字符串,那麼你會好起來的--Excel將把逗號視爲屬於封閉的字符串而不是分隔符。 – slugster
@slugster問題是如何把我的代碼中的引號我試圖插入引號,但有很多錯誤。 –