如何冻结在Excel US preadsheet标题行从ASP.NET出口 [英] How to freeze the header row in an Excel spreadsheet exported from ASP.NET

查看:236
本文介绍了如何冻结在Excel US preadsheet标题行从ASP.NET出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的函数导出一个ASP.NET的GridView到Excel。格式化工作真的很好,除了我需要冻结在Excel中的标题行对出口。我真的试图避免使用第三方插件的Excel的这一点,但除非有我的AddExcelStyling功能有些过时Excel的标记。

 公用Sub exportGrid(BYVAL psFileName作为字符串)        Response.Clear()
        将Response.Buffer = TRUE
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType =应用程序/ vnd.ms-EXCEL
        Response.AddHeader(内容处置,附件;文件名= PriceSheet.xls)
        Response.Charset的=
        Me.EnableViewState =假
        昏暗SW作为新的StringWriter()
        HTW昏暗作为新的HtmlTextWriter(SW)
        sfggcPriceSheet.RenderControl(HTW)
        的Response.Write(< META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8>中+ Environment.NewLine)
        的Response.Write(AddExcelStyling())
        的Response.Write(sw.ToString())
        的Response.Write(< /身体GT;)
        的Response.Write(< / HTML>中)
        到Response.End()    结束小组

和格式化的黑魔法:

 专用功能AddExcelStyling()作为字符串
    昏暗某人作为StringBuilder的=新的StringBuilder()
    sb.Append(< HTML的xmlns:O =瓮:架构 - 微软-COM:办公室:办公室'+ Environment.NewLine + _
    的xmlns:X =瓮:架构 - 微软-COM:办公室:Excel的+ Environment.NewLine + _
    的xmlns =HTTP://www.w3.org/TR/REC-html40'> + Environment.NewLine + _
    < HEAD>中)
    sb.Append(<风格>中+ Environment.NewLine)    sb.Append(@页)
    sb.Append({保证金:.25in .25in .25in .25in;+ Environment.NewLine)    sb.Append(MSO的头利润率:.025in;+ Environment.NewLine)
    sb.Append(MSO英尺利润率:.025in;+ Environment.NewLine)    sb.Append(MSO的页面方向:景观;}+ Environment.NewLine)
    sb.Append(< /风格与GT;+ Environment.NewLine)    sb.Append(&下;! - [如果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:名称>&PriceSheets LT; / X:名称>+ Environment.NewLine)
    sb.Append(&所述; X:WorksheetOptions>中+ Environment.NewLine)    sb.Append(< X:印刷>中+ Environment.NewLine)
    sb.Append(&所述; X:ValidPrinterInfo />中+ Environment.NewLine)    sb.Append(&所述; X:PaperSizeIndex> 9&下; / X:PaperSizeIndex>中+ Environment.NewLine)
    sb.Append(&所述; X:Horizo​​ntalResolution> 600℃/ X:Horizo​​ntalResolution+ Environment.NewLine)    sb.Append(&所述; X:VerticalResolution> 600℃/ X:VerticalResolution+ Environment.NewLine)
    sb.Append(< / X:打印与GT;+ Environment.NewLine)    sb.Append(< X:选择/>中+ Environment.NewLine)
    sb.Append(&所述; X:DoNotDisplayGridlines />中+ Environment.NewLine)    sb.Append(< X:ProtectContents>假LT; / X:ProtectContents>中+ Environment.NewLine)
    sb.Append(< X:ProtectObjects>假LT; / X:ProtectObjects>中+ Environment.NewLine)    sb.Append(< X:ProtectScenarios>假LT; / 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>假LT; / X:ProtectStructure>中+ Environment.NewLine)
    sb.Append(< X:ProtectWindows>假LT; / X:ProtectWindows>中+ Environment.NewLine)    sb.Append(&下; / X:ExcelWorkbook>中+ Environment.NewLine)
    sb.Append(&下; / xml的>&下;![ENDIF] - gt;中+ Environment.NewLine)    sb.Append(< /头>中+ Environment.NewLine)
    sb.Append(<身体GT;+ Environment.NewLine)    返回sb.ToString()结束功能


解决方案

修改WorksheetOption元素类似以下内容:

 < X:WorksheetOptions>
     < X:选择/>
     &所述; X:FreezePanes />
     &所述; X:FrozenNoSplit />
     &所述; X:SplitHorizo​​ntal→1&下; / X:SplitHorizo​​ntal>
     &所述; X:TopRowBottomPane→1&下; / X:TopRowBottomPane>
     &所述; X:ActivePane→2&下; / X:ActivePane>
     < X:窗格>
      < X:窗格>
       < X:数>第3版; / X:号码>
      < / X:窗格>
      < X:窗格>
       < X:数> 2'; / X:号码>
      < / X:窗格>
     < / X:窗格>
     < X:ProtectContents>假LT; / X:ProtectContents>
     < X:ProtectObjects>假LT; / X:ProtectObjects>
     < X:ProtectScenarios>假LT; / X:ProtectScenarios>
    < / X:WorksheetOptions>

注意FreezePanes元素。我砍了这一点,我曾与冻结的第一行保存为HTML A S preadsheet的。当这个文件被打开使用Excel,第一行被冻结。

编辑:
为了让每个页面上的标题行打印,你需要这样的:

 < X:ExcelName>
  < X:名称>&Print_Area LT; / X:名称>
  &所述; X:SheetIndex→1&下; / X:SheetIndex>
  < X:公式> =工作表Sheet1!$ A $ 2:!$ F $ 97 LT; / X:公式>
 < / X:ExcelName>
 &所述; X:ExcelName>
  < X:名称>&Print_Titles LT; / X:名称>
  &所述; X:SheetIndex→1&下; / X:SheetIndex>
  < X:公式> =工作表Sheet1 $ 1:$ 1 LT; / X:公式>
 < / X:ExcelName>

您将需要动态地修改公式中的值数据。

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.

    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

And the formatting black magic:

   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

解决方案

Modify the WorksheetOption element to something like the following:

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

Notice the FreezePanes element. I cut this out of a spreadsheet that I had saved as HTML with the first row frozen. When this file is opened with Excel, the first row is frozen.

EDIT: To have the header row print on each page, you will need something like this:

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

You will need to modify the values in the formula dynamically for your data.

这篇关于如何冻结在Excel US preadsheet标题行从ASP.NET出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆