【问题标题】:Enabling Excel autofilter in export excel file using使用在导出 excel 文件中启用 Excel 自动过滤器
【发布时间】:2011-04-07 10:03:05
【问题描述】:

我编写了一个将导出到 Excel 2007 的 C# 方法,这是一个包含 html 表的 html 文件。在 Excel 中打开导出的文件时,数据呈现完美。

我想在上面的导出中添加一个额外的功能,这样当我在 Excel 中打开导出的数据时,自动过滤器会自动打开。

在导出期间我可以在 C# 中做什么,以启用所有列的自动筛选?

【问题讨论】:

    标签: asp.net export-to-excel


    【解决方案1】:

    如果您想让过滤器自动显示在通过以编程方式将 html 导出到 .xls 文件而创建的 Excel 文件中的标题行,请记住向该文件添加以下 2 个功能。

    • 将 Excel 的架构添加到 html 标记,如下面列表中的粗体所示。准确添加这些架构。
    • 将以下属性 x:autofilter='all' 添加到每个标题列的“td”标记中,该属性在下面的列表中也以粗体显示。

    就是这样。然后你就完成了。就这么简单。下面给出了我从记事本中保存为“SampleInHTML.xls”并在 Excel 2007 和 Excel 2010 中测试的示例列表,可以完美地与在 Excel 中打开保存的文件时自动出现的自动筛选配合使用。

    <html xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">
    
    <head></head>
    
    <body >
    
    <table >
    
     <tr >
      <td  width=130 x:autofilter='all'   
      >Age</td>
      <td  width=130 x:autofilter='all'
      >Salary</td>
     </tr>
     <tr height=18 style='height:13.2pt'>
      <td  >34</td>
      <td >2333</td>
     </tr>
     <tr >
      <td>55</td>
      <td>1222</td>
     </tr>
    
    </table>
    
    </body>
    
    </html>
    

    【讨论】:

      【解决方案2】:

      要执行此类操作,您必须直接对电子表格对象进行操作。据我所知,这不能通过导出 HTML 表格来完成。

      我用过 Gembox.Spreadsheet,它可以做你想做的事。但是,它不是免费的。

      【讨论】:

      【解决方案3】:

      嗯,我可以通过以下几点来实现这一点:

      1. 命名空间xmlns:x="urn:schemas-microsoft-com:office:excel".
      2. th - x:autofilter='all' 的自动筛选属性

      下面是示例 html。

      <html xmlns:x="urn:schemas-microsoft-com:office:excel">
      <head>
          <!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Sheet1</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo/></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
          <table>
            <thead>
              <tr>
                <th x:autofilter='all'>Age</th>
                <th x:autofilter='all'>Salary</th>
              </tr>
           </thead>
           <tbody>
             <tr>
              <td>34</td>
              <td>2333</td>
             </tr>
           </tbody>
         </table>
      </head>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2013-10-07
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 2021-12-11
        • 2014-11-11
        • 2022-11-21
        • 2014-03-30
        • 1970-01-01
        相关资源
        最近更新 更多