Click here to Skip to main content
14,485,248 members
Rate this:
Please Sign up or sign in to vote.
Hello! I am exporting excel from gridview and after excel download, I press ctrl+p, then all columns are not fitting in print tab. I want to fit them in one page. Here is my code-

HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));

HttpContext.Current.Response.ContentType = "application/";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=windows-1250\">\n");
HttpContext.Current.Response.Write("<html xmlns:o='urn:schemas-microsoft-com:office:office'\n" + "xmlns:x='urn:schemas-microsoft-com:office:excel'\n" +
"xmlns=''>\n" +

Any help will be appriciated, Thanks!

What I have tried:

I searched on google but my problem did not solve.
Updated 30-Dec-17 4:49am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

OK, the code you posted has nothing to do with how the client prints the Excel sheet. That code is just setting up the content headers, not the content, your Excel workbook, itself.

But, typically, there's really nothing you can do on the server to generate a sheet that's guaranteed to print on one sheet on the client side. You have no way of knowing how big the paper is on the client so no way of determining how many columns are going to fit.

About all you can do is the same thing you can do in print settings of Excel, "Fit to sheets". In Excel 2016, you get the options of "Fit Sheet on One Page", or "Fit all Columns on One Page", "Fit all Rows on One Page", and so on.

In the OpenXML SDK used to generate an Excel workbook, you use the PageSetup Class[^] to set this up.

You ARE using the OpenXML SDK, or similar, aren't you?

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100