Write Text file with tab-delimited in Asp.Net Core 2.2

asp.net-core-2.2 asp.net-core-mvc-2.0 c# entity-framework-core text-files

Question

Hi do you have any guides, work aid or step by step how to export to text with tab delimited. Im using Asp.Net Core 2.2 MVC EF. I want to export a list from my table.. I want to have a button where the user click in this DownloadFile Action will trigger.

public IActionResult 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; 
        }
1
1
10/11/2019 5:04:23 AM

Popular Answer

You will need to setup the CsvWriter with a Configuration.

Thus, your code needs only a slight change:

[...]
var configuration = new CsvHelper.Configuration.Configuration();
configuration.Delimiter = '\t';

using (var csv = new CsvWriter(writer, configuration))
{
    csv.WriteHeader<BdoPE>();
    csv.WriteRecord(payments);
}
[...]
0
10/11/2019 6:18:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow