欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > java生成excel,uniapp微信小程序接收excel并打开

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

2024/10/25 11:29:29 来源:https://blog.csdn.net/new_public/article/details/140049426  浏览:    关键词: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文档文章,思路基本一致。


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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com