English 中文(简体)
• 如何将含有 com的数值写到CSV在C#中的档案中?
原标题:How to write a value which contain comma to a CSV file in c#?

我正在使用数据表储存数据。

我将数据从数据表输出到CSV档案。

有时可能含有 com(,)的数值,因此这些数值不正确出口。

For Example

数值为<代码>9,11,32”。 我必须出口。

But when I do the first column conatins 9 then in next column 11.

我想在CSV档案的同一栏中显示9,11,32<>/code>。 我如何这样做?

问题回答

简单地把你的数据放在像“......”+你的数据+“”。

StringWriter csv = new StringWriter();
// Generate header of the CSV file
csv.WriteLine(string.Format("{0},{1}", "Header 1", "Header 2"));
// Generate content of the CSV file
foreach (var item in YourListData)
{
    csv.WriteLine(string.Format("{0},{1}", item.Data1, """ + item.Data2 + """));
}

return File(new System.Text.UTF8Encoding().GetBytes(csv.ToString()), "application/csv", string.Format("{0}{1}", "YourFileName", ".csv"));

例如: 您的数据2 可能含有 com子“,”

  1. Fields with embedded commas must be delimited with double-quote characters.

    <>光线>

    1. abc, xyz
    2. pqr

<<>strong>csv edition:

"abc, xyz" , pqr
  1. 含有<条码>双重引用的特征的方面必须以双重引用为重,嵌入的双重引用必须分别以连续的双字母为代表。

    http://www.ohchr.org。

    Welcome to "My World"
    

    <<>strong>csv edition:

    "Welcome to ""My World"""
    

如果在数据中有一个ma子,你需要将。 引言如下:

"123,56","A dog, cat and a frog"
StringBuilder sb = new StringBuilder();          
        foreach (DataColumn col in dt.Columns)
        {
            if (col.ColumnName.Contains(","))
            {
                sb.Append(String.Format(""{0}",", col.ColumnName));
            }
            else
            {
                sb.Append(String.Format("{0},", col.ColumnName));
            }
        }

双重引用的价值。

string ValueToEscape = "a,b";

""" + ValueToEscape + """

CSV Output = a,b

书写使双面引述的价值分离,没有空间为 com子分离的数值形成一栏。

页: 1 页: 1 价值Code01 &Val1,Val2,Val3。 制作记本栏下特定数据记号的记号,并用剪辑予以保存。

Code,Description
Code01,"Val1,Val2,Val3"




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...