如何冻结从ASP.NET导出的Excel电子表格中的标题行
我正在使用以下函数将ASP.NET gridview导出到Excel。 格式工作得很好,除了我需要冻结导出的Excel中的标题行。 我真的想避免使用第三方的Excel插件,但除非我的AddExcelStyling函数中有一些古老的Excel标记。
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("<meta http-equiv=Content-Type content=""text/html; charset=utf-8"">" + Environment.NewLine) Response.Write(AddExcelStyling()) Response.Write(sw.ToString()) Response.Write("</body>") Response.Write("</html>") Response.End() End Sub
格式化黑魔法:
Private Function AddExcelStyling() As String Dim sb As StringBuilder = New StringBuilder() sb.Append("<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'>" + Environment.NewLine + _ "<head>") sb.Append("<style>" + 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("</style>" + Environment.NewLine) sb.Append("<!--[if gte mso 9]><xml>" + Environment.NewLine) sb.Append("<x:ExcelWorkbook>" + Environment.NewLine) sb.Append("<x:ExcelWorksheets>" + Environment.NewLine) sb.Append("<x:ExcelWorksheet>" + Environment.NewLine) sb.Append("<x:Name>PriceSheets</x:Name>" + Environment.NewLine) sb.Append("<x:WorksheetOptions>" + Environment.NewLine) sb.Append("<x:Print>" + Environment.NewLine) sb.Append("<x:ValidPrinterInfo/>" + Environment.NewLine) sb.Append("<x:PaperSizeIndex>9</x:PaperSizeIndex>" + Environment.NewLine) sb.Append("<x:HorizontalResolution>600</x:HorizontalResolution" + Environment.NewLine) sb.Append("<x:VerticalResolution>600</x:VerticalResolution" + Environment.NewLine) sb.Append("</x:Print>" + Environment.NewLine) sb.Append("<x:Selected/>" + Environment.NewLine) sb.Append("<x:DoNotDisplayGridlines/>" + Environment.NewLine) sb.Append("<x:ProtectContents>False</x:ProtectContents>" + Environment.NewLine) sb.Append("<x:ProtectObjects>False</x:ProtectObjects>" + Environment.NewLine) sb.Append("<x:ProtectScenarios>False</x:ProtectScenarios>" + Environment.NewLine) sb.Append("</x:WorksheetOptions>" + Environment.NewLine) sb.Append("</x:ExcelWorksheet>" + Environment.NewLine) sb.Append("</x:ExcelWorksheets>" + Environment.NewLine) sb.Append("<x:WindowHeight>12780</x:WindowHeight>" + Environment.NewLine) sb.Append("<x:WindowWidth>19035</x:WindowWidth>" + Environment.NewLine) sb.Append("<x:WindowTopX>0</x:WindowTopX>" + Environment.NewLine) sb.Append("<x:WindowTopY>15</x:WindowTopY>" + Environment.NewLine) sb.Append("<x:ProtectStructure>False</x:ProtectStructure>" + Environment.NewLine) sb.Append("<x:ProtectWindows>False</x:ProtectWindows>" + Environment.NewLine) sb.Append("</x:ExcelWorkbook>" + Environment.NewLine) sb.Append("</xml><![endif]-->" + Environment.NewLine) sb.Append("</head>" + Environment.NewLine) sb.Append("<body>" + Environment.NewLine) Return sb.ToString() End Function
修改WorksheetOption元素,如下所示:
<x:WorksheetOptions> <x:Selected/> <x:FreezePanes/> <x:FrozenNoSplit/> <x:SplitHorizontal>1</x:SplitHorizontal> <x:TopRowBottomPane>1</x:TopRowBottomPane> <x:ActivePane>2</x:ActivePane> <x:Panes> <x:Pane> <x:Number>3</x:Number> </x:Pane> <x:Pane> <x:Number>2</x:Number> </x:Pane> </x:Panes> <x:ProtectContents>False</x:ProtectContents> <x:ProtectObjects>False</x:ProtectObjects> <x:ProtectScenarios>False</x:ProtectScenarios> </x:WorksheetOptions>
注意FreezePanes元素。 我把这个电子表格从第一行冻结的HTML格式保存下来。 当这个文件用Excel打开时,第一行被冻结。
编辑:要在每个页面上打印标题行,您将需要这样的东西:
<x:ExcelName> <x:Name>Print_Area</x:Name> <x:SheetIndex>1</x:SheetIndex> <x:Formula>=Sheet1!$A$2:$F$97</x:Formula> </x:ExcelName> <x:ExcelName> <x:Name>Print_Titles</x:Name> <x:SheetIndex>1</x:SheetIndex> <x:Formula>=Sheet1!$1:$1</x:Formula> </x:ExcelName>
您将需要dynamic修改数据中的值。
也许你可以使用我在尝试做“专业”格式化时所使用的技巧。
用你想expression的所有行和单元格格式创build一个XLS。 然后复制它,添加你的数据,并保存它。
以这种方式,你不需要任何付费插件,你总是得到任何格式你想要的…
需要多个专业? 用所需的格式创build另一个my_MT_Excel.xls。
你可以有一个文件夹充满了这些东西都准备好去,而不诉诸更复杂的编程技术。
TOB