IC Improve Code Recipes

Chapter 11

Excel, CSV and PDF Output

Sooner or later every business application grows an "Export" button. Users want the grid in Excel, finance wants a CSV for the general ledger, and customers want a PDF invoice. The first version is usually written in an afternoon, and it works until the first customer name with a comma in it, the first row of French data, or the first month with 200,000 rows.

The archive behind this book has export code in 26 engagements, with 3,044 Excel- and CSV-related classes, and CSVHelper appears as a class name 194 times. PDF and reporting code shows up in 17 engagements, with 1,255 classes. Most of the export code starts from a DataTable; about 9,585 hand-written files in the archive create a DataSet or DataTable, across 30 engagements. The typical CSV writer joins values with commas and never quotes anything. The typical Excel export writes an HTML table with an .xls extension and lets Excel complain about the format. The typical PDF code depends on a reporting component that nobody has a license key for anymore.

This chapter replaces those with code that holds up: a CSV writer and reader that follow the format's rules, Excel export and import with ClosedXML, PDF generation with QuestPDF, and a practical path for RDLC and SSRS reports in modern .NET.

Recipes

  1. 11.1A Correct CSV WriterCSV looks trivial, so everyone writes their own. Then a field contains a comma, a quote or a line break, and the file shifts columns from that row…
  2. 11.2A Correct CSV ReaderThe usual reader calls line.Split(','). That breaks on the first quoted field containing a comma, and ReadLine breaks on the first quoted field…
  3. 11.3Excel Export with ClosedXMLUsers want a real spreadsheet: numbers that sum, dates that sort, a header row that stays in place. The legacy answers are an HTML table renamed to…
  4. 11.4Importing Excel SafelyUsers upload spreadsheets for price lists, budgets and bulk updates. Import code reads cells by position, assumes every cell holds the expected type,…
  5. 11.5PDF Generation with QuestPDFInvoices, statements, certificates and letters need to be PDFs. Legacy options are a reporting tool with a designer, an HTML-to-PDF converter that…
  6. 11.6RDLC and SSRS in Modern AppsMany line-of-business systems have years of reports built as SSRS server reports or RDLC files rendered by the ReportViewer control in Web Forms or…

Chapter checklist

  • Replace hand-rolled string.Join(",") exports with a writer that quotes, uses the invariant culture and guards against formula injection.
  • Add a round-trip unit test for your CSV writer and reader with commas, quotes, line breaks and non-Latin text.
  • Stop sending HTML tables with an .xls extension; generate real .xlsx files with ClosedXML and typed cell values.
  • Never automate Excel through COM on a server.
  • Import spreadsheets by header name, with size and row limits, per-row errors, and nothing saved until the whole file validates.
  • Check QuestPDF's current license terms against your organization, and declare the license in Program.cs.
  • Keep existing RDLC and SSRS reports running from ASP.NET Core through URL access or a server-side RDLC renderer, and plan their replacement.