Ⅰ js中xlsx.utils怎麼拿到所有的行
參考這個方法:
var sheet2arr = function(sheet){
var result = [];
var row;
var rowNum;
var colNum;
var range = XLSX.utils.decode_range(sheet['!ref']);
for(rowNum = range.s.r; rowNum <= range.e.r; rowNum++){
row = [];
for(colNum=range.s.c; colNum<=range.e.c; colNum++){
var nextCell = sheet[
XLSX.utils.encode_cell({r: rowNum, c: colNum})
];
if( typeof nextCell === 'undefined' ){
row.push(void 0);
} else row.push(nextCell.w);
}
result.push(row);
}
return result;
};
Ⅱ 怎麼使用github的js-xlsx庫進行excel文件處理
JS-XLS
JS-XLS的安裝是很簡單的,在瀏覽器中使用該腳本文件,並使用腳本標記載入它:
<!-- https://github.com/SheetJS/js-xls/blob/master/xls.js -->
<script src="/path/to/xls.js"></script>
對於Node.js,只要通過NPM安裝:
$ npm install xlsjs
$ node
> require('xlsjs').readFile('excel_file.xls');
然後,我們可以簡單地使用他們提供的代碼在他們的index.html文件解析和轉換的xls文件JSON,CSV或公式為基礎的輸出。
function get_radio_value( radioName ) {
var radios = document.getElementsByName( radioName );
for( var i = 0; i < radios.length; i++ ) {
if( radios[i].checked ) {
return radios[i].value;
}
}
}
function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var roa = XLS.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return result;
}
function to_csv(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var csv = XLS.utils.make_csv(workbook.Sheets[sheetName]);
if(csv.length > 0){
result.push("SHEET: " + sheetName);
result.push("");
result.push(csv);
}
});
return result.join("\n");
}
function to_formulae(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var formulae = XLS.utils.get_formulae(workbook.Sheets[sheetName]);
if(formulae.length > 0){
result.push("SHEET: " + sheetName);
result.push("");
result.push(formulae.join("\n"));
}
});
return result.join("\n");
}
var tarea = document.getElementById('b64data');
function b64it() {
var cfb = XLS.CFB.read(tarea.value, {type: 'base64'});
var wb = XLS.parse_xlscfb(cfb);
process_wb(wb);
}
function process_wb(wb) {
var output = "";
switch(get_radio_value("format")) {
case "json":
output = JSON.stringify(to_json(wb), 2, 2);
break;
case "form":
output = to_formulae(wb);
break;
default:
output = to_csv(wb);
}
if(out.innerText === undefined) out.textContent = output;
else out.innerText = output;
}
var drop = document.getElementById('drop');
function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
var i,f;
for (i = 0, f = files[i]; i != files.length; ++i) {
var reader = new FileReader();
var name = f.name;
reader.onload = function(e) {
var data = e.target.result;
var cfb = XLS.CFB.read(data, {type: 'binary'});
//var arr = String.fromCharCode.apply(null, new Uint8Array(data));
//var cfb = XLS.CFB.read(btoa(arr), {type: 'base64'});
var wb = XLS.parse_xlscfb(cfb);
process_wb(wb);
};
reader.readAsBinaryString(f);
//reader.readAsArrayBuffer(f);
}
}
function handleDragover(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = '';
}
if(drop.addEventListener) {
drop.addEventListener('dragenter', handleDragover, false);
drop.addEventListener('dragover', handleDragover, false);
drop.addEventListener('drop', handleDrop, false);
}
JS代碼很容易理解,它使用了HTML5拖放API允許我們上傳文件然後看他們為二進制字元串。二進制,然後直接傳遞到庫,並進一步分析使用此代碼:
var cfb = XLS.CFB.read(data, {type: 'binary'});
var wb = XLS.parse_xlscfb(cfb);
最終,WB(Workbook)對象是處理並轉換成需要的格式(JSON,CSV和Excel公式)。這沒有HTML5拖放文件閱讀器的API,可用於Node.js代碼相同。
註:此模塊不支持xlsx。為了克服這一限制,我們可以通過同一作者稱為JS-XLSX用另一個包。