开发者

How to export to excel from a table on the ASP.NET MVC View Page?

I have a "export to Excel" button on the ASP.NET MVC Page.

When the user clicks on this button, the data from the table need to export to Excel includ开发者_如何学Cing the Header.

Could you please the code sample.

Appreciate your responses.

Thanks.


When the user clicks on the button have the action return an ExcelML document. You would have full control over the formatting and it's automatically opened in Excel. Xml in Excel

Here are some constants that I use in a SharePoint site I'm working on to build some XML to be opened by Excel

private const string excelTable = 
    "<?xml version=\"1.0\"?>\n"+
        "<?mso-application progid=\"Excel.Sheet\"?>\n" + 
        "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\""+
                   "xmlns:o=\"urn:schemas-microsoft-com:office:office\"" + 
                   "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"" +
                   "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" "  + 
                   "xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n"+
             "<Worksheet ss:Name=\"Sheet 1\">\n"+
                 "<Table>\n"+
                     "{0}" +
                 "</Table>\n"+
                 "<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"+ 
                 "</WorksheetOptions>\n"+
             "</Worksheet>\n"+
         "</Workbook>";
private const string excelRowFormat = "<Row>{0}</Row>\n";
private const string excelCellFormat = 
                 "{0}<Cell><Data ss:Type=\"String\">{1}</Data></Cell>";

Not the prettiest or most efficient way but it works. The XML that I build I attach to the HttpResponse just like you would when a user would download a file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜