java實現流輸出形式導出數據(使用EasyExcel)並打包爲zip包

使用EasyExcel實現數據導出以流輸出形式並打包爲zip包

pom.xml文件導入easyexcel

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>2.0.5</version>
</dependency>

導出數據的核心代碼

@GetMapping("/export")
public void export(ConsumptionDetailQuery query, HttpServletResponse response) {
 	try {
 		String exportRedisFlag = "exportRedisFlag";
        if(redisTemplate.hasKey(exportRedisFlag)) {
            LOGGER.info("導出正在運行,請稍候重試");
            errorResponse(response, "導出正在運行,請稍候重試", 501);
            return;
        }
 		List<RiskIndexExport> exportList;//用來存儲查詢到的數據
		Sheet sheet1 = new Sheet(1, 0, ExportModel.class);
		sheet1.setSheetName("sheet1");
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
		writer.write(exportList, sheet1);
		writer.finish();
		String fileName = "exportName";
		zipOutput(response, out, fileName);
	catch (IOException e) {
        LOGGER.error("導出時寫入數據到文件出錯" + e.getMessage(), e);
        errorResponse(response, "寫入數據到文件出錯", 500);
     } finally {
            redisTemplate.delete(exportRedisFlag);
        }
}

zipOutput()方法代碼

private void zipOutput(HttpServletResponse response, ByteArrayOutputStream out, String fileName) throws IOException {
        ZipOutputStream zipout = null;
        InputStream inputStream = null;
        try {
            response.setContentType("application/force-download");
            response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment; filename=" + new String((fileName+".zip").getBytes("UTF-8"), "ISO8859-1"));
            inputStream = new ByteArrayInputStream(out.toByteArray());
            zipout = new ZipOutputStream(response.getOutputStream());
            //excel文件寫入zip
            zipout.putNextEntry(new ZipEntry(fileName+".xlsx"));
            int len;
            byte[] buf = new byte[1024];
            while ((len = inputStream.read(buf)) > 0) {
                zipout.write(buf, 0, len);
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            LOGGER.error("zipFiles exception:{}", e.getMessage());
        } finally {
            if (zipout != null) {
                zipout.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

errorResponse方法代碼

private void errorResponse(HttpServletResponse response, String message, Integer code) {
        try {
            Map map = new HashMap();
            map.put("code", code);
            map.put("msg", message);
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getOutputStream().write(JSONObject.toJSONString(map).getBytes());
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

ExportModel 實體類(需要繼承BaseRowModel)

public class ExportModel extends BaseRowModel {

	//註解形式實現標題行
    @ExcelProperty(value = "編號", index = 0)
    private String id;
    
    @ExcelProperty(value = "指標", index = 1)
    private String name;

    @ExcelProperty(value = "年齡", index = 2)
    private String age;
}

有問題大家互相交流

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章