2017-12-08 23 views
1

CSVファイルに書き込む際に、私が書いている値の1つである{1、$ 1.00,2、$ 3.00 }複数値の列に引用符を付けないでCSVに書き込む

リストのカンマのため、出力で二重引用符で囲まれていますが、この機能を無効にしたいと考えています。私は本当の QuoteNoFieldsを=使用することを提案しているドキュメントを読ん

は、しかし、私はプロジェクトを実行しようとすると、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"); 


     } 

私は私の結果はこのように見えるように期待:1 {、 "タルト"、 "日付のタルト"、

"A"、9600015、 "日付のタルト"、 "日付" 、$ 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、 "日付のタルト"、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

+0

次に出力は何ですか? – SLaks

+0

@slak希望のフォーマットは、値を囲む引用符を付けずに、上記のように{1、$ 1.00,2、$ 2.00}にする必要があります。 –

+1

これは有効なCSVではありません。 – SLaks

答えて

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; 
関連する問題