C# 在多值列上写入不带引号的CSV
我试图在CSVHelper的帮助下写入CSV文件,我正在写入的值之一具有如下值:{1,$1.00,2,$3.00} 由于列表中的逗号,它在输出中是双引号,但我想禁用此功能 我已经阅读了建议使用的文档 QuoteNoFields=true,但是我得到了错误 IReaderConfiguration不包含QuoteNoFields的定义 当我尝试运行项目时 此方法是否已被弃用,或者我是否做了一些不正确的事情C# 在多值列上写入不带引号的CSV,c#,csv,point-of-sale,csvhelper,C#,Csv,Point Of Sale,Csvhelper,我试图在CSVHelper的帮助下写入CSV文件,我正在写入的值之一具有如下值:{1,$1.00,2,$3.00} 由于列表中的逗号,它在输出中是双引号,但我想禁用此功能 我已经阅读了建议使用的文档 QuoteNoFields=true,但是我得到了错误 IReaderConfiguration不包含QuoteNoFields的定义 当我尝试运行项目时 此方法是否已被弃用,或者我是否做了一些不正确的事情 try { using (Text
try
{
using (TextReader reader = File.OpenText(filepath))
using (StreamWriter sw = new StreamWriter(@"C:\temp\mi_imp.txt"))
using (CsvWriter writer = new CsvWriter(sw))
{
var csv = new CsvReader(reader);
csv.Configuration.Delimiter = ",";
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.HeaderValidated = null;
csv.Configuration.MissingFieldFound = null;
csv.Configuration.IgnoreQuotes = true;
csv.Configuration.QuoteNoFields = true;
var usersFromCsv = csv.GetRecords<ProductItem>();
statuslabel.Content = "Status: Reading CSV";
// ********************
string menuItemNametemp;
string buttontxt1temp;
string buttontxt2temp;
string kitchenprintertexttemp;
string customerreceipttexttemp;
string kitchenvideotexttemp;
// ********************
foreach (var item in usersFromCsv)
{
// **** reset to null ****
menuItemNametemp = "";
buttontxt1temp = "";
buttontxt2temp = "";
kitchenprintertexttemp = "";
customerreceipttexttemp = "";
kitchenvideotexttemp = "";
// ************************
// ****** set default values *******
item.Action = "A";
item.PriceLevelID = "{1,$0.00}";
item.SecurityLevel = "0";
item.UseWeightFlag = "0";
item.WeightTare = "0";
item.BarGunCode = "";
// ********* build strings ************
buttontxt1temp = @"""" + item.ButtonTxt1 + @"""" + ",";
buttontxt2temp = @"""" + item.ButtonTxt2 + @"""" + ",";
menuItemNametemp = @"""" + item.MenuItemName + @"""" + ",";
kitchenprintertexttemp = @"""" + item.KitchenPrinterLabel + @""",";
customerreceipttexttemp = @"""" + item.ReceiptText + @""",";
kitchenvideotexttemp = @"""" + item.KitchenVideoText + @""",";
// *************************************
// *********** transfer to object *******
item.ButtonTxt1 = buttontxt1temp;
item.ButtonTxt2 = buttontxt2temp;
item.MenuItemName = menuItemNametemp;
item.KitchenPrinterLabel = kitchenprintertexttemp;
item.ReceiptText = customerreceipttexttemp;
item.KitchenVideoText = kitchenvideotexttemp;
// ****************************************
writer.WriteRecord(item);
}
statuslabel.Content = "Status: Complete";
}
}
catch (Exception ex)
{
LogWriter log = new LogWriter(ex.ToString());
statuslabel.Content = "Status: Error";
textcontent.Text = "";
MessageBox.Show("" + ex, "Error");
}
试试看
{
使用(TextReader=File.OpenText(filepath))
使用(StreamWriter sw=new StreamWriter(@“C:\temp\mi_imp.txt”))
使用(CsvWriter编写器=新CsvWriter(sw))
{
var csv=新的CsvReader(读卡器);
csv.Configuration.Delimiter=“,”;
csv.Configuration.HasHeaderRecord=true;
csv.Configuration.HeaderValidated=null;
csv.Configuration.MissingFieldFound=null;
csv.Configuration.IgnoreQuotes=true;
csv.Configuration.QuoteNoFields=true;
var usersFromCsv=csv.GetRecords();
statuslabel.Content=“状态:读取CSV”;
// ********************
字符串menuItemNametemp;
字符串按钮xt1temp;
字符串按钮xt2temp;
字符串kitchenprintertexttemp;
字符串customerreceipttexttemp;
字符串kitchenvideotexttemp;
// ********************
foreach(usersFromCsv中的变量项)
{
//****重置为空****
menuItemNametemp=“”;
按钮xt1temp=“”;
按钮xt2temp=“”;
kitchenprintertexttemp=“”;
customerreceipttexttemp=“”;
kitchenvideotexttemp=“”;
// ************************
//*******设置默认值*******
项目.行动=“A”;
item.PriceLevelID=“{1,$0.00}”;
item.SecurityLevel=“0”;
item.UseWeightFlag=“0”;
item.WeightTare=“0”;
item.BarGunCode=“”;
//**********生成字符串************
buttontxt1temp=@“”+项。ButtonTxt1++++,“;
buttontxt2temp=@“”+项。ButtonTxt2++++,“;
MenuItemName温度=@“”+项。MenuItemName++++“”,”;
kitchenprintertexttemp=@“”+项。KitchenPrinterLabel+@“”,“;
customerreceipttexttemp=@“”+item.ReceiptText+@“”,”;
kitchenvideotexttemp=@“”+项。KitchenVideoText+@“”,“;
// *************************************
//**********传输到对象*******
item.ButtonXT1=ButtonXT1Temp;
item.ButtonXT2=ButtonXT2Temp;
item.MenuItemName=menuItemNametemp;
item.KitchenPrinterLabel=kitchenprintertexttemp;
item.ReceiptText=customerreceipttexttemp;
item.KitchenVideoText=kitchenvideotexttemp;
// ****************************************
书写记录(项目);
}
statuslabel.Content=“状态:完成”;
}
}
捕获(例外情况除外)
{
LogWriter log=新的LogWriter(例如ToString());
statuslabel.Content=“状态:错误”;
textcontent.Text=”“;
MessageBox.Show(“+ex,”Error”);
}
我希望我的结果如下所示:
“A”,9600015,“日期塔”,“日期塔”,“日期塔”,“日期塔”,“日期塔”,“1,$0.00},76,7,1,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,{215,0},{},0,0,“日期塔”,0,0,0,0,0,{},0
但我得到的却是这样的东西
“A”,9600015,“日期塔”,“日期塔”,“日期塔”,“日期塔”,“{1,$0.00}”,76,7,1,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,0”,“日期塔”,“1,0”,“日期塔”,“0,0,0,0”,“日期塔”,0,0,0,0,{},0经过一些搜索,我找到了解决方案 虽然我认为该方法已被弃用,因为它没有出现在csv.configuration下,但我意识到我应该在writer实例化下设置此配置设置
using (TextReader reader = File.OpenText(filepath))
using (StreamWriter sw = new StreamWriter(@"C:\temp\mi_imp.txt"))
using (CsvWriter writer = new CsvWriter(sw))
var csv = new CsvReader(reader);
// what i was doing previously - incorrect
csv.configuration.quotenofields = true;
// correct solution
writer.configuration.quotenofields = true;
public void CreateCsv()
{
//用记录填充数据
可数数据;
var _csvConfiguration=新的CsvHelper.Configuration.Configuration{
ShouldQuote=(字段,ctx)=>false
};
使用(var file=file.CreateText(_outputFileName))
使用(var csv=new CsvWriter(文件,_csvConfiguration))
{
csv.WriteHeader();
csv.NextRecord();
while(data.MoveNext())
{
csv.WriterRecord(数据当前);
csv.NextRecord();
}
}
}
公共类SampleDataClass
{
公共字符串乘积_id{get;set;}
公共字符串顺序_id{get;set;}
公共字符串用户_id{get;set;}
}
那么您想要什么输出?@slak所需的格式应该是{1,$1.00,2,$2.00},例如,如上所述,值周围没有引号。这不是一个有效的CSV。@SLaks,{}充当具有多个值的某些字段的分隔符。@Ash您可以发布用于编写字段的代码吗。查看看起来您应该能够指定在编写本文时是否要引用该字段。你也可以发布当前的结果(与你想要的相比较),让它更清楚。
public void CreateCsv()
{
// populate data with records
IEnumerable<SampleDataClass> data;
var _csvConfiguration = new CsvHelper.Configuration.Configuration {
ShouldQuote = (field, ctx) => false
};
using (var file = File.CreateText(_outputFileName))
using (var csv = new CsvWriter(file, _csvConfiguration))
{
csv.WriteHeader<SampleDataClass>();
csv.NextRecord();
while (data.MoveNext())
{
csv.WriteRecord(data.Current);
csv.NextRecord();
}
}
}
public class SampleDataClass
{
public string product_id { get; set; }
public string order_id { get; set; }
public string user_id { get; set; }
}