使用以下命令在导出 Excel 文件中启用 Excel 自动筛选

发布于 2024-10-31 12:19:44 字数 183 浏览 3 评论 0原文

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

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

在导出过程中,我可以在 C# 中做什么来为所有列启用自动筛选?

I have written a C# method that will export to Excel 2007, a html file that contains an html table. The data renders perfectly when the exported file is opened in Excel.

I would like to add an extra feature into the above export, so that when I open the exported data in Excel, the autofilter is turned on automatically.

What can I do in C# during export, to enable autofilter for all columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情洁癖 2024-11-07 12:19:44

如果要自动使过滤器显示在通过以编程方式将 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>

If you want to automatically make the filters appear for header row in an Excel file that is created through programatically exporting html to an .xls file, then remember to add the following 2 features to this file.

  • Add schemas for Excel to html tag as in bold in listing below. Add exactly these schemas.
  • Add to each header column 'td' tag the following attribute x:autofilter='all' which is also in bold in listing below.

That's it. Then you are done. Its as simple as this. A sample listing that I saved as 'SampleInHTML.xls' from notepad and tested in Excel 2007 AND Excel 2010 to work perfectly with auto-filtering appearing automatically on opening the saved file in Excel, is given below.

<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>
善良天后 2024-11-07 12:19:44

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

我已经使用了 Gembox.Spreadsheet ,它将能够做你想做的事情。然而,它不是免费的。

To do these kind of things, you have to operate directly against a spreadsheet obect. As far as I know this cannot be done by exporting a HTML table.

I have used Gembox.Spreadsheet and it will be able to do what you want. However, it is not free.

撩动你心 2024-11-07 12:19:44

好吧,我只需使用以下几点即可实现此目的:

  1. 命名空间 xmlns:x="urn:schemas-microsoft-com:office:excel"
  2. th 的 Autofilter 属性 - 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>

Well, I was able to achieve this just using below points:

  1. namespace xmlns:x="urn:schemas-microsoft-com:office:excel".
  2. Autofilter attribute for th - x:autofilter='all'

Below is the sample html for that.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文