Save IList <T> or IQueryable <T> in Excel - C #

I create a report through Linq to SQL and display it on the website.

I need to save the report in MS Excel (or .csv) and send it to the user.

Any ideas on how to save it as Excel or .csv?

.NET 3.5 / C #

EDIT:

Gridview hack works great. If you use AJAX on the page, make sure you add PostBaseTrigger to your update panel to call FULL MAIL, this will not work.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Export Grid" 
            onclick="Button1_Click" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="Button1" />
    </Triggers>
</asp:UpdatePanel>

I also found a library for generating Excel XML Workbooks in .NET , however this is not as simple as the decision made.

+3
source share
2 answers
+2

LINQtoCSV . VS 201 FieldMapper.cs, , :

//Array.Sort(m_IndexToInfo);

, .

( , )

:

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);

IQueryable <T > . WriteToFile (path), .

+5

Source: https://habr.com/ru/post/1704693/


All Articles