Click here to Skip to main content
15,901,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Question for you.

Iam confused with Export to excel in gridview. my all grid view design
structures are in client side. so using javascript I got a downloaded File
format as the result of export to excel without .xls extension. When i
trying to write code in controller, i got a .xls file with empty data.
since i don't know how to get clint side webgrid value to the server side
controller, so thet i just reuse the method again for getting grid data in
server side where i tryed to return
But did't get a best result. I kindly expect your help.

Thanks and Regards

Hi i know two waies to export to xls
first : loop to DataTable and create manauly then write it to response.

<pre lang="cs">Response.ContentType = "application/";
          Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");

second : use exporters link Devexpress ASPXGridExporter.

<pre lang="c#">
protected void imbPersonsXLS_Click(object sender, ImageClickEventArgs e)

the second way is very easy and devexpress aspxgridview filter also suported.
Share this answer
Member 10464204 28-Jan-14 8:06am    
thanks for your reply. but i didn't get the result, I tryed that u wrote first. i still waiting with uncomplete
... I can wrot my code here..

public ActionResult GetRMAList(string gridData, string RMADateFrom, string RMADateTo, string PM20SN, string PhSNo, string sort = _rmaDefaultSortCol, string sortdir = defaultSortDir, int? page = 1)
var stream = new MemoryStream();
//var sb = new StringBuilder();
IEnumerable<DtoRMAMonitor> pagedrmamonitors = Enumerable.Empty<DtoRMAMonitor>();

PagedResult<DtoRMAMonitor> rmaMonitors = new PagedResult<DtoRMAMonitor>();

rmaMonitors = _RMAManager.GetRMAMonitors(null, null, serialNumber, phoneSNo, null, RMAdateFrom, RMAdateTo, null, sort, sortdir, pageSize, page.Value);

currentPageIndex = page.HasValue ? page.Value - 1 : 0;
totalrows = rmaMonitors.RowCount;
pagedrmamonitors = rmaMonitors.Results.ToPagedList(currentPageIndex, pageSize, totalrows);

this.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
this.Response.AddHeader("content-disposition", "attachment; filename=report.xls");
Object obj = new Object();
obj = pagedrmamonitors;

XmlDocument xmlDoc = new XmlDocument();
XmlSerializer oXmlSerializer = new XmlSerializer(typeof(Object));

oXmlSerializer.Serialize(stream, obj);

stream.Position = 0;

catch (Exception ex)
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Orders.xls");}

@if (Model != null)

var grid = new WebGrid<DtoRMAMonitor>(null, rowsPerPage: Model.PageSize, defaultSort: "RtdComplete", canPage: true, canSort: true, ajaxUpdateContainerId: "monitorlist");
// alternative server binding option code snippet is as shown below.
//var grid = Html.ServerPagedGrid(Model, Model.TotalItemCount, rowsPerPage: Model.PageSize, defaultSort: "SiteId", canPage: true, canSort: true, ajaxUpdateContainerId: "container-grid");
grid.Bind(source: Model, rowCount:
<img src="/images/excel-icon.jpg" alt="Export to Excel" title="Export to Excel" />

<script src=""></script>
<script type="text/javascript">
$(document).ready(function () {
$('#excel').on('hover', function () {
$(this).css('cursor', 'pointer');
$("#excel").click(function (e) {
var prtContent = document.getElementById(list); ( "", "import.xls","letf=0,top=0,width=800%,height=600,toolbar=1,scrollbars=1,status=1");'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,' + ($('div[id$=list]').html()));

Hi. the link that Abhinav S was very easey and clear here the thing which you need is very simple and easy...

<pre lang="cs">protected void Button1_Click(object sender, EventArgs e)
        string.Format("attachment;filename={0}.xls", "Exported"));
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/ms-excel";

    using (StringWriter sw = new StringWriter())
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            //  Create a table to contain the grid
            Table table = new Table();

            foreach (GridViewRow row in GridView1.Rows)

            //  render the table into the htmlwriter

            //  render the htmlwriter into the response

Use this Simple Functionality and Avoid doing Complex Coding the beauty is in Simplicity.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900