2017-05-24 257 views
0

嘗試使用 cellValue = worksheet.getRow(1).getCell(1).value;TypeError:無法讀取未定義的屬性'getRow'

和 cellValue =的console.log(worksheet.getCell( 'A1'))

請在下面找到我的代碼:

cellread2=function(){ 
     var Excel; 
     var filePath = path.resolve(__dirname,'E:/excel.xlsx'); 
     if (typeof require !== 'undefined') { 
        Excel = require('C:/Users/user/AppData/Roaming/npm/node_modules/exceljs/dist/exceljs'); 
     } 
     var wb = new Excel.Workbook(); 
     console.log(wb); 
     wb.xlsx.readFile(filePath); 
     var worksheet = wb.getWorksheet('Sheet1'); 
     //cellValue= worksheet.getRow(1).getCell(1).value;//Error :TypeError: Cannot read property 'getRow' of undefined 
     cellValue=console.log(worksheet.getCell('A1'))// TypeError: Cannot read property 'getCell' of undefined 
     console.log(cellValue); 


    } 

回答

0

沒有與現有的庫代碼中的問題。如果你想使它工作,你將不得不對文件AppData\Roaming\npm\node_modules\exceljs\dist\es5\xlsx\xform\sheet\worksheet-xform.js中的現有代碼進行一些更改。將第284行的代碼替換爲以下內容:

if (drawing.anchors && drawing.anchors.length > 0) { 
     drawing.anchors.forEach(function (anchor) {    
      if (anchor.medium && anchor.range) { 
      var image = { 
       type: 'image', 
       imageId: anchor.medium.index, 
       range: anchor.range 
      }; 
      model.media.push(image); 
      } 
     }); 
     } 

至於用於讀取文件的代碼,請使用以下代碼。

注:我全球的裝機量exceljs庫,我使用的版本4.6.1

var path = require('path'); 
var Excel = require('exceljs'); 
var cellread2 = function() { 
    var filePath = path.resolve('E:', 'excel.xlsx'); 
    var wb = new Excel.Workbook(); 
    wb.xlsx.readFile(filePath).then(function (data) { 
    var worksheet = wb.getWorksheet('Sheet1'); 
    var cellValue = worksheet.getRow(1).getCell(1).value; 
    console.log('cellValue', cellValue); 
    }).catch(function (e) { 
    console.log(e); 
    }); 
} 
cellread2(); 
相關問題