【问题标题】:Write a text file with tab delimiter in .Net Core在 .Net Core 中编写带有制表符分隔符的文本文件
【发布时间】:2019-10-10 09:19:10
【问题描述】:

您好,您有任何指南、工作帮助或逐步导出到制表符分隔的文本吗?我正在使用 Asp.Net Core 2.2 MVC EF。我想从我的 table.。我想要一个按钮,用户单击此 DownloadFile 操作将触发该按钮。

public object DownloadFile()
        {
            var payments = new List<BdoPE>
            {
                new BdoPE
                {
                    DocDateInDoc = "01/01/2019",
                    DocType = "DZ",
                    CompanyCode = "3000",
                    PosDateInDoc = "01/01/2019",
                    FiscalPeriod = "01",
                    CurrentKey = "PHP",
                    RefDocNum = "Over-The-Counter",
                    DocHeadT = "BDO",
                    PosKeyInNextLine = "40",
                    AccMatNextLine = "11231131",
                    AmountDocCur = "0000000010050",
                    ValDate = "01/01/2019",
                    AssignNum = "EEA",
                    ItemText = "1000136212  ",
                    PosKeyInNextLine2 = "15",
                    AccMatNextLine2 = "0115027FF",
                    AmountDocCur2 = "0000000010050",
                    BaseDateDueCal = "01/01/2019",
                    ItemText2 = "1000136212"
                },
            };

            // I want this part to let the user select where they want to save the text file.
            using (var writer = new StreamWriter("path\\to\\file.txt")) // not static location like this one.

            using (var csv = new CsvWriter(writer))
            {
                csv.WriteHeader<BdoPE>();
                csv.WriteRecord(payments);
            }

            // where should i put the delimiter part?

            return; // to what?
        }

【问题讨论】:

    标签: c# model-view-controller entity-framework-core text-files asp.net-core-2.2


    【解决方案1】:

    你应该使用这个包https://joshclose.github.io/CsvHelper/

    您可以轻松编写带有制表符分隔符的文本文件。

    using CsvHelper;
    using System;
    using System.Collections.Generic;
    using System.IO;
    
    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {
                var records = new List<Foo>
                {
                    new Foo { Id = 1, Name = "one" },
                };
    
                var configuration = new CsvHelper.Configuration.Configuration()
                {
                    Delimiter = "\t"
                };
    
                using (var writer = new StreamWriter("path\\to\\file.csv"))
                using (var csv = new CsvWriter(writer, configuration))
                {
                    csv.WriteRecords(records);
                }
            }
    
            public class Foo
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多