title: Save IList<T> or IQueryable<T> to Excel - C#
body: <p>I'm generating a report via Linq to SQL and displaying it on the website.</p>
  <p>I need to save the report in MS Excel (or .csv) and email it to user.</p>
  <p>Any ideas on how to save it as Excel or .csv ?</p>
  <p>.NET 3.5 / C#</p>
  EDIT:
  <p>the <a href="http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html" rel="nofollow noreferrer">Gridview hack</a> works perfect. If you use AJAX on the page, make sure you add a PostBaseTrigger to your Update Panel to cause FULL POSTBACK, it won't work otherwise.</p>
  <pre><code>&lt;asp:UpdatePanel ID=&quot;UpdatePanel1&quot; runat=&quot;server&quot;&gt;
      &lt;ContentTemplate&gt;
          &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Export Grid&quot; 
              onclick=&quot;Button1_Click&quot; /&gt;
      &lt;/ContentTemplate&gt;
      &lt;Triggers&gt;
          &lt;asp:PostBackTrigger ControlID=&quot;Button1&quot; /&gt;
      &lt;/Triggers&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  <p>I've also found  a <a href="http://www.carlosag.net/Tools/ExcelXmlWriter/" rel="nofollow noreferrer">Library to Generate Excel XML Workbooks in .NET</a>, however it's not as simple as accepted solution.</p>