找回密码
 立即注册

QQ登录

只需一步,快速开始

BND
金牌服务用户   /  发表于:2022-9-17 16:26  /   查看:1897  /  回复:11
1金币
image.png907942809.png 这个通用的方法下载下来的excel里面什么都没有,还请问是否是outputstream有问题

最佳答案

查看完整内容

参考如下代码: 后端: 前端:

11 个回复

倒序浏览
最佳答案
最佳答案
Clark.Pan讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-9-17 16:26:23
来自 4#
参考如下代码:
后端:
  1. @RequestMapping(value = "/exportExcel", method = RequestMethod.POST)
  2.         @ResponseBody
  3.         public ResponseEntity exportExcel() {
  4.                 ByteArrayOutputStream out = new ByteArrayOutputStream();
  5.                 Workbook myworkbook = new Workbook();
  6.                 IWorksheet worksheet = myworkbook.getActiveSheet();
  7.                 worksheet.getRange("A1").setValue("测试");
  8.                 myworkbook.save(out);
  9.                 byte[] contents = out.toByteArray();

  10.         HttpHeaders headers = new HttpHeaders();
  11.         ResponseEntity<byte[]> response = new ResponseEntity<>(contents, headers, HttpStatus.OK);
  12.         return response;
  13.                  
  14.         }
复制代码

前端:
  1. document.getElementById("export").onclick = function(){
  2.                 var xhr = new XMLHttpRequest();
  3.                 xhr.open('POST', "exportExcel", true);
  4.                 xhr.responseType = 'blob';   
  5.                 xhr.onload = function(e) {
  6.                         if (this.status == 200) {
  7.                                 var blob = this.response;
  8.                                 saveAs(blob,"export.xlsx");
  9.                         }       
  10.                 }
  11.                 xhr.send();
  12.             }
复制代码
回复 使用道具 举报
Clark.Pan讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-9-19 13:49:57
2#
收到,调查一下给您回复
回复 使用道具 举报
BND
金牌服务用户   /  发表于:2022-9-19 14:49:56
3#
Clark.Pan 发表于 2022-9-19 13:49
收到,调查一下给您回复

你好,请问有结果了吗? 我这边着急要用
回复 使用道具 举报
BND
金牌服务用户   /  发表于:2022-9-20 09:56:24
5#
Clark.Pan 发表于 2022-9-19 18:09
参考如下代码:
后端:

好的,谢谢,我试试效果
回复 使用道具 举报
Lynn.Dou讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-9-20 15:57:39
6#
有问题继续交流
回复 使用道具 举报
BND
金牌服务用户   /  发表于:2022-9-21 11:25:09
7#

请问pdf又改如何下载导出呢?
回复 使用道具 举报
BND
金牌服务用户   /  发表于:2022-9-21 11:32:10
8#

导出excel,我试了也还是不行,是不是gcexel版本低了。版本:5.1.4。或者是不是需要前端spreadjs的组件才行,我是直接用postman调用接口导出的
回复 使用道具 举报
BND
金牌服务用户   /  发表于:2022-9-21 11:35:05
9#

"Cannot auto-detect encoding, not enough chars\norg.glassfish.json.UnicodeDetectingInputStream.detectEncoding(UnicodeDetectingInputStream.java:131)"网络服务错误。报这个错误
代码:
InputStream download = aliyunOSSHolder.download(excelName);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                Workbook workbook = new Workbook();
                workbook.fromJson(download);
                workbook.save(out);
                byte[] contents = out.toByteArray();
                HttpHeaders headers = new HttpHeaders();
                ResponseEntity<byte[]> responseExcel = new ResponseEntity<>(contents, headers, HttpStatus.OK);
回复 使用道具 举报
Clark.Pan讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-9-21 17:36:29
10#
你这报的是个json的错误,问题我这边判断问题应该出的不是下载的地方,而是其他地方。建议你把这个download的ssjson发给我,我检测一下是你ssjson本身的问题还是其他问题。
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部