Save IList*T* or IQueryable*T* to Excel – C#

I’m generating a report via Linq to SQL and displaying it on the website. I need to save the report in MS Excel (or .csv) and email it to user. Any ideas on how to save it as Excel or .csv ?.NET 3.5 / C#EDIT: the Gridview hack works perfect. If you use AJAX on the page, make sure you add …

via Word Tutorials » Search Results » ajax:

Save IList*T* or IQueryable*T* to Excel – C#

I’m generating a report via Linq to SQL and displaying it on the website.
I need to save the report in MS Excel (or .csv) and email it to user.
Any ideas on how to save it as Excel or .csv ?
.NET 3.5 / C#
EDIT:
the Gridview hack 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.
I’ve also found a Library to Generate Excel XML Workbooks in .NET, however it’s not as simple as accepted solution.
c# excel .net-3.5

………………………………..

This gridview hack is the simplest solution I’ve found.
………………………………..

Go to this article on LINQtoCSV on Code Project. Then open the source code in VS and comment out line 201 in FieldMapper.cs to fix a bug that mangles the column ordering:
//Array.Sort(m_IndexToInfo); Recompile and you’re good to go.
(I found that from the comments of the article)
Then to use:
CsvFileDescription outputFileDescription = new CsvFileDescription SeparatorChar = ‘t’, // tab delimited FirstLineHasColumnNames = false // no column names in first record ; CsvContext cc = new CsvContext(); cc.Write( YOUR_IQUERYABLE_RESULT_HERE, “filename.csv”, outputFileDescription); I made a simplified IQueryable.WriteToFile(path) extension method but I don’t have it on this machine.

For more info: Save IList*T* or IQueryable*T* to Excel – C#

Word Tutorials » Search Results » ajax

Save IList*T* or IQueryable*T* to Excel – C#

Share this post:

Related Posts

Leave a Comment