nodejs读取Excel API
发布于 8 年前 作者 151263 9330 次浏览 来自 分享

nodejs读取Excel API

npm install read_xlsx

https://github.com/sail-sail/read_xlsx

var fs = require("fs");
var read_xlsx = require("../read_xlsx");
var excelBuffer = fs.readFileSync("./test.xlsx");
//返回Promise对象
read_xlsx.getWorkbook(excelBuffer).then(function(workbook){
	//获得所有工作簿名称
	var sheetNames = workbook.getSheetNames();
	console.log(sheetNames);	
	//获得名称为Sheet1的工作簿
	workbook.getSheet("Sheet1").then(function(sheet){
		//获得总行数
		var rowLen = sheet.getRows();
		//获得总列数
		var cellLen = sheet.getColumns();
		//遍历所有单元格
		for(var i=0; i<rowLen; i++) {
			for(var k=0; k<cellLen; k++) {
				var cell = sheet.getCell(i,k);
				//If the cell is empty, it is possible that the cell does not exist return null!
				if(cell !== null) {
					//打印单元格内容
					console.log(cell.getName()+":"+cell.getContents())
				}
			}
		}
		//find cell by name 通过名称获得某个单元格
		var a1Sheet = sheet.findCell("A1");
		var a1Str = a1Sheet.getContents();
		console.log(a1Str)
	})["catch"](function(err) {
		console.error(err.stack);
	});
});
2 回复

支持,前排广告。

回到顶部