DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Implement Hibernate Second-Level Cache With NCache
  • JSON-Based Serialized LOB Pattern
  • Advanced Maintenance of a Multi-Database Citus Cluster With Flyway
  • Develop XR With Oracle Cloud, Database on HoloLens, Ep 2: Property Graphs, Data Visualization, and Metaverse

Trending

  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • Using LLMs to Automate Data Cleaning and Transformation Pipelines
  • Exactly-Once Processing: Myth vs Reality
  1. DZone
  2. Coding
  3. Languages
  4. Generating CSV-files on .NET

Generating CSV-files on .NET

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Jun. 26, 15 · Interview
Likes (1)
Comment
Save
Tweet
Share
4.7K Views

Join the DZone community and get the full member experience.

Join For Free

I have project where I need to output some reports as CSV-files. I found a good library called CsvHelper from NuGet and it works perfect for me. After some playing with it I was able to generate CSV-files that were shown correctly in Excel. Here is some sample code and also extensions that make it easier to work with DataTables.

Simple report

Here’s the simple fragment of code that illustrates how to use CsvHelper.

using (var writer = new StreamWriter(Response.OutputStream))

using (var csvWriter = new CsvWriter(writer))

{

    csvWriter.Configuration.Delimiter = ";";



    csvWriter.WriteField("Task No");

    csvWriter.WriteField("Customer");

    csvWriter.WriteField("Title");

    csvWriter.WriteField("Manager");

    csvWriter.NextRecord();



    foreach (var project in data)

    {

        csvWriter.WriteField(project.Code);

        csvWriter.WriteField(project.CustomerName);

        csvWriter.WriteField(project.Name);

        csvWriter.WriteField(project.ProjectManagerName);

        csvWriter.NextRecord();

    }

}

Of course, you can use other methods to output whole object or object list with one shot. I just needed here custom headers that doesn’t match property names 1:1.

Generic helper for DataTable

Some of my projects come from service layer as DataTable. I don’t want to add new models or Data Transfer Objects (DTO) with no good reason and DataTable is actually flexible enough if you need to add new fields to report and you want to do it fast.

As DataTables are not supported by default (yet?), I wrote simple extension methods that work on DataTable views. When called on DataTable it selects default view automatically. The idea is – you can set filter on default data view and leave out the rows you don’t need. If you just want to show DataTable to screen as table then check out my posting Simple view to display contents of DataTable.

public static class CsvHelperExtensions

{

    public static void WriteDataTable(this CsvWriter csvWriter, DataTable table)

    {

        WriteDataView(csvWriter, table.DefaultView);

    }



    public static void WriteDataView(this CsvWriter csvWriter, DataView view)

    {

        foreach (DataColumn col in view.Table.Columns)

        {

            csvWriter.WriteField(col.ColumnName);

        }

        csvWriter.NextRecord();



        foreach (DataRowView row in view)

        {

            foreach (DataColumn col in view.Table.Columns)

            {

                csvWriter.WriteField(row[col.ColumnName]);



            }

            csvWriter.NextRecord();

        }

    }

}

And here is simple MVC controller action that gets data as DataTable and returns it as CSV-file. The result is CSV-file that opens correctly in Excel.

[HttpPost]

public void ExportIncomesReport()

{

    var data = // Get DataTable here



    Response.ContentType = "text/csv";

    Response.AddHeader("Content-disposition", "attachment;filename=IncomesReport.csv");



    var preamble = Encoding.UTF8.GetPreamble();

    Response.OutputStream.Write(preamble, 0, preamble.Length);



    using (var writer = new StreamWriter(Response.OutputStream))

    using (var csvWriter = new CsvWriter(writer))

    {

        csvWriter.Configuration.Delimiter = ";";

        csvWriter.WriteDataTable(data);

    }

}

One thing to notice – with CsvHelper we have full control over a stream where we write data and this way we can write more performant code.

Related Posts

  • .Net Framework 4.0: string.IsNullOrWhiteSpace() method
  • Exporting GridView Data to Excel
  • Code Contracts: Hiding ContractException
  • How to dump object properties
  • My object to object mapper source released

The post Generating CSV-files on .NET appeared first on Gunnar Peipman - Programming Blog.

Object (computer science) Data (computing) Database Service layer POST (HTTP) Property (programming) .NET

Published at DZone with permission of Gunnar Peipman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Implement Hibernate Second-Level Cache With NCache
  • JSON-Based Serialized LOB Pattern
  • Advanced Maintenance of a Multi-Database Citus Cluster With Flyway
  • Develop XR With Oracle Cloud, Database on HoloLens, Ep 2: Property Graphs, Data Visualization, and Metaverse

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook