Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
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='http://www.w3.org/TR/REC-html40'>\n" +
"<head>\n");


Any help will be appriciated, Thanks!

What I have tried:

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

1 solution

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?
 
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