前言
SheetJS 是一個 JavaScript 函式庫,用於從表格中讀取和寫入資料。
下載
GitHub
官網 - SheetJS CE Docs - Vue
匯出資料 - Updating State
1 2 3 4 5 6
| <div class="importBtn__wrap ml-12px"> <div class="relative w-96px"> <el-button color="#F9EEA4">匯入名冊</el-button> <input @click="clearFile" @change="handleUpload" type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ref="uploadFile" class="absolute left-0 top-0 h-full w-full opacity-0"/> </div> </div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| const uploadFile = ref(null);
const importList = ref([]);
async function getWorkbookFromFile(excelFile) { return new Promise((resolve) => { const reader = new FileReader(); reader.onload = (event) => { const data = event.target.result; const workbook = XLSX.read(data, { type: 'binary' }); const rowObj = XLSX.utils.sheet_to_json(workbook.Sheets['工作表1']); resolve(rowObj); };
reader.readAsBinaryString(excelFile); }); }
async function handleUpload() { const file = uploadFile.value.files[0]; const workbook = await getWorkbookFromFile(file); importList.value = workbook.map((item) => reverseObjKey(item)); }
function clearFile(e) { e.target.value = ''; }
|
常見content-type對應表
常使用的幾種
種類 |
檔案副檔名 |
Content-Type(Mime-Type) |
2003 Excel |
.xls |
application/vnd.ms-excel |
2010 Excel |
.xlsx |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
文字檔案 |
.txt |
text/plain |
圖片 |
.png/.jpg/.gif |
image/* |
頁面 |
.htm/.html |
text/html |
影片 |
.avi/ .mpg/ .mpeg/ .mp4 |
video/* |
音訊 |
.mp3/ .wav/ |
audio/* |
PDF |
.pdf |
application/pdf |
參考資料
常見content-type對應表
JS - SheetJS