Export data set into Excel using Closed XML

Below is the simple code example to export the DataSet into an Excel using ClosedXML dll.

private static void ExportDataSet(DataSet ds, string destination)
{
          var workbook = new ClosedXML.Excel.XLWorkbook();
          foreach (DataTable dt in ds.Tables) {
                  var worksheet = workbook.Worksheets.Add(dt.TableName);
                  worksheet.Cell(1, 1).InsertTable(dt);
                  worksheet.Columns().AdjustToContents(); 
          }
          workbook.SaveAs(destination);
          workbook.Dispose();
}
Advertisements

#closedxml-to-export-data-set