2016-11-28 95 views
1

我是Java新手。我正在尝试使用JavaScript阅读excel文件。无法直接从使用javascript的路径读取excel文件

使用下面的代码,我正在从对象“的FileReader”中提出,从浏览按钮选择文件的文件,我需要直接将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> 
+0

我假设你正在使用ignite-ui库。另外,你准确得到的错误是什么?我无法从你的问题中弄清楚这一点。 – Sid

+0

是的,我使用ignite-ui库,只需要把excel文件的url直接放到 – wisam

+0

@wesam你是什么意思,你想要“url文件直接”?浏览按钮不是让您从文件系统中选择文件,还是要从外部源文件加载文件而不是从文件系统加载文件? –

回答

0

你的问题是相关的到FileReader API,而不是Ignite UI Excel引擎。下面介绍一下文件阅读器可以做到:

的的FileReader对象允许Web应用程序异步读取存储在用户的计算机上, 使用文件或BLOB对象指定的文件或文件(或原始数据缓冲区)的 内容要读取的数据。从文件列表对象返回作为使用该元件中选择文件的用户的一个 结果,从一个 拖放操作的dataTransfer对象,或者从 mozGetAsFile()API上HTMLCanvasElement

文件对象可以被获得。

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

相关问题