【问题标题】:Import Excel file to a table of HTML page将 Excel 文件导入 HTML 页面的表格
【发布时间】:2015-09-18 08:58:45
【问题描述】:

我的计算机中有一个简单的 Excel 文件,位于“D:/Book1.xls”。我想将其导入以制作表格并将表格附加到我的 HTML 页面中的 div 标记。

你会修改我下面的代码吗?

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style type="text/css">

</style>
<script src='http://alasql.org/console/alasql.min.js'></script>
<script src='http://alasql.org/console/xlsx.core.min.js'></script>
<script src="./libs/jquery-2.1.4.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        alasql('select * into html("#res",{headers:true}) \
                  from xlsx("d:/Book1.xls",\
                            {headers:true})');
        alert("end of function")
    });
</script>
</head>
<body>
    <div id="res">res</div>
</body>
</html>

【问题讨论】:

    标签: javascript jquery excel alasql


    【解决方案1】:

    问题是您试图直接从网页访问文件,这是不可能的。您无法访问浏览器之外的任何文件。为此,您必须选择 html 的输入元素,并在获取文件数据后将其存储到 javascript 变量中。

    <script src="alasql.min.js"></script>
    <script src="xlsx.core.min.js"></script>
    <p>Select CSV file to read:</p>
    <input id="readfile" type="file" onchange="loadFile(event)"/>
    <script>
        function loadFile(event) {
            alasql('SELECT * FROM FILE(?,{headers:true})',[event],function(data){
                console.log(data);
                // You can data to div also.
            });
         }
    </script>
    

    【讨论】:

    • 感谢您的友好评论,
    • 我刚刚解决了我的问题。这是一个非常简单和微不足道的错误。 “xlsx()”函数只能读取“Book1.xlsx”文件而不是“Book1.xls”文件。我已将 .xls 文件更改为 xlsx 文件,并且代码工作正常。 alasql('select * into html("#res",{headers:true}) \ from xlsx("Book1.xlsx",
    【解决方案2】:

    脚本

    <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/infragistics.core.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/infragistics.lob.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_core.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_collections.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_text.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_io.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_ui.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.documents.core_core.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_collectionsextended.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.excel_core.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_threading.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.ext_web.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.xml.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.documents.core_openxml.js"></script>
    <script type="text/javascript" src="http://cdn-na.infragistics.com/igniteui/2018.2/latest/js/modules/infragistics.excel_serialization_openxml.js"></script>
    

    JS

    $(function () {
        $("#input").on("change", function () {
            var excelFile,
                fileReader = new FileReader();
    
            $("#result").hide();
    
            fileReader.onload = function (e) {
                var buffer = new Uint8Array(fileReader.result);
    
                $.ig.excel.Workbook.load(buffer, function (workbook) {
                    var column, row, newRow, cellValue, columnIndex, i,
                        worksheet = workbook.worksheets(0),
                        columnsNumber = 0,
                        gridColumns = [],
                        data = [],
                        worksheetRowsCount;
    
                    // Both the columns and rows in the worksheet are lazily created and because of this most of the time worksheet.columns().count() will return 0
                    // So to get the number of columns we read the values in the first row and count. When value is null we stop counting columns:
                    while (worksheet.rows(0).getCellValue(columnsNumber)) {
                        columnsNumber++;
                    }
    
                    // Iterating through cells in first row and use the cell text as key and header text for the grid columns
                    for (columnIndex = 0; columnIndex < columnsNumber; columnIndex++) {
                        column = worksheet.rows(0).getCellText(columnIndex);
                        gridColumns.push({ headerText: column, key: column });
                    }
    
                    // We start iterating from 1, because we already read the first row to build the gridColumns array above
                    // We use each cell value and add it to json array, which will be used as dataSource for the grid
                    for (i = 1, worksheetRowsCount = worksheet.rows().count(); i < worksheetRowsCount; i++) {
                        newRow = {};
                        row = worksheet.rows(i);
    
                        for (columnIndex = 0; columnIndex < columnsNumber; columnIndex++) {
                            cellValue = row.getCellText(columnIndex);
                            newRow[gridColumns[columnIndex].key] = cellValue;
                        }
    
    
                        data.push(newRow);
                    }
    
                    // we can also skip passing the gridColumns use autoGenerateColumns = true, or modify the gridColumns array
                    createGrid(data, gridColumns);
                }, function (error) {
                    $("#result").text("The excel file is corrupted.");
                    $("#result").show(1000);
                });
            }
    
            if (this.files.length > 0) {
                excelFile = this.files[0];
                if (excelFile.type === "application/vnd.ms-excel" || excelFile.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || (excelFile.type === "" && (excelFile.name.endsWith("xls") || excelFile.name.endsWith("xlsx")))) {
                    fileReader.readAsArrayBuffer(excelFile);
                } else {
                    $("#result").text("The format of the file you have selected is not supported. Please select a valid Excel file ('.xls, *.xlsx').");
                    $("#result").show(1000);
                }
            }
    
        })
    });
    
    function createGrid(data, gridColumns) {
        if ($("#grid1").data("igGrid") !== undefined) {
            $("#grid1").igGrid("destroy");
        }
    
        $("#grid1").igGrid({
            columns: gridColumns,
            autoGenerateColumns: true,
            dataSource: data,
            width: "100%",
    
        });
    
    
    }
    

    HTML

    <input type="file" id="input" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
    
    <div id="result"></div>
    <table id="grid1"></table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 2016-06-10
      相关资源
      最近更新 更多