On Fri, Jan 2, 2009 at 1:22 PM, okey <[email protected]> wrote:

>
> I am exporting a gridview to Excel using
>
>        var Response = HttpContext.Current.Response;
>        string attachment = String.Format("attachment; filename=
> {0}.xls", FileName.Replace(" ", string.Empty));
>        Response.ClearContent();
>        Response.AddHeader("content-disposition", attachment);
>        Response.ContentType = "application/ms-excel";
>        var sw = new StringWriter();
>        var htw = new HtmlTextWriter(sw);
>        gv.RenderControl(htw);
>        Response.Write(sw.ToString());
>        Response.End();


You are not _really_ exporting to Excel at all.  If you open your exported
file in a plain text
editor, you will see that you have just HTML in there.  NOT an Excel file.
Excel looks for
the <table> and then renders it, somewhat.  You could create an export page
that
has no CSS to possibly eliminate your problem.
Or.

You could actually do a little Excel automation if the IIS server has MS
Office installed.
Or.
You could just loop through your gv and create a real CSV file.  Wrap the
text columns
in "" and just change your ContentType header.  This way you at least have a
file that is
not just an HTML <table>, but a CSV file that can be used/parsed by many
programs.

Do a little Google search an you will find code to show you how to loop
through the gv
and create a CSV file.

If you have the time, I personally recommend learning a little Office
automation and
creating a real Excel file that you will have much more control over the
layout, etc.

Best,

Jim

Reply via email to