javascript Excel数据转成html输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23808188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:36:19  来源:igfitidea点击:

Excel data into html output

javascriptjqueryhtml

提问by KM123

I have managed to find out how to pull data from a excel file into HTML.

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

I am now trying to look how to search for values within a set of cells. Does anyone know how to achieve this?

我现在正在尝试查看如何在一组单元格中搜索值。有谁知道如何实现这一目标?

Thanks in advance!

提前致谢!

回答by Robert P

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

回答by workabyte

jQuery is likely going to help you with that. when building the HTML i would also add data-[somethingHelpfulWhenSearching] or add class values that could help.

jQuery 很可能会帮助你解决这个问题。在构建 HTML 时,我还会添加 data-[somethingHelpfulWhenSearching] 或添加可能有帮助的类值。

then you can search for the item by class

然后您可以按类别搜索该项目

$('.[searchableClassName]')

or by data attribute:

或按数据属性:

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

hope this helps

希望这可以帮助

回答by Neil VanLandingham

From the way you worded the question, it sounds like you have a table in your HTML, and you just want to loop over all of the cells to check which cells contain a given value, and return those DOM nodes that contain the provided search string within their text content. If that's an accurate interpretation, here is a Vanilla JS solution:

从您提出问题的方式来看,听起来您的 HTML 中有一个表格,您只想遍历所有单元格以检查哪些单元格包含给定值,并返回那些包含提供的搜索字符串的 DOM 节点在他们的文本内容中。如果这是一个准确的解释,这里是一个 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;
}

回答by Silviu Burcea

Since you're already using jQuery, try DataTables, which is a jQuery plugin and does a lot more than filtering for you. It allows for both client side and server side filtering, so it's not a problem if your table is large.

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