C#CSV分隔符格式问题,如何正确格式化 [英] C# CSV Delimiter formatting Problem, how to format properly

查看:419
本文介绍了C#CSV分隔符格式问题,如何正确格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何格式化CSV文件,因为我在Excel上打开它时遇到问题,例如。价格(整数)分开,例如Php 7,100必须在一个单元格中,但它显示在2个单元格中,如7 100,如何修复它







How to format the CSV file, coz i got problem in opening it on excel, E.g. Price (integer) separates, for Example Php 7,100 must be in one cell , but it showed in 2 cells, like 7 100, how to fix it



// create one file gridview.csv in writing mode using streamwriter
StreamWriter sw = new StreamWriter("gridview.csv");
// now add the gridview header in csv file suffix with "," delimeter except last one
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
    sw.Write(dataGridView1.Columns[i].HeaderText);
    if (i != dataGridView1.Columns.Count)
    {
        sw.Write(",");
    }
}
// add new line
sw.Write(sw.NewLine);
// iterate through all the rows within the gridview
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
    // iterate through all colums of specific row
    for (int i = 0; i < dataGridView1.Columns.Count; i++)
    {
        // write particular cell to csv file
        sw.Write(dr.Cells[i].Value +",");
 
        if (i != dataGridView1.Columns.Count)
        {
           // sw.Write(",");
        }
    }
    // write new line
    sw.Write(sw.NewLine);
}
// flush from the buffers.
sw.Flush();
// closes the file
sw.Close();

推荐答案

因为它是逗号分隔文件,所以不能放入逗号而不是分隔符: https://en.wikipedia.org/wiki/Comma-separated_values [ ^ ]



最简单的方法(但不是唯一的方法)是删除他们在你创建csv时: https://social.msdn.microsoft.com/Forums/en-US/7c293b41-e26d-4d46-9b26-d95cdc8d30a2/c-streamwriter-how-to-ignore- read-逗号字符?forum = csharplanguage [ ^ ]
As it is a comma delimited file you cannot put in commas that are not meant as delimiters: https://en.wikipedia.org/wiki/Comma-separated_values[^]

The easiest way (but not the only way) is to remove them as you are creating the csv: https://social.msdn.microsoft.com/Forums/en-US/7c293b41-e26d-4d46-9b26-d95cdc8d30a2/c-streamwriter-how-to-ignore-reading-comma-characters?forum=csharplanguage[^]


这篇关于C#CSV分隔符格式问题,如何正确格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆