This commit is contained in:
icssoa 2023-05-30 22:40:55 +08:00
parent bbd057dbf0
commit 09003b1a74
3 changed files with 45 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{
"name": "front-next",
"version": "6.2.0",
"version": "6.3.0",
"scripts": {
"dev": "vite --host",
"build": "vite build",
@ -15,6 +15,7 @@
"@wangeditor/editor": "^5.1.23",
"@wangeditor/editor-for-vue": "^5.1.12",
"axios": "^1.3.4",
"chardet": "^1.5.1",
"core-js": "^3.23.5",
"dayjs": "^1.11.7",
"echarts": "^5.3.3",

View File

@ -12,6 +12,7 @@
<div class="inner">
<cl-upload
drag
:limit-size="limitSize"
:accept="accept"
:disabled="disabled"
type="file"
@ -33,6 +34,8 @@ import { useForm } from "@cool-vue/crud";
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue";
import XLSX from "xlsx";
import chardet from "chardet";
import { extname } from "/@/cool/utils";
const props = defineProps({
onConfig: Function,
@ -53,7 +56,11 @@ const props = defineProps({
default: 10
},
disabled: Boolean,
accept: String
accept: {
type: String,
default:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel,text/csv"
}
});
const emit = defineEmits(["change"]);
@ -63,7 +70,7 @@ const Form = useForm();
//
const upload = reactive({
filename: "",
data: []
data: [] as any[]
});
//
@ -127,27 +134,41 @@ function open() {
function onUpload(raw: File, _: any, { next }: any) {
const reader = new FileReader();
const ext = extname(raw.name);
reader.onload = (event: any) => {
const { result } = event.target;
let data = "";
const workbook = XLSX.read(result, { type: "binary" });
if (ext == "csv") {
const detected = chardet.detect(new Uint8Array(event.target.result));
const decoder = new TextDecoder(detected);
data = decoder.decode(event.target.result);
} else {
data = event.target.result;
}
let data: any[] = [];
const workbook = XLSX.read(data, { type: "binary" });
let json: any[] = [];
for (const sheet in workbook.Sheets) {
if (workbook.Sheets.hasOwnProperty(sheet)) {
data = data.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
json = json.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
}
}
// @ts-ignore
upload.data = data;
upload.data = json;
upload.filename = raw.name;
console.log(upload.filename, upload.data);
emit("change", data);
emit("change", json);
};
reader.readAsBinaryString(raw);
if (ext == "csv") {
reader.readAsArrayBuffer(raw);
} else {
reader.readAsBinaryString(raw);
}
next();
@ -166,6 +187,13 @@ function download() {
function setProgress(val: string) {
progress.value = parseInt(val);
}
defineExpose({
open,
clear,
setProgress,
Form
});
</script>
<style lang="scss" scoped>

View File

@ -1188,6 +1188,11 @@ chalk@^4.0.0, chalk@^4.1.2:
ansi-styles "^4.1.0"
supports-color "^7.1.0"
chardet@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-1.5.1.tgz#2a15770a46ce15e25a99f2741c949f43f9925dd5"
integrity sha512-0XMOtA52igKDOIfvJZJ6v0+J9yMF3IuYyEa5oFUxBXA01G6mwCNKpul3bgbFf7lmZuqwN/oyg/zQ1cGS7NyJkQ==
"chokidar@>=3.0.0 <4.0.0":
version "3.5.3"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd"