2016-11-28 96 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本身無法訪問客戶端計算機上的整個文件系統,因爲有許多安全問題。

相關問題