无法直接从使用JavaScript的path读取excel文件

我是Java新手。 我正在尝试阅读使用JavaScript的Excel文件。

使用下面的代码,我正在从对象“fileReader”从浏览button读取所选文件的文件,我需要直接插入URL文件,代码:

<script type="text/javascript"> function myFunction() { var x = document.getElementById("mySelect").value; document.getElementById("demo").innerHTML = x; } $(function () { $("#input").on("change", function () { var excelFile, fileReader = new FileReader(); $("#result").hide(); fileReader.onload = function (e) { var buffer = new Uint8Array(fileReader.result); var workbook = new $.ig.excel.Workbook("C:\\xampp\\htdocs\\TrustAgents.xlsx"); $.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%" }); } </script> 

您的问题与FileReader API相关,而不是Ignite UI Excel引擎。 以下是文件读取器可以执行的操作:

FileReader对象允许Web应用程序使用File或Blob对象asynchronous读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,以指定要读取的文件或数据。

文件对象可以从作为用户使用元素select文件,从拖放操作的DataTransfer对象或从HTMLCanvasElement上的mozGetAsFile()APIselect文件返回的FileList对象中获得。

这里是文档的链接 。 FileReader本身无法访问客户端计算机上的整个文件系统,因为有许多安全问题。

Interesting Posts