导出Excel文件是业务中经常遇到的需求,以下是经常遇到的一些问题:
1,导出中文文件名乱码
String filename = "sheet1";response.setCharacterEncoding("UTF-8");response.setContentType("application/octet-stream");response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
2,导出多个sheet怎么实现(map)
private Map<String, Object> workbook = new LinkedHashMap();
3,导出excel的表头是多级的怎么实现
参考这个可以实现:https://blog.csdn.net/qq_28430851/article/details/80496125,依赖的jar包版本升级,需要做些许改动。
4,导出的sheet命名不规范报错
public static String excelSheetName(String str) { if (StringUtils.isBlank(str)){ str = String.valueOf(System.currentTimeMillis()); }else { String regex = "(\\*|/|:|\\\\|\\[|\\]|\\?)"; str = str.replaceAll(regex, "-"); if (str.length() > EXCEL_SHEET_NAME_LENGTH) { str = str.substring(0, EXCEL_SHEET_NAME_LENGTH); } } return str;}