title: Export Sql data to Excel very slow
body: <p>I am exporting Sql data to Excel. The code I am using currently is :</p>
  
  <pre><code>    DataTable dt = new DataTable();
              // Create sql connection string
              string conString = "Data Source=DELL\\SQLSERVER1;Trusted_Connection=True;DATABASE=Zelen;CONNECTION RESET=FALSE";
  
              SqlConnection sqlCon = new SqlConnection(conString);
              sqlCon.Open();
  
  
              SqlDataAdapter da = new SqlDataAdapter("select LocalSKU,ItemName, QOH,Price,Discontinued,CAST(Barcode As varchar(25)) As Barcode,Integer2,Integer3,ISNULL(SalePrice,0.0000)AS SalePrice,SaleOn,ISNULL(Price2,0.0000)AS Price2 from dbo.Inventory", sqlCon);
  
              System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
  
              da.Fill(dtMainSQLData);
              DataColumnCollection dcCollection = dtMainSQLData.Columns;
  
              // Export Data into EXCEL Sheet
              Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
              ExcelApp.Application.Workbooks.Add(Type.Missing);
  
  
  
              int i = 1;
              int j = 1;
              int s = 1;
              //header row
              foreach (DataColumn col in dtMainSQLData.Columns)
              {
                  ExcelApp.Cells[i, j] = col.ColumnName;
                  j++;
  
                  ExcelApp.Rows.AutoFit();
                  ExcelApp.Columns.AutoFit();
              }
  
              i++;
  
              //data rows
              foreach (DataRow row in dtMainSQLData.Rows)
              {
                  for (int k = 1; k &lt; dtMainSQLData.Columns.Count + 1; k++)
                  {
                      ExcelApp.Cells[i, k] = "'" + row[k - 1].ToString();
                  }
  
                  i++;
                  s++;
                  Console.Write(s);
                  Console.Write("\n\r");
  
                  ExcelApp.Columns.AutoFit();
                  ExcelApp.Rows.AutoFit();
              }
  
              var b = Environment.CurrentDirectory + @"\Sheet1.xlsx";
              ExcelApp.ActiveWorkbook.SaveCopyAs(b);
  
              ExcelApp.ActiveWorkbook.Saved = true;
              ExcelApp.Quit();
  
              Console.WriteLine(".xlsx file Exported succssessfully.");
  </code></pre>
  
  <p>Takes are 70000 rows in my sql database. I am running this script in Console application.
  It takes more then an hour to export it to excel file.</p>
  
  <p>How can I use this to export it faster?</p>
  
  <p>Examples would be appreciated.</p>