導出CSV 換行問題。


 

程序方面:

1.Windows 中的換行符"\r\n"

2.Unix/Linux 平台換行符是 "\n"。

3.MessageBox.Show() 的換行符為 "\n"

4.Console 的換行符為 "\n"

為保持平台的通用性,可以用系統默認換行符 System.Environment.NewLine。

 

sql server:換行

select subscriber_id,subscriber_email,company_name from [dbo].[NewsLetterSystem_Subscriber]
where subscriber_id=6967035

update [NewsLetterSystem_Subscriber] 
set company_name=N'ALLY CATERING SOLUTION LIMITED'+CHAR(13)+CHAR(10) +N'安聯餐飲設備有限公司'
where subscriber_id=6967035

update [NewsLetterSystem_Subscriber] 
set company_name=N'ALLY CATERING SOLUTION LIMITED'+CHAR(10) +N'安聯餐飲設備有限公司'
where subscriber_id=6967035


update [NewsLetterSystem_Subscriber] 
set company_name=N'ALLY CATERING SOLUTION LIMITED'+CHAR(13) +N'安聯餐飲設備有限公司'
where subscriber_id=6967035

 

C# 程序:

  public static string ExportReportInCsv(DataTable dt, string filename, string tmpDir) { string tmpFilename = DateTime.Now.Ticks + "_" + filename; string tmpFilenameWithPath = tmpDir + tmpFilename; FileStream fs = new FileStream(tmpFilenameWithPath, System.IO.FileMode.Create, System.IO.FileAccess.Write); //var encoding = GetFileEncodeType(fs);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); StringBuilder sb = new StringBuilder(); //寫出列名稱
            for (int i = 0; i < dt.Columns.Count; i++) { sb.Append(dt.Columns[i].ColumnName.ToString()); if (i < dt.Columns.Count - 1) { sb.Append(","); } } sw.WriteLine(sb.ToString()); for (int i = 0; i < dt.Rows.Count; i++) { sb.Clear(); for (int j = 0; j < dt.Columns.Count; j++) { string rowsText = dt.Rows[i][j].ToString(); rowsText = HandleWrap(rowsText); sb.Append(rowsText); if (j < dt.Columns.Count - 1) { sb.Append(","); } } sw.WriteLine(sb.ToString()); } sw.Close(); fs.Close(); return tmpFilenameWithPath; } public static string HandleWrap(string content) { if (content.Contains(Environment.NewLine)) { var list = content.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); content = string.Join(" ",list); } content = content.Replace(@"\n", " ").Replace(@"\r\n", " ").Replace((char)13,' ').Replace((char)10, ' '); return content; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM