赞
踩
- 1,读取数据库,放到Excel中,返回byte数组
-
- import org.apache.commons.io.output.ByteArrayOutputStream;
- import org.apache.dubbo.config.annotation.Reference;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.jeecgframework.poi.excel.ExcelExportUtil;
- import org.jeecgframework.poi.excel.entity.ExportParams;
- import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
- import org.springframework.stereotype.Component;
-
- public byte[] export(Log log, String startTime, String endTime) {
- ExportParams exportParams = new ExportParams(null, null, ExcelType.XSSF);
- Workbook workbook = null;
- //最大100条数据,日志太多,导致超时,而且数据没有什么意义,当然要根据需求来,数据量大可以用队列异步导出,在线等就用多线程导出
- List<Log> logList = logService.getPageList(new Page<>(0, 100), log, startTime, endTime).getRecords();
- workbook = ExcelExportUtil.exportBigExcel(exportParams, Log.class, logList);
- try {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- if (workbook != null) {
- workbook.write(out);
- }
- return out.toByteArray();
- } catch (IOException e) {
- throw new RuntimeException("文件下载失败", e);
- }
- }
2,把读取文件流,放到response中
- import org.apache.commons.io.IOUtils;
-
- public void outExcelFile(HttpServletResponse response, String fileName, byte[] data) {
- try {
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- response.setHeader("Content-Disposition",
- "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO-8859-1"));
- ServletOutputStream outputStream = response.getOutputStream();
- IOUtils.write(data, outputStream);
- } catch (Exception e) {
- throw new RuntimeException("下载文件失败");
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。