vue3 + ts 导入.xlsx并读取数据

1、先安装插件 xlsx

npm install xlsx

2、代码

<template>
  <el-upload
    :show-file-list="false"
    accept=".xlsx"
    :auto-upload="false"
    :limit="1"
    :on-change="handleOnchange"
  >
    <el-button type="primary" style="margin-right: 10px;">导入</el-button>
  </el-upload> 
</template>
  
<script lang="ts">
import { defineComponent, shallowRef } from "vue";
import type { UploadFile } from "element-plus";
import * as xlsx from "xlsx";

export default defineComponent({
  name: "importExcel",
  setup() {
    // 上传
    let ExcelList = shallowRef<any[]>([]);
    const handleOnchange = (uploadFile: UploadFile) => {
     
      let file = uploadFile.raw;  // 获取文件信息
      
      const fileReader = new FileReader(); //创建读取器
      fileReader.readAsBinaryString(file!); // 导入xlsx读取
      
      fileReader.onload = (ev) => { //读取完成
        const data = ev.target?.result; //获取内容
        
        const workbook = xlsx.read(data, { // 读取工作簿
            type: "binary", // 以字符编码的方式解析
        });

		const exlname = workbook.SheetNames[0]; // 取第一张表
		const d = workbook.Sheets[exlname];

		const exl = xlsx.utils.sheet_to_json(d).map((v: any) => {
            return {
              name: v.物料长代码,
              date: v.扫码信息,
            };
        });
        ExcelList.value = exl;
      };
    };

    return {
      handleOnchange,
    };
  },
});
</script>

3、表格样式
在这里插入图片描述
4、拿到的数据格式
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在Vue 3项目中使用TypeScript和Webpack,并且正确地生成.d.ts声明文件,你需要按照以下步骤进行配置: 1. 安装依赖: ```bash npm install --save-dev typescript ts-loader vue-loader@next @types/vue @vue/compiler-sfc ``` 2. 创建`tsconfig.json`文件: 在项目根目录下创建一个名为`tsconfig.json`的文件,并添加以下内容: ```json { "compilerOptions": { "target": "esnext", "module": "esnext", "strict": true, "jsx": "preserve", "moduleResolution": "node", "resolveJsonModule": true, "esModuleInterop": true, "lib": ["esnext", "dom"], "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx"] } ``` 3. 配置Webpack: 在Webpack配置文件中,添加对TypeScript和Vue文件的处理规则。例如,如果你使用的是`webpack.config.js`文件,可以按照以下示例进行配置: ```javascript const path = require('path'); module.exports = { // ...其他配置 resolve: { extensions: ['.js', '.ts', '.vue'], alias: { '@': path.resolve(__dirname, 'src') } }, module: { rules: [ { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, options: { appendTsSuffixTo: [/\.vue$/] } }, { test: /\.vue$/, loader: 'vue-loader' } ] }, // ...其他配置 }; ``` 4. 添加Vue文件声明: 在项目的`src`目录中,为Vue文件添加一个`.d.ts`的同名文件。例如,对于`HelloWorld.vue`组件,创建一个名为`HelloWorld.vue.d.ts`的文件,并添加以下内容: ```typescript declare module '*.vue' { import { ComponentOptions } from 'vue'; const componentOptions: ComponentOptions; export default componentOptions; } ``` 5. 构建项目: 运行Webpack构建命令,以启动TypeScript编译和生成声明文件: ```bash npm run build ``` 如果一切顺利,你的Vue 3 + TypeScript项目应该可以成功编译,并且在构建过程中会生成相应的.d.ts声明文件。 希望以上步骤可以帮助你配置Vue 3 + TypeScript + Webpack项目的.d.ts声明文件。如果有任何问题,请随时追问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值