JS Excelファイル(xls)互換Chrome/IE/Firefoxを読み込む
Excelを読み込むにはまず2つのJSファイルが必要です.
ダウンロードアドレス
Htmlコード:
JSコード(excel.acc.js):
上記の2ページのコード+JS-ExcelプラグインでXlsファイルの読み取りが完了します.
ダウンロードアドレス
Htmlコード:
New Document
var drop = document.getElementById('xlf');
// Change
if(drop.addEventListener) {
drop.addEventListener('change', handleFile, false);
}
function excelReader(data,json) {
//
document.getElementById('txtArea').innerText = json;
}
JSコード(excel.acc.js):
// shim.js、xls.js
//
var X = XLS;
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 fixdata(data) {
var o = "", l = 0, w = 10240;
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
return o;
}
function process_wb(wb,type) {
var output = "";
switch (type) {
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;
return output;
}
function ReadExcel(out) {
// json
var exlData = JSON.parse(out);
// Page1 ,
// if (exlData.Page1 == null || exlData.Page1 == undefined) {
// msgErro(" Excel !");
// //saveLoading('hide');
// return;
// }
//
excelReader(exlData,out);
}
// Input Change
function handleFile(e) {
if (e.target.files[0].name.indexOf(".xls") < 0) {
msgErro(" .xls !");
return;
}
//saveLoading('show');
var files = e.target.files;
var output = "";
var f = files[0];
{
var reader = new FileReader();
var name = f.name;
reader.onload = function (e) {
var data = e.target.result;
var arr = fixdata(data);
var wb = X.read(btoa(arr), { type: 'base64' });
output = process_wb(wb, "json");
ReadExcel(output);
};
reader.readAsArrayBuffer(f);
}
}
上記の2ページのコード+JS-ExcelプラグインでXlsファイルの読み取りが完了します.