讀取本地Excel檔案生成echarts
Excel樣式
1.匯入
<script type="text/javascript" src="xlsx.core.min.js"></script>
或
yarn add xlsx
import XLSX from 'xlsx'
2.上傳本地檔案
const File = function () {
var that;
var obj = function () {
that = this;
window.addEventListener("load", that._init, false);
}
obj.prototype = {
upload: function (options) {
that._initByOptions(options);
that.fileInput.click();
},
_initByOptions: function (options) {
that.fileInput.value = "";
that.options = {
multi: false,
url: "",
accept: "",
param: null,
uploadType:'',
before: function () {
},
after: function () {
},
progress: function () {
}
};
if (options) {
for (var i in options) {
that.options[i] = options[i];
}
}
// multiple
if (that.options.multi) that.fileInput.setAttribute("multiple", "multiple");
else that.fileInput.removeAttribute("multiple");
// accept
that.fileInput.setAttribute("accept", that.options.accept || "");
},
_init: function () {
var ipt = document.createElement("input");
ipt.style.display = "none";
ipt.setAttribute("type", "file");
ipt.addEventListener("change", that._fileChange, false);
document.body.appendChild(ipt);
that.fileInput = ipt;
},
_fileChange: function () {
if (that.options.before) {
var result = that.options.before(this.files);
if (result == false) return;
}
for (var i = 0; i < this.files.length; i++) {
that._uploadFile(this.files[i]);
}
},
_uploadFile: function (file) {
if(that.options.uploadType == 'local'){
that.options.after && that.options.after(file);
return;
}
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
that.options.progress && that.options.progress(evt.loaded / evt.total);
} else {
// No data to calculate on
}
}, false);
xhr.addEventListener("load", function () {
}, false);
xhr.open("post", that.options.url);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
that.options.after && that.options.after(xhr.responseText, file);
xhr = null;
}
}
}
xhr.onerror = function () {
that.options.error && that.options.error();
}
// Send the file (doh)
if ("getAsBinary" in file) {
//FF 3.5+
xhr.sendAsBinary(file.getAsBinary());
} else {
var formData = new FormData();
formData.append("path", "default");
formData.append("upload_file", file);
formData.append("type", file.name.substring(file.name.indexOf(".")));
if (that.options.param) {
for (var p in that.options.param) {
formData.append(p, that.options.param[p]);
}
}
if (me.global.token) {
xhr.setRequestHeader('token', me.global.token)
}
xhr.send(formData);
}
}
}
return new obj();
};
export default File();
3.讀取Excel
function XLSXRender(data, accept, maxSize, callBack, maxCount) {
File.upload({
multi: true,
accept: accept,
uploadType: "local",
before: function (files) {},
after: function (file) {
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
var workbook = XLSX.read(data, {type: 'binary'});
var sheetNames = workbook.SheetNames; // 工作表名稱集合
var worksheet = workbook.Sheets[sheetNames[0]]; // 這裡我們只讀取第一張sheet
callBack && callBack(XLSX.utils.sheet_to_json(worksheet));
};
reader.readAsBinaryString(file);
},
error: function () {},
progress: function () {}
});
}
4.生成echarts
XLSXRender({}, "image/jpg;image/jpeg;image/png;image/gif;image/bmp;", 500, function (data) {
if (!data) return;
var source = [];
var a = {};
for(let i in data){
for(let key in data[i]) {
if(!a[key])a[key] = [key]
a[key].push(data[i][key])
}
}
for(let i in a){
source.push(a[i])
}
initData(source)
})
function initData(source){
const _data = document.getElementById("data");
const option = {
legend: {},
tooltip: {},
dataset: {
source: source
},
xAxis: {type: 'category'},
yAxis: {},
series: [
{type: 'bar'},
{type: 'bar'},
{type: 'bar'}
]
}
if (!_data) {
_data.setOption(option)
return;
}
var _data = this.$echarts.init(_data)
_data.setOption(option)
}
相關文章
- js如何讀取excel檔案,繪製echarts圖形。JSExcelEcharts
- js讀取excel檔案,繪製echarts圖形---資料處理JSExcelEcharts
- 前端讀取excel檔案前端Excel
- spark直接讀取本地檔案系統的檔案Spark
- nodejs 讀取excel檔案,並去重NodeJSExcel
- java 生成 excel檔案JavaExcel
- 流式生成Excel檔案Excel
- POI生成EXCEL檔案Excel
- 使用openpyxl庫讀取Excel檔案資料Excel
- Maatwebsite\Excel 讀取帶公式的excel檔案得到值呢?WebExcel公式
- Java 讀取txt檔案生成Word文件Java
- Python生成器讀取大檔案Python
- Python實用方法之讀取本地檔案Python
- 用Groovy讀取本地檔案的程式碼
- C# 讀取txt檔案生成Word文件C#
- 用 ABAP 讀取本地文字檔案內容試讀版
- Java讀取本地檔案內容並輸出Java
- Pandas之EXCEL資料讀取/儲存/檔案分割/檔案合併Excel
- python讀寫excel檔案PythonExcel
- Python讀取大量Excel檔案並跨檔案批次計算平均值PythonExcel
- python讀取兩個excel資料檔案輸出整理好以後的excel資料檔案PythonExcel
- EasyExcel庫來讀取指定Excel檔案中的資料Excel
- 【python】python初學 讀取map檔案資料到excel中PythonExcel
- 基礎補充:使用xlrd模組讀取excel檔案Excel
- 用 ABAP 新建本地 Excel 檔案並寫入資料試讀版Excel
- shell讀取構建檔案資訊生成json字串JSON字串
- JavaScript讀取本地檔案內容(相容低版本IE)JavaScript
- Java 根據模板生成 PDF 檔案 以及 excel 檔案JavaExcel
- 基於.NET的程式讀取Excel檔案的解決方案Excel
- php讀取excel檔案資料的匯入和匯出PHPExcel
- EXCEL不可讀怎麼辦,修復EXCEL檔案不可讀Excel
- 任意檔案讀取
- Java 讀取檔案Java
- java呼叫window本地應用程式;讀取TXT型別檔案Java型別
- 將 SAP ABAP 內表內容本地匯出成 Excel 檔案試讀版Excel
- Python 高階技巧:深入解析讀取 Excel 檔案的多種方法PythonExcel
- Laravel Excel 如何獲取 Excel 檔案的公式結果LaravelExcel公式
- IOC - 讀取配置檔案