Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to freeze the header row in an Excel spreadsheet exported from ASP.NET
    primarykey
    data
    text
    <p>I'm exporting an ASP.NET gridview to Excel using the following function. The formatting is working really well, except I need to freeze the header row in Excel on the export. I'm really trying to avoid using a 3rd party Excel plugin for this, but unless there's some archaic excel markup in my AddExcelStyling function. </p> <pre><code> Public Sub exportGrid(ByVal psFileName As String) Response.Clear() Response.Buffer = True Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.ms-excel" Response.AddHeader("content-disposition", "attachment;filename=PriceSheet.xls") Response.Charset = "" Me.EnableViewState = False Dim sw As New StringWriter() Dim htw As New HtmlTextWriter(sw) sfggcPriceSheet.RenderControl(htw) Response.Write("&lt;meta http-equiv=Content-Type content=""text/html; charset=utf-8""&gt;" + Environment.NewLine) Response.Write(AddExcelStyling()) Response.Write(sw.ToString()) Response.Write("&lt;/body&gt;") Response.Write("&lt;/html&gt;") Response.End() End Sub </code></pre> <p>And the formatting black magic:</p> <pre><code> Private Function AddExcelStyling() As String Dim sb As StringBuilder = New StringBuilder() sb.Append("&lt;html xmlns:o='urn:schemas-microsoft-com:office:office'" + Environment.NewLine + _ "xmlns:x='urn:schemas-microsoft-com:office:excel'" + Environment.NewLine + _ "xmlns='http://www.w3.org/TR/REC-html40'&gt;" + Environment.NewLine + _ "&lt;head&gt;") sb.Append("&lt;style&gt;" + Environment.NewLine) sb.Append("@page") sb.Append("{margin:.25in .25in .25in .25in;" + Environment.NewLine) sb.Append("mso-header-margin:.025in;" + Environment.NewLine) sb.Append("mso-footer-margin:.025in;" + Environment.NewLine) sb.Append("mso-page-orientation:landscape;}" + Environment.NewLine) sb.Append("&lt;/style&gt;" + Environment.NewLine) sb.Append("&lt;!--[if gte mso 9]&gt;&lt;xml&gt;" + Environment.NewLine) sb.Append("&lt;x:ExcelWorkbook&gt;" + Environment.NewLine) sb.Append("&lt;x:ExcelWorksheets&gt;" + Environment.NewLine) sb.Append("&lt;x:ExcelWorksheet&gt;" + Environment.NewLine) sb.Append("&lt;x:Name&gt;PriceSheets&lt;/x:Name&gt;" + Environment.NewLine) sb.Append("&lt;x:WorksheetOptions&gt;" + Environment.NewLine) sb.Append("&lt;x:Print&gt;" + Environment.NewLine) sb.Append("&lt;x:ValidPrinterInfo/&gt;" + Environment.NewLine) sb.Append("&lt;x:PaperSizeIndex&gt;9&lt;/x:PaperSizeIndex&gt;" + Environment.NewLine) sb.Append("&lt;x:HorizontalResolution&gt;600&lt;/x:HorizontalResolution" + Environment.NewLine) sb.Append("&lt;x:VerticalResolution&gt;600&lt;/x:VerticalResolution" + Environment.NewLine) sb.Append("&lt;/x:Print&gt;" + Environment.NewLine) sb.Append("&lt;x:Selected/&gt;" + Environment.NewLine) sb.Append("&lt;x:DoNotDisplayGridlines/&gt;" + Environment.NewLine) sb.Append("&lt;x:ProtectContents&gt;False&lt;/x:ProtectContents&gt;" + Environment.NewLine) sb.Append("&lt;x:ProtectObjects&gt;False&lt;/x:ProtectObjects&gt;" + Environment.NewLine) sb.Append("&lt;x:ProtectScenarios&gt;False&lt;/x:ProtectScenarios&gt;" + Environment.NewLine) sb.Append("&lt;/x:WorksheetOptions&gt;" + Environment.NewLine) sb.Append("&lt;/x:ExcelWorksheet&gt;" + Environment.NewLine) sb.Append("&lt;/x:ExcelWorksheets&gt;" + Environment.NewLine) sb.Append("&lt;x:WindowHeight&gt;12780&lt;/x:WindowHeight&gt;" + Environment.NewLine) sb.Append("&lt;x:WindowWidth&gt;19035&lt;/x:WindowWidth&gt;" + Environment.NewLine) sb.Append("&lt;x:WindowTopX&gt;0&lt;/x:WindowTopX&gt;" + Environment.NewLine) sb.Append("&lt;x:WindowTopY&gt;15&lt;/x:WindowTopY&gt;" + Environment.NewLine) sb.Append("&lt;x:ProtectStructure&gt;False&lt;/x:ProtectStructure&gt;" + Environment.NewLine) sb.Append("&lt;x:ProtectWindows&gt;False&lt;/x:ProtectWindows&gt;" + Environment.NewLine) sb.Append("&lt;/x:ExcelWorkbook&gt;" + Environment.NewLine) sb.Append("&lt;/xml&gt;&lt;![endif]--&gt;" + Environment.NewLine) sb.Append("&lt;/head&gt;" + Environment.NewLine) sb.Append("&lt;body&gt;" + Environment.NewLine) Return sb.ToString() End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload