sheetjs-clone/demos/vue/pages/index.vue
SheetJS 75845a0ca7 version bump 0.11.19: browser writeFile
- IE6-9 ActiveX + VBScript shim
- `writeFile` supported in browser
- `oldie` demo for IE write strategies
2018-02-03 15:46:32 -05:00

93 lines
2.8 KiB
Vue

<!-- xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com -->
<template>
<div @drop="_drop" @dragenter="_suppress" @dragover="_suppress">
<div class="row"><div class="col-xs-12">
<form class="form-inline">
<div class="form-group">
<label for="file">Spreadsheet</label>
<input type="file" class="form-control" id="file" :accept="SheetJSFT" @change="_change" />
</div>
</form>
</div></div>
<div class="row"><div class="col-xs-12">
<button :disabled="data.length ? false : true" class="btn btn-success" @click="_export">Export</button>
</div></div>
<div class="row"><div class="col-xs-12">
<div class="table-responsive">
<table class="table table-striped">
<thead><tr>
<th v-for="c in cols" :key="c.key">{{c.name}}</th>
</tr></thead>
<tbody>
<tr v-for="(r, key) in data" :key="key">
<td v-for="c in cols" :key="c.key"> {{ r[c.key] }}</td>
</tr>
</tbody>
</table>
</div>
</div></div>
</div>
</template>
<script>
const make_cols = refstr => Array(XLSX.utils.decode_range(refstr).e.c + 1).fill(0).map((x,i) => ({name:XLSX.utils.encode_col(i), key:i}));
const _SheetJSFT = [
"xlsx", "xlsb", "xlsm", "xls", "xml", "csv", "txt", "ods", "fods", "uos", "sylk", "dif", "dbf", "prn", "qpw", "123", "wb*", "wq*", "html", "htm"
].map(function(x) { return "." + x; }).join(",");
export default {
data() {
return {
data: ["SheetJS".split(""), "1234567".split("")],
cols: [
{name:"A", key:0},
{name:"B", key:1},
{name:"C", key:2},
{name:"D", key:3},
{name:"E", key:4},
{name:"F", key:5},
{name:"G", key:6},
],
SheetJSFT: _SheetJSFT
}; },
methods: {
_suppress(evt) { evt.stopPropagation(); evt.preventDefault(); },
_drop(evt) {
evt.stopPropagation(); evt.preventDefault();
const files = evt.dataTransfer.files;
if(files && files[0]) this._file(files[0]);
},
_change(evt) {
const files = evt.target.files;
if(files && files[0]) this._file(files[0]);
},
_export(evt) {
/* convert state to workbook */
const ws = XLSX.utils.aoa_to_sheet(this.data);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "SheetJS");
/* generate file and send to client */
XLSX.writeFile(wb, "sheetjs.xlsx");
},
_file(file) {
/* Boilerplate to set up FileReader */
const reader = new FileReader();
reader.onload = (e) => {
/* Parse data */
const bstr = e.target.result;
const wb = XLSX.read(bstr, {type:'binary'});
/* Get first worksheet */
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
/* Convert array of arrays */
const data = XLSX.utils.sheet_to_json(ws, {header:1});
/* Update state */
this.data = data;
this.cols = make_cols(ws['!ref']);
};
reader.readAsBinaryString(file);
}
}
};
</script>