上传文件-读取excel文件数据

1.导入easypoi包
2.`
/**

  • @param filePath Excel文件路径

  • @param sheetIndex 对应的表单,从0开始,0代表第一个表单

  • @param clazz 对应封装的数据实例对象

  • @return 返回数据集合

*/

public static List readExcel(String filePath, int sheetIndex, Class clazz) {

// 定义输入流

FileInputStream fis = null;

List datas = null;

try {

// 创建输入流对象

fis = new FileInputStream(filePath);

// 创建一个easypoi使用的配置类

ImportParams params = new ImportParams();

// 设置表格坐标

params.setStartSheetIndex(sheetIndex);

// 校验Excel文件,去掉空行

params.setNeedVerify(true);

// 读取数据

datas = ExcelImportUtil.importExcel(fis, clazz, params);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (fis != null) {

fis.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return datas;

}

}

`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 提供了一个上传组件 `Upload`,通过这个组件可以上传文件到服务器,然后在服务器端读取 Excel 文件里的值。 以下是一个简单的示例: ```vue <template> <div> <a-upload :before-upload="beforeUpload" :on-success="onSuccess" :show-upload-list="false" > <a-button> <a-icon type="upload"></a-icon> 点击上传 </a-button> </a-upload> <div v-if="excelData"> <table> <thead> <tr> <th v-for="header in excelData.headers" :key="header">{{ header }}</th> </tr> </thead> <tbody> <tr v-for="(row, index) in excelData.rows" :key="index"> <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td> </tr> </tbody> </table> </div> </div> </template> <script> import XLSX from 'xlsx'; export default { data() { return { excelData: null, }; }, methods: { beforeUpload(file) { const isExcel = /xlsx|xls/.test(file.type); if (!isExcel) { this.$message.error('只能上传 Excel 文件!'); } return isExcel; }, onSuccess(response) { const reader = new FileReader(); reader.onload = (event) => { const data = event.target.result; const workbook = XLSX.read(data, { type: 'binary' }); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; const excelData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); this.excelData = { headers: excelData[0], rows: excelData.slice(1) }; }; reader.readAsBinaryString(response.file); }, }, }; </script> ``` 在这个例子中,我们使用 `XLSX` 库来解析 Excel 文件。在上传成功后,我们使用 `FileReader` 对象来读取上传文件,并将其转换为二进制字符串, 然后使用 `XLSX` 库将其转换为 JSON 数据。最后,我们将这些数据存储在 `excelData` 变量中,以供渲染。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值