4
回答
java Apache POI excel导出,文件没有出来,求解
华为云实践训练营,热门技术免费实践!>>>   

使用poi方式导出excel,文件没有导出,response一堆乱码

部分代码如下:

response中设置了这样

<无标签>
举报
黄成老湿
发帖于2个月前 4回/138阅
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.IExcelService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


@RequestMapping(value = "/exportexcel")
public void exportExcel(HttpServletResponse response) {

	XSSFWorkbook xss = service.exportExcel();

	response.setContentType("application/octet-stream");
	response.setHeader("name", "excel.xlsx");
	response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
	response.setHeader("Pragma", "public");
	response.setDateHeader("Expires", 0);

	try {
		response.setHeader("Content-disposition", "attachment; filename=\"" + URLEncoder.encode("excel.xlsx", "UTF-8") + "\"");
	} catch (UnsupportedEncodingException e) {
		logger.warn("设置Content-disposition错误!",e);
	}

	try {
		xss.write(response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	} catch (IOException e) {
		logger.warn("导出Excel错误!可能是文件流出现中断!", e);
	}

}

 

代码贴哪了看不见,看着像响应没写对

--- 共有 2 条评论 ---
哈斯卡会不知道你kit里面做了些什么 应该差这个把 OutputStream ouputStream = response.getOutputStream(); workbook.write(ouputStream); 2个月前 回复
黄成老湿之前上传了图片,不知道里面怎么没保存好图片的。现在传了,麻烦看下 2个月前 回复
要么导出来文件放在mongo里,要么在项目里创建模板,利用post请求成功回调时在发起一个url请求访问构建好数据的excel文件
顶部