Java后端调用三方导出接口返回的字符串流能转换成excel吗?

三方提供了导出excel的接口,然后我这边用Java后端调用该接口返回了这种字符串,这是excel文件流数据吗?如何将这种数据转换成excel再给前端导出?

PK 0��W            

_rels/.rels���J1

���Pr���

����(�A-

�p(�t

�f�Ȅ/ґ� ��,H�����



eY�'z��8�R� �幓 �&zXc���E8k\�=�(z�[�ʝ4~��gH���LP<�� =�E_�?ID����PK�c��@ 2 PK 0��W  docProps/app.xmlM��

�0D��ro�z�4� �'{����MHV�盓z���T��E�1e�����Ɇ�ѳ����:�No7jH!bb�Y��V��������T�)$o���0M��9ؗGb�7�pe��*~�R�>��Y�EB���nW������ PK6n�!� � PK 0��W  docProps/core.xmlm��J�0F��!�M�����"ʂ��`E�.$c[l~H�]�޴�[A���3���\�Ԁ>����

�i�had��

�@

(���<���)���<Yȝ�j

�t,f.^���������������A΄

@��`���T���������f�Hr���&[��

V�ג���+��q�U

��}��ȥ

!�'���PK��{�

 � PK 0��W  xl/sharedStrings.xml=�A


回答:

解决了,需求是前端要导出,但因各种原因没法直接调服务2的导出接口,需要服务1中转一下。
服务1接收到前端请求后,调用服务2的controller,服务2正常执行,服务1拿到数据转成byte[],然后写入到outputStream中就可以了

服务2公用导出方法

/**

* 导出Excel

*

* @param response 响应实体

* @param fileName 文件名

* @param exportList 数据列表

* @param clazz 映射类

*/

public static void exportExcel(HttpServletResponse response, String fileName, List exportList, Class<?> clazz) {

ServletOutputStream out = null;

try {

out = response.getOutputStream();

} catch (IOException e) {

e.printStackTrace();

}

ExcelWriterBuilder builder = new ExcelWriterBuilder();

builder.file(out).excelType(ExcelTypeEnum.XLSX).autoCloseStream(true).head(clazz);

ExcelWriter writer = builder.build();

WriteSheet sheet = EasyExcel.writerSheet().build();

writer.write(exportList, sheet);

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");

try {

response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

response.setHeader("Pragma", "public");

response.setHeader("Cache-Control", "no-store");

response.addHeader("Cache-Control", "max-age=0");

out.flush();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

writer.finish();

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

服务1中转接口

    @Resource

private RestTemplate restTemplate;

public void export(@RequestBody Map<String, Object> paramMap,HttpServletResponse response) {

// 构建请求实体信息

HttpEntity<String> requestEntity = RestTemplateHttpUtils.buildHttpEntity(paramMap);

String url = "xxxxxxxxxxx";

ServletOutputStream outputStream = null;

InputStream inputStream = null;

try {

outputStream = response.getOutputStream();

byte[] bytes = restTemplate.postForObject(url, requestEntity, byte[].class);

/// 将Excel流文件字符串转换为字节数组输入流

inputStream = new ByteArrayInputStream(bytes);

byte[] buffer = new byte[1024];

int bytesRead;

// 循环读取输入流的数据并写入输出流

while ((bytesRead = inputStream.read(buffer)) != -1) {

outputStream.write(buffer, 0, bytesRead);

}

outputStream.flush();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

outputStream.close();

inputStream.close();

} catch (IOException e) {

throw new RuntimeException(e);

}

}

}

以上是 Java后端调用三方导出接口返回的字符串流能转换成excel吗? 的全部内容, 来源链接: utcz.com/p/945532.html

回到顶部