admin管理员组

文章数量:1399916

I have managed to find out how to pull data from a excel file into 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!

I have managed to find out how to pull data from a excel file into 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!

Share Improve this question asked May 22, 2014 at 13:24 KM123KM123 1,3871 gold badge11 silver badges21 bronze badges 2
  • if my answer is not helpful, i think i will need some more info, an example of what you are trying to search for/against and what you are looking to do with the results. – workabyte Commented May 22, 2014 at 13:37
  • Please provide code how you were able to convert the Excel data to HTML. That will help to answer how to search in the data. – Petr Hejda Commented May 30, 2018 at 12:43
Add a ment  | 

4 Answers 4

Reset to default 0

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.

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

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:

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

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.

本文标签: javascriptExcel data into html outputStack Overflow