title: Most efficient way to export DataTable to Excel File (.xlsx)
body: <p>I wanted to know what is the most efficient way to export a DataTable or DataSet to an .xlsx file in terms of speed.</p>
  
  <p>I have tables of 200K rows and looping is useless, so I want to make like a bulk export or something like that.</p>
  
  <p>Anything easy to implement answer my question?</p>
  
  <p><strong>SOLUTION:</strong>
  I finally used OpenXml by this way, if someone needs it. It exports 100k in about 1 minute:</p>
  
  <pre><code>    private void ExportDataSet(DataSet ds, string destination)
      {
          using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
          {
              var workbookPart = workbook.AddWorkbookPart();
  
              workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
  
              workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
  
              foreach (System.Data.DataTable table in ds.Tables)
              {
  
                  var sheetPart = workbook.WorkbookPart.AddNewPart&lt;WorksheetPart&gt;();
                  var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
                  sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
  
                  DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild&lt;DocumentFormat.OpenXml.Spreadsheet.Sheets&gt;();
                  string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
  
                  uint sheetId = 1;
                  if (sheets.Elements&lt;DocumentFormat.OpenXml.Spreadsheet.Sheet&gt;().Count() &gt; 0)
                  {
                      sheetId =
                          sheets.Elements&lt;DocumentFormat.OpenXml.Spreadsheet.Sheet&gt;().Select(s =&gt; s.SheetId.Value).Max() + 1;
                  }
  
                  DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
                  sheets.Append(sheet);
  
                  DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
  
                  List&lt;String&gt; columns = new List&lt;string&gt;();
                  foreach (System.Data.DataColumn column in table.Columns)
                  {
                      columns.Add(column.ColumnName);
  
                      DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                      cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                      cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
                      headerRow.AppendChild(cell);
                  }
  
  
                  sheetData.AppendChild(headerRow);
  
                  foreach (System.Data.DataRow dsrow in table.Rows)
                  {
                      DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                      foreach (String col in columns)
                      {
                          DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                          cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                          cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
                          newRow.AppendChild(cell);
                      }
  
                      sheetData.AppendChild(newRow);
                  }
  
              }
          }
      }
  </code></pre>