在多值列上写入CSV而不带引号

时间:2017-12-08 01:28:25

标签: c# csv point-of-sale csvhelper

我试图在CSVHelper的帮助下写入CSV文件,我写的其中一个值有这样的值:{1,$ 1.00,2,$ 3.00}

由于列表中的逗号,它在输出中双引号,但我想禁用此功能。

我已阅读建议使用的文档 QuoteNoFields = true,但是我收到了错误

IReaderConfiguration不包含QuoteNoFields的定义 当我尝试运行该项目时。

此方法是否已被弃用,或者我做错了什么?

        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");


        }

我希望我的结果看起来像这样:

“A”,9600015,“Date Tart”,“Date”,“Tart”,“Date Tart”,{1,$ 0.00},76,7,1,0,1,0,0,{}, ,$ 0.00,0,0,1,1,1,0,1,1,“Date Tart”,1,0,{215,0},{},0,0,“Date Tart”,0,0, 0,{},0

但我会得到类似的东西

“A”,9600015,“Date Tart”,“Date”,“Tart”,“Date Tart”,“{1,$ 0.00}”,76,7,1,0,1,0,0,{ } ,, $ 0.00,0,0,1,1,1,0,1,1,“Date Tart”,1,0,“{215,0}”,{},0,0,“Date Tart”, 0,0,0,{},0

2 个答案:

答案 0 :(得分:0)

经过一番搜索,我找到了解决方案。

虽然我认为该方法已被弃用,因为它没有出现在csv.configuration下 - 我意识到我应该在编写器实例化下设置此配置设置。

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;

答案 1 :(得分:0)

    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; }

    }