C#String替换为“,”,“用于.csv输出 [英] C# String Replace , to "," for .csv output

查看:118
本文介绍了C#String替换为“,”,“用于.csv输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的.csv字符串中的,替换。



例如:



更改此

1,S0000093,GNL-000099,Aarpee Hygiene Pvt Ltd,H0010AW0101,G1150505010,G1150504070406,14,GNL-005111,35637153767



到此

1,S0000093,GNL-000099,Aarpee Hygiene Pvt Ltd,H0010AW010,G1150505010,G1150504070406,14 ,GNL-005111,,35637153767



我一直在尝试使用line.Replace(\,\\\ \\ )+ \;但它可以正常工作。



任何帮助都将不胜感激....在此先感谢...



我的代码



  private   void  ExportToCsv( string  fileOut)
{
string text = ;
string text2 = 数据源= +
(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()。GetName()。CodeBase))+
\\freedom.sdf; Persist Security Info = False;
SqlCeConnection sqlCeConnection = new SqlCeConnection(text2);
sqlCeConnection.Open();
string text3 = select * from Test ;
SqlCeCommand sqlCeCommand = new SqlCeCommand(text3,sqlCeConnection);
SqlCeDataReader dr = sqlCeCommand.ExecuteReader();
DataTable schemaTable = dr.GetSchemaTable();
StreamWriter streamWriter = new StreamWriter(fileOut, false );

streamWriter.WriteLine( string .Format( Sr.No,销售订单号,装运号,客户名称,物料,捆绑号,卷号,数量,透明ID,验证者,记录ID new object [ 0 ]));
while (dr.Read())
{
string text4 = ;
for int i = 0 ; i < dr.FieldCount; i ++)
{
object obj = dr.GetValue(i);
if (obj!= null
{
text4 + = obj.ToString();
}
else
{
text4 =(text4 ?? );
}
if (i < dr.FieldCount - 1
{
// text4 + = text;
text4 + = \ + text.Replace ( \ \\)+ \ ;

}
}
streamWriter.WriteLine(text4);
}
streamWriter.Close();
sqlCeConnection.Close();
}

解决方案

你没有替换你要求更换的东西。



你需要用,代替: line.Replace(,,@ ,); 然后你可以在开头和结尾添加缺少的,使用 string.Format ,效率不高字符串连接(字符串是不可变的,这就是原因)。



之前,你有另一种解决方案,你首先用,分割字符串然后再次在新字符串中加入数组,这次添加引号。为什么你忽略了这个解决方案?



毕竟,所有这些字符串操作和CSV本身都是低技术的,你必须自己解决,至少要一些技术节目。只需要耐心和注意细节。并使用调试器。



-SA


更改此行



 text4 + =   \  + text.Replace(  \  \\)+   \; 





到此



 text4 + =   \ + text4.Replace(  \   \\)+   \; 


I want to replace , with a "," in my .csv string.

For Example:

Change this
1,S0000093,GNL-000099,Aarpee Hygiene Pvt Ltd,H0010AW0101,G1150505010,G1150504070406,14,GNL-005111,,35637153767

to this
1,"S0000093","GNL-000099","Aarpee Hygiene Pvt Ltd","H0010AW010","G1150505010","G1150504070406","14","GNL-005111","","35637153767"

I have been trying the line.Replace("\"","\"\"")+"\""; but it get anything working properly.

Any help would be appreciated....Thanks in Advance...

my code

private void ExportToCsv(string fileOut)
        {
            string text = ",";
            string text2 = "Data Source=" +
                (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) +
                "\\freedom.sdf;Persist Security Info=False";
            SqlCeConnection sqlCeConnection = new SqlCeConnection(text2);
            sqlCeConnection.Open();
            string text3 = "select * from Test";
            SqlCeCommand sqlCeCommand = new SqlCeCommand(text3, sqlCeConnection);
            SqlCeDataReader dr = sqlCeCommand.ExecuteReader();
            DataTable schemaTable = dr.GetSchemaTable();
            StreamWriter streamWriter = new StreamWriter(fileOut, false);
          
            streamWriter.WriteLine(string.Format("Sr.No,Sales Order No,Shipment No,Cust. Name,Item,Bundle No.,Roll No.,Qty,Trans Id,Validated By,Record Id", new object[0]));
            while (dr.Read())
            {
                string text4 = "";
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    object obj = dr.GetValue(i);
                    if (obj != null)
                    {
                        text4+= obj.ToString();
                    }
                    else
                    {
                        text4 = (text4 ?? "");
                    }
                    if (i < dr.FieldCount - 1)
                    {
                        //text4 += text;
                        text4 += "\"" + text.Replace("\"", "\"\"") + "\"";
                        
                    }
                }
                streamWriter.WriteLine(text4);
            }
            streamWriter.Close();
            sqlCeConnection.Close();
        }

解决方案

You are not replacing what you claim to replace.

You need to replace , with ",": line.Replace(",", @""","""); then you can add missing "" at the beginning and the end, using string.Format, not inefficient string concatenation (strings are immutable, that's why).

Before, you got alternative solution where you first split the string by "," and then join the array in new string again, this time adding quotation marks. Why did you ignore this solution?

After all, all those string manipulation and CSV itself is low-tech you have to solve by yourself, to tech yourself at least some programming. Just use some patience and attention for detail. And use the debugger.

—SA


Change this line

text4 += "\"" + text.Replace("\"", "\"\"") + "\"";



to this

text4 += "\"" + text4.Replace("\"", "\"\"") + "\"";


这篇关于C#String替换为“,”,“用于.csv输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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