【问题标题】:Excel data into html outputExcel 数据转换成 html 输出
【发布时间】:2014-07-11 13:42:47
【问题描述】:

我已经设法了解如何将数据从 excel 文件中提取到 HTML 中。

我现在正在尝试研究如何在一组单元格中搜索值。有谁知道如何做到这一点?

提前致谢!

【问题讨论】:

  • 如果我的回答没有帮助,我想我需要更多信息,例如您尝试搜索/反对的内容以及您希望如何处理结果。
  • 请提供如何将 Excel 数据转换为 HTML 的代码。这将有助于回答如何在数据中进行搜索。

标签: javascript jquery html


【解决方案1】:

既然您已经在使用 jQuery,请尝试使用 DataTables,这是一个 jQuery 插件,它为您做的不仅仅是过滤。它允许客户端和服务器端过滤,所以如果你的表很大,这不是问题。

【讨论】:

    【解决方案2】:
    <html>
        <script>
            function mytest1() {
                var Excel, Book; // Declare the variables
                Excel = new ActiveXObject("Excel.Application"); // Create the Excel application object.
                Excel.Visible = false; // Make Excel invisible.
                Book = Excel.Workbooks.Add() // Create a new work book.
                Book.ActiveSheet.Cells(2, 2).Value = document.all.my_textarea1.value;
                Book.SaveAs("C:/temp/TEST.xls");
                Excel.Quit(); // Close Excel with the Quit method on the Application object.
            }
    
            function mytest2() {
                var Excel;
                Excel = new ActiveXObject("Excel.Application");
                Excel.Visible = false;
                form1.my_textarea2.value = Excel.Workbooks.Open("C:/temp/TEST.xls").ActiveSheet.Cells(1, 1).Value;
                Excel.Quit();
            }
        </script>
    
        <body>
            <form name="form1">
                <input type=button onClick="mytest1();" value="Send Excel Data">
                <input type=text name="my_textarea1" size=70 value="enter ur data here">
                <br><br>
                <input type=button onClick="mytest2();" value="Get Excel Data">
                <input type=text name="my_textarea2" size=70 value="no data collected yet">
            </form>
        </body>
    </html>
    

    【讨论】:

    • 这难道不是只在 IE 中有效,除非用户跳过一些障碍在其他浏览器上安装活动 x?
    【解决方案3】:

    从您提出问题的方式来看,您的 HTML 中似乎有一个表格,您只想遍历所有单元格以检查哪些单元格包含给定值,并返回那些包含在其文本内容中提供了搜索字符串。如果这是一个准确的解释,这里有一个 Vanilla JS 解决方案:

    function findCells(str) {
        var allCells = document.querySelectorAll("td");
        var matchingCells = [];
        for (var i = 0; i < allCells.length; i++) {
            if (allCells[i].textContent.indexOf(str) !== -1) {
                matchingCells.push(allCells[i]);
            }
        }
        return matchingCells;
    }
    

    【讨论】:

      【解决方案4】:

      jQuery 可能会帮助你。在构建 HTML 时,我还会添加数据-[somethingHelpfulWhenSearching] 或添加可以提供帮助的类值。

      然后你可以按类别搜索项目

      $('.[searchableClassName]')
      

      或按数据属性:

      $('[data-[somethingHelpfulWhenSearching]') //only looking that the tag exists
      $('[data-[somethingHelpfulWhenSearching]="something im looking for"') //only looking that the tag and checking the value
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2014-06-09
        • 1970-01-01
        • 1970-01-01
        • 2014-02-08
        • 1970-01-01
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多