title: Export to excel in asp.net using Microsoft.Office.Interop.Excel
body: <p>I am using following example on <a href="http://msdn.microsoft.com/en-us/library/dd264733%28v=vs.100%29.aspx" rel="nofollow noreferrer">Microsoft.com</a> website  to exports data from sql server to Excel sheet i slightly modified the example to use <code>datatable</code> for my version while debugging it loops through all the rows but it only show one last row in actual excel file</p>
  
  <p>Below is the code for vs 2010, I am sure i did miss something</p>
  
  <pre><code>foreach (DataRow row in dt.Rows)
  {
     // rowCounter++;
      workSheet.Cells[1, "A"] = row["Date"];
      workSheet.Cells[1, "B"] = row["Title"];
      workSheet.Cells[1, "C"] = row["Description"];
  }
      ((Excel.Range)workSheet.Columns[1]).AutoFit();
      ((Excel.Range)workSheet.Columns[2]).AutoFit();
      ((Excel.Range)workSheet.Columns[3]).AutoFit();
  </code></pre>
  
  <p>How can i also modify this code so that it write output to <code>Download_Excel.xls</code> file</p>
  
  <p>Above code only show last one row in excel.
  <strong>OUTPUT</strong> 
  <img src="https://i.stack.imgur.com/3uDXJ.jpg" alt="enter image description here"></p>
  
  <p>It doesn't custom  Title as either.</p>
  
  <p>TWO MISTAKE I WAS DOING.</p>
  
  <p>First <code>ROWINDEX</code>  was starting from 1  so i set <code>ROWINDEX= 2</code> and things worked fine</p>
  
  <p><strong>WORKING CODE</strong></p>
  
  <pre><code>foreach (DataRow row in dt.Rows)
  {
      rowCounter++;
      workSheet.Cells[rowCounter, "A"] = row["Date"];
      workSheet.Cells[rowCounter, "B"] = row["Title"];
      workSheet.Cells[rowCounter, "C"] = row["Description"];
  }
  
      string fileName = "Product.xls";
      excelApp.DisplayAlerts = false;
      workSheet.SaveAs(HttpContext.Current.Server.MapPath("../"+fileName));
  </code></pre>