无翼而飞者,声也。——佚名
今天导出excel
报错
No converter for [class org.springframework.core.io.ByteArrayResource] with preset Content-Type 'application/vnd.ms-excel'
代码如下:
代码语言:javascript复制@PostMapping("exportSettle")
fun export(dto: CommonDTO) {
val writer = ExcelUtil.getWriter()
writer.writeRow(Maps.of("key", "value"), true)
response.addHeader("Content-Disposition", writer.getDisposition("test.xlsx", CharsetUtil.CHARSET_UTF_8))
response.contentType = ContentType.build(writer.contentType, StandardCharsets.UTF_8)
writer.flush(response.outputStream, true)
writer.close()
}
发现没有配置MediaTypes
在MvcConfig
配置一下即可
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
jackson2HttpMessageConverter.setObjectMapper(JacksonUtil.newObjectMapper());
jackson2HttpMessageConverter.setSupportedMediaTypes(Lists.of(MediaType.ALL));
converters.add(jackson2HttpMessageConverter);
}