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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s