问题描述
我想用我的.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();
}
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(); }
这篇关于C#String替换为“,”,“用于.csv输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!