vue3 预览excel文件

 <el-dialog
		v-model="viewDialog"
		title="预览文件"
		custom-class="dialogCommution"
		width="750px"
		:close-on-click-modal="false"
   		@close="handleClose"
	>

    <div class="file-pre">
      <el-tabs v-model="activeName"  type="border-card">
        <el-tab-pane v-for="(item,index) in excelSheet" :key="index" :label="item.name" :name="item.name">
            <div class="table" v-html="item.innerHTML"></div>
        </el-tab-pane>
      </el-tabs>
   </div>
  </el-dialog>
// npm i xlsx@0.17.5
import XLSX from 'xlsx';

const viewDialog = ref(false)
const excelSheet = ref([])
const activeName = ref('')
const downLoad = (item)=>{
  downloadLocal({fileId:item.id}).then(res => {
      const data = new Uint8Array(res);
      const workbook = XLSX.read(data, {
          type: 'array'
      });
      // 删除掉没有数据的sheet
      Object.values(workbook.Sheets).forEach((sheet, index) => {
          if (Object.keys(sheet).indexOf('!ref') === -1) {
              delete workbook.Sheets[workbook.SheetNames[index]];
          }
      });
      tableToHtml(workbook);
    })

}

const tableToHtml = (workbook) => {
  const sheetList = workbook.SheetNames.filter(v => v.indexOf('数据源') === -1);
  activeName.value = sheetList[0];
  sheetList.forEach(sheet => {
      const worksheet = workbook.Sheets[sheet];
      if (worksheet) {
          const innerHTML = XLSX.utils.sheet_to_html(worksheet);
          excelSheet.value.push({
              name: sheet,
              innerHTML: innerHTML
          });
      } else {
          excelSheet.value.push({
              name: sheet,
              innerHTML: '暂无数据',
          });
      }
  });
    viewDialog.value = true
}
// 关闭dialog 清除缓存
const handleClose = ()=>{
  excelSheet.value = []
  activeName.value = ''
}
请求需携带 {responseType:‘arraybuffer’}
const res = await axiosGet($api.safe.downloadLocal, params,{responseType:'arraybuffer'});

async function axiosGet(
	url: string,
	params?: Record<string, any>,
	config?: AxiosRequestConfig
): Promise<any> {
	try {
		const res = await instance({
			method: "get",
			url,
			params,
			...config,
		});
		return res.data;
	} catch (err) {
		return Promise.reject(err);
	}
}
.file-pre {
	height: calc(100vh - 40px);
	padding: 20px;

	.table-html-wrap table {
		border-right: 1px solid #e8eaec;
		border-bottom: 1px solid #e8eaec;
		border-collapse: collapse;
		margin: auto;
	}

	.table-html-wrap table td {
		border-left: 1px solid #e8eaec;
		border-top: 1px solid #e8eaec;
		white-space: wrap;
		text-align: left;
		min-width: 100px;
		padding: 4px;
	}

	table {
		border-top: 1px solid #EBEEF5;
		border-left: 1px solid #EBEEF5;
		width: 100%;
		overflow: auto;

		tr {
			height: 44px;
		}

		td {
			min-width: 200px;
			max-width: 400px;
			padding: 4px 8px;
			border-right: 1px solid #EBEEF5;
			border-bottom: 1px solid #EBEEF5;
		}
	}

	.el-tabs--border-card > .el-tabs__content {
		overflow: auto;
		height: calc(100vh - 110px);
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下步骤在Vue中实现在线预览Excel文件: 1. 安装依赖:在终端中,进入您的Vue项目目录,并运行以下命令安装`xlsx`和`file-saver`依赖: ``` npm install xlsx file-saver ``` 2. 创建组件:创建一个名为`ExcelPreview`的Vue组件,并在模板中添加一个按钮和一个文件输入框: ```html <template> <div> <input type="file" @change="handleFileChange" /> <button @click="previewExcel">预览</button> </div> </template> ``` 3. 导入依赖:在组件的`<script>`标签中导入所需的依赖: ```javascript import XLSX from 'xlsx'; import FileSaver from 'file-saver'; ``` 4. 处理文件:在组件的`methods`中添加一个处理文件变化的方法,获取用户选择的Excel文件: ```javascript methods: { handleFileChange(event) { const file = event.target.files[0]; this.file = file; }, // 其他方法... } ``` 5. 预览Excel:在组件的`methods`中添加一个预览Excel文件的方法,使用`XLSX`库解析文件并生成预览: ```javascript methods: { // 其他方法... previewExcel() { const reader = new FileReader(); reader.onload = (e) => { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: 'array' }); const firstSheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[firstSheetName]; const excelData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); // 在此处处理预览Excel数据,例如展示在页面上或使用其他组件进行渲染 console.log(excelData); }; reader.readAsArrayBuffer(this.file); } } ``` 6. 预览数据处理:在上述代码中的注释部分,您可以根据需要处理预览Excel数据。例如,您可以将其展示在页面上的表格中,或使用其他组件进行渲染。 这样,当用户选择一个Excel文件并点击预览按钮时,您就可以在控制台中看到解析后的Excel数据了。您可以根据具体需求,在组件中进一步处理和渲染这些数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值