当前位置: 首页 > news >正文

java生成excel,uniapp微信小程序接收excel并打开

java引包,引的是apache.poi

        <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version></dependency>

写一个测试类,把excel输出到指定路径

    public static void main(String[] args) {// 学生类@Data@AllArgsConstructorclass Student implements Serializable {private static final long serialVersionUID = 1L;private String name;private Integer age;private String sex;}// 将要导出的数据List<Student> list = new ArrayList<>();list.add(new Student("张潇", 23, "男"));list.add(new Student("李小思", 19, "女"));list.add(new Student("某人", 66, "未知"));try (// 创建一个Excel工作簿Workbook workbook = new XSSFWorkbook();// 输出流, 保存到指定路径, 请确保路径存在FileOutputStream fileOutputStream = new FileOutputStream("D:/A_software/templast/student.xlsx");BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);) {// 创建一个工作表sheetSheet sheet = workbook.createSheet("Sheet1");// 设置表头List<String> sheetHeadList = Arrays.asList("姓名", "年龄", "性别");Row sheetHead = sheet.createRow(0);for (int index = 0; index < sheetHeadList.size(); index++) {// 设置每列宽度 大小乘以256,调整13即可sheet.setColumnWidth(index, 13 * 256);sheetHead.createCell(index).setCellValue(sheetHeadList.get(index));}// 设置值for (int index = 0; index < list.size(); index++) {// 创建行,表头是第一行,所以这里 + 1Row row = sheet.createRow(index + 1);Student student = list.get(index);// 行里创建单元格并设置值row.createCell(0).setCellValue(student.getName());row.createCell(1).setCellValue(student.getAge());row.createCell(2).setCellValue(student.getSex());}workbook.write(bufferedOutputStream);} catch (IOException e) {throw new RuntimeException("导出excel失败");}}

执行之后,去打开excel 

 

没毛病,接下来是把excel传给前端,小程序打开,有很多种方式:

  1. 后端保存excel到指定位置,返回文件下载的url给前端,前端根据url下载。
  2. 后端保存excel后,返回文件流,前端调用接口生成excel时,接收返回的文件流即可。
  3. 后端不保存excel,把excel输出成字节数组,再转成base64格式,返回前端base64字符串。

我这里用了第三种,因为我是导出excel给用户,后端不需要存下这个文件。

上面的测试方法改一下就行了

// 输出流, 保存到指定路径, 请确保路径存在
FileOutputStream fileOutputStream = new FileOutputStream("D:/A_software/templast/student.xlsx");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

把上面的输出流换成

ByteArrayOutputStream bufferedOutputStream = new ByteArrayOutputStream();

然后调用workbook.write写入

workbook.write(bufferedOutputStream);
// 写入完了之后,转成字节数组
byte[] bytes = bufferedOutputStream.toByteArray();
// 再转成base64格式
String base64 = Base64.encodeBase64String(bytes);

最后直接返回base64字符串给前端就行了。


前端微信小程序

// 假设接口请求完成,返回了base64字符串
const base64Value = 接口返回
// 文件保存路径
const filePath = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.xlsx`
// 保存excle到手机上
wx.getFileSystemManager().writeFile({filePath: filePath,data: base64Value ,encoding: 'base64',success: () => {// 保存成功后打开,也可以不打开,看你需求setTimeout(() => {uni.openDocument({filePath: filePath,showMenu: true,fileType: 'xlsx',fail: (error) => {// 打开excel失败}})}, 500)},fail: (error) => {// 保存excel失败}
})

完事了,如果是复杂样式的excel,推荐后端引包【freemarker】包生成excel,支持自定义,就是代码有点繁琐,具体看看我的java导出word文档文章,思路基本一致。


码字不易,于你有利,勿忘点赞

http://www.lryc.cn/news/388698.html

相关文章:

  • sam_out 目标检测的应用
  • VLAN原理与配置
  • 使用Spring Boot实现RESTful API
  • 中英双语介绍美国常春藤联盟( Ivy League):八所高校
  • 【计算机网络】常见的网络通信协议
  • java实现http/https请求
  • NC204871 求和
  • git克隆代码warning: could not find UI helper ‘git-credential-manager-ui‘
  • Generator 是怎么样使用的以及各个阶段的变化如何
  • 一文了解Java中 Vector、ArrayList、LinkedList 之间的区别
  • 【论文复现|智能算法改进】基于自适应动态鲸鱼优化算法的路径规划研究
  • 【Win测试】窗口捕获的学习笔记
  • PostgreSQL的学习心得和知识总结(一百四十七)|深入理解PostgreSQL数据库之transaction chain的使用和实现
  • 宝塔linux网站迁移步骤
  • 电路笔记(三极管器件): MOSFETIGBT
  • Docker 镜像导出和导入
  • QueryClientProvider is not defined
  • HTTPS是什么?原理是什么?用公钥加密为什么不能用公钥解密?
  • 系统中非功能性需求的思考
  • 力扣第215题“数组中的第K个最大元素”
  • java.util.function实现原理和Java使用场景【Function、Predicate集合转换过滤,BiConsumer事件处理】
  • 《每天5分钟用Flask搭建一个管理系统》 第6章:数据库集成
  • pandas读取和处理Excel文件的基础应用1
  • electron vite react 创建一个项目
  • 鸿蒙使用 @Builder扩展出来的布局数据更新没法更新UI
  • 湖南省教育网络协会莅临麒麟信安调研教育网络数字化建设及教育信创发展情况
  • 论文阅读_优化RAG系统的检索
  • STC8/32 软硬件I2C通讯方式扫描I2C设备地址
  • Linux——数据流和重定向,制作镜像
  • Windows 11的市场份额越来越大了,推荐你升级!