excel util;读取模板,动态加载模板

来源:互联网 发布:数据安全工程师 编辑:程序博客网 时间:2024/06/05 17:39

jar

<dependency>    <groupId>org.apache.poi</groupId>    <artifactId>poi-ooxml</artifactId>    <version>3.14</version></dependency>



util

package com.ciicsh.gto.settlementcenter.invoiceservice.util;import java.io.*;import java.math.BigDecimal;import java.net.URLEncoder;import java.text.DecimalFormat;import java.text.NumberFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.apache.poi.hpsf.SummaryInformation;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFClientAnchor;import org.apache.poi.hssf.usermodel.HSSFComment;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFPatriarch;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.DataFormat;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.streaming.SXSSFCell;import org.apache.poi.xssf.streaming.SXSSFRow;import org.apache.poi.xssf.streaming.SXSSFSheet;import org.apache.poi.xssf.streaming.SXSSFWorkbook;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;/** * excel util * * @author linhaihai */public class ExcelUtil {    /**     * 默认日期格式     */    private static final String DEFAULT_DATE_PATTERN = "yyyyMMdd";    private static final int DEFAULT_COLUMN_WIDTH = 17;    private static ExcelUtil excelUtil = new ExcelUtil();    private ExcelUtil() {    }    public static ExcelUtil getExcelUtil() {        return excelUtil;    }    /**     * 创建excel     */    public void createExcel() {    }    /**     * 导出Excel 97(.xls)格式 ,少量数据     *     * @param title       标题行     * @param headMap     属性-列名     * @param jsonArray   数据集     * @param datePattern 日期格式,null则用默认日期格式     * @param colWidth    列宽 默认 至少17个字节     * @param out         输出流     */    public void exportExcel(String title, Map<String, String> headMap, JSONArray jsonArray,                            String datePattern, int colWidth, OutputStream out) {        HSSFWorkbook workbook = null;        try{            if (datePattern == null) {                datePattern = DEFAULT_DATE_PATTERN;            }            // 声明一个工作薄            workbook = new HSSFWorkbook();            workbook.createInformationProperties();            workbook.getDocumentSummaryInformation().setCompany("*****公司");            SummaryInformation si = workbook.getSummaryInformation();            //填加xls文件作者信息            si.setAuthor("JACK");            //填加xls文件创建程序信息            si.setApplicationName("导出程序");            //填加xls文件最后保存者信息            si.setLastAuthor("最后保存者信息");            //填加xls文件作者信息            si.setComments("JACK is a programmer!");            //填加xls文件标题信息            si.setTitle("POI导出Excel");            //填加文件主题信息            si.setSubject("POI导出Excel");            si.setCreateDateTime(new Date());            //表头样式            HSSFCellStyle titleStyle = workbook.createCellStyle();            titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);            HSSFFont titleFont = workbook.createFont();            titleFont.setFontHeightInPoints((short) 20);            titleFont.setBoldweight((short) 700);            titleStyle.setFont(titleFont);            // 列头样式            HSSFCellStyle headerStyle = workbook.createCellStyle();            headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);            headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);            headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);            headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);            headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);            headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);            HSSFFont headerFont = workbook.createFont();            headerFont.setFontHeightInPoints((short) 12);            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);            headerStyle.setFont(headerFont);            // 单元格样式            HSSFCellStyle cellStyle = workbook.createCellStyle();            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);            HSSFFont cellFont = workbook.createFont();            cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);            cellStyle.setFont(cellFont);            // 生成一个(带标题)表格            HSSFSheet sheet = workbook.createSheet();            // 声明一个画图的顶级管理器            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();            // 定义注释的大小和位置,详见文档            HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,                    0, 0, 0, (short) 4, 2, (short) 6, 5));            // 设置注释内容            //comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));            // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.            //comment.setAuthor("JACK");            //设置列宽,至少字节数            int minBytes = colWidth < DEFAULT_COLUMN_WIDTH ? DEFAULT_COLUMN_WIDTH : colWidth;            int[] arrColWidth = new int[headMap.size()];            // 产生表格标题行,以及设置列宽            String[] properties = new String[headMap.size()];            String[] headers = new String[headMap.size()];            int ii = 0;            for (String key : headMap.keySet()) {                properties[ii] = key;                headers[ii] = key;                int bytes = key.getBytes().length;                arrColWidth[ii] = bytes < minBytes ? minBytes : bytes;                sheet.setColumnWidth(ii, arrColWidth[ii] * 256);                ii++;            }            // 遍历集合数据,产生数据行            int rowIndex = 0;            for (Object obj : jsonArray) {                if (rowIndex == 65535 || rowIndex == 0) {                    //如果数据超过了,则在第二页显示                    if (rowIndex != 0) {                        sheet = workbook.createSheet();                    }                    //表头 rowIndex=0                    HSSFRow titleRow = sheet.createRow(0);                    titleRow.createCell(0).setCellValue(title);                    titleRow.getCell(0).setCellStyle(titleStyle);                    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1));                    //列头 rowIndex =1                    HSSFRow headerRow = sheet.createRow(1);                    for (int i = 0; i < headers.length; i++) {                        headerRow.createCell(i).setCellValue(headers[i]);                        headerRow.getCell(i).setCellStyle(headerStyle);                    }                    //数据内容从 rowIndex=2开始                    rowIndex = 2;                }                JSONObject jo = (JSONObject) JSONObject.toJSON(obj);                HSSFRow dataRow = sheet.createRow(rowIndex);                for (int i = 0; i < properties.length; i++) {                    HSSFCell newCell = dataRow.createCell(i);                    Object o = jo.get(properties[i]);                    String cellValue = "";                    if (o == null) {                        cellValue = "";                    } else if (o instanceof Date) {                        cellValue = new SimpleDateFormat(datePattern).format(o);                    } else {                        cellValue = o.toString();                    }                    newCell.setCellValue(cellValue);                    newCell.setCellStyle(cellStyle);                }                rowIndex++;            }            // 自动调整宽度            /*for (int i = 0; i < headers.length; i++) {                sheet.autoSizeColumn(i);            }*/            workbook.write(out);        }catch (Exception e){            e.printStackTrace();        }finally {            try{                workbook.close();            }catch (Exception e){                e.printStackTrace();            }        }    }    /**     * 导出Excel 2007 OOXML (.xlsx)格式     *     * @param title       标题行     * @param headMap     属性-列头     * @param jsonArray   数据集     * @param datePattern 日期格式,传null值则默认 年月日     * @param colWidth    列宽 默认 至少17个字节     * @param out         输出流     */    public void exportExcelX(String title, Map<String, String> headMap, JSONArray jsonArray,                             String datePattern, int colWidth, OutputStream out) {        SXSSFWorkbook workbook = null;        try{            if (datePattern == null) {                datePattern = DEFAULT_DATE_PATTERN;            }            // 声明一个工作薄,//缓存            workbook = new SXSSFWorkbook(1000);            workbook.setCompressTempFiles(true);            //表头样式            CellStyle titleStyle = workbook.createCellStyle();            titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);            Font titleFont = workbook.createFont();            titleFont.setFontHeightInPoints((short) 20);            titleFont.setBoldweight((short) 700);            titleStyle.setFont(titleFont);            // 列头样式            CellStyle headerStyle = workbook.createCellStyle();            headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);            headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);            headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);            headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);            headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);            headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);            Font headerFont = workbook.createFont();            headerFont.setFontHeightInPoints((short) 12);            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);            headerStyle.setFont(headerFont);            // 单元格样式            CellStyle cellStyle = workbook.createCellStyle();            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);            Font cellFont = workbook.createFont();            cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);            cellStyle.setFont(cellFont);            // 生成一个(带标题)表格            SXSSFSheet sheet = workbook.createSheet();            //设置列宽//至少字节数            int minBytes = colWidth < DEFAULT_COLUMN_WIDTH ? DEFAULT_COLUMN_WIDTH : colWidth;            int[] arrColWidth = new int[headMap.size()];            // 产生表格标题行,以及设置列宽            String[] properties = new String[headMap.size()];            String[] headers = new String[headMap.size()];            int ii = 0;            for (String key : headMap.keySet()) {                properties[ii] = key;                headers[ii] = headMap.get(key);                int bytes = key.getBytes().length;                arrColWidth[ii] = bytes < minBytes ? minBytes : bytes;                sheet.setColumnWidth(ii, arrColWidth[ii] * 256);                ii++;            }            // 遍历集合数据,产生数据行            int rowIndex = 0;            for (Object obj : jsonArray) {                if (rowIndex == 65535 || rowIndex == 0) {                    //如果数据超过了,则在第二页显示                    if (rowIndex != 0) {                        sheet = workbook.createSheet();                    }                    //表头 rowIndex=0                    SXSSFRow titleRow = sheet.createRow(0);                    titleRow.createCell(0).setCellValue(title);                    titleRow.getCell(0).setCellStyle(titleStyle);                    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1));                    //列头 rowIndex =1                    SXSSFRow headerRow = sheet.createRow(1);                    for (int i = 0; i < headers.length; i++) {                        headerRow.createCell(i).setCellValue(headers[i]);                        headerRow.getCell(i).setCellStyle(headerStyle);                    }                    //数据内容从 rowIndex=2开始                    rowIndex = 2;                }                JSONObject jo = (JSONObject) JSONObject.toJSON(obj);                SXSSFRow dataRow = sheet.createRow(rowIndex);                for (int i = 0; i < properties.length; i++) {                    SXSSFCell newCell = dataRow.createCell(i);                    Object o = jo.get(properties[i]);                    String cellValue = "";                    if (o == null) {                        cellValue = "";                    } else if (o instanceof Date) {                        cellValue = new SimpleDateFormat(datePattern).format(o);                    } else if (o instanceof Float || o instanceof Double) {                        cellValue = new BigDecimal(o.toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();                    } else {                        cellValue = o.toString();                    }                    newCell.setCellValue(cellValue);                    newCell.setCellStyle(cellStyle);                }                rowIndex++;            }            // 自动调整宽度            /*for (int i = 0; i < headers.length; i++) {                sheet.autoSizeColumn(i);            }*/            workbook.write(out);        }catch (Exception e){            e.printStackTrace();        }finally {            try{                workbook.close();            }catch (Exception e){                e.printStackTrace();            }            try{                workbook.dispose();            }catch (Exception e){                e.printStackTrace();            }        }    }    /**     * 导出excel web下载     *     * @param title     * @param headMap     * @param ja     * @param response     */    public void downloadExcelFile(String title, Map<String, String> headMap, JSONArray ja, HttpServletResponse response) {        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        ServletOutputStream outputStream = null;        try {            ByteArrayOutputStream os = new ByteArrayOutputStream();            exportExcelX(title, headMap, ja, null, 0, os);            byte[] content = os.toByteArray();            InputStream is = new ByteArrayInputStream(content);            // 设置response参数,可以打开下载页面            response.reset();            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");            response.setHeader("Content-Disposition", "attachment;filename=" + new String((title + ".xlsx").getBytes(), "iso-8859-1"));            response.setContentLength(content.length);            outputStream = response.getOutputStream();            bis = new BufferedInputStream(is);            bos = new BufferedOutputStream(outputStream);            byte[] buff = new byte[8192];            int bytesRead;            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {                bos.write(buff, 0, bytesRead);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                bis.close();            } catch (Exception e) {                e.printStackTrace();            }            try {                bos.close();            } catch (Exception e) {                e.printStackTrace();            }            try {                outputStream.flush();            } catch (Exception e) {                e.printStackTrace();            }            try {                outputStream.close();            } catch (Exception e) {                e.printStackTrace();            }        }    }        /**     * (2007 xlsx后缀 导出)     * @param fromFilePath 模板文件路径     * @param toFilePath 保存文件路径     * @param dataMap 填充数据     */    public void createXLSX(String fromFilePath,String toFilePath,Map<String,Object> dataMap) {        FileOutputStream out = null;        try{            File file = new File(toFilePath);            if(file.exists()){                file.delete();            }            //excel模板路径            File fi = new File(fromFilePath);            InputStream in = new FileInputStream(fi);            //读取excel模板            XSSFWorkbook wb = new XSSFWorkbook(in);            //读取了模板内所有sheet内容            XSSFSheet sheet = wb.getSheetAt(0);            //如果这行没有了,整个公式都不会有自动计算的效果的            sheet.setForceFormulaRecalculation(true);            //在相应的单元格进行赋值            //11行 第6            for(int i = sheet.getFirstRowNum();i <= sheet.getLastRowNum();i++){                XSSFRow row = sheet.getRow(i);                for(int j = 0 ; j < row.getLastCellNum(); j ++){                    XSSFCell cell = row.getCell(j);                    if(cell != null){                        cellDataAssemble(dataMap,cell,wb);                    }                }            }            //修改模板内容导出新模板            out = new FileOutputStream(toFilePath);            wb.write(out);        }catch (Exception e){            e.printStackTrace();        }finally {            try{                out.close();            }catch (Exception e){                e.printStackTrace();            }        }    }    /**     * 填充数据到excel模板,并web下载     * @param fromFilePath excel模板     * @param dataMap 填充数据     * @param fileName 下载保存文件名     * @param response response     */    public void downloadXLSXFile(String fromFilePath,                                 Map<String,Object> dataMap,                                 String fileName,                                 HttpServletResponse response) {        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        ServletOutputStream outputStream = null;        try {            fileName = URLEncoder.encode(fileName, "UTF-8");            ByteArrayOutputStream os = new ByteArrayOutputStream();            createXLSX(fromFilePath,os,dataMap);            byte[] content = os.toByteArray();            InputStream is = new ByteArrayInputStream(content);            response.reset();            response.setContentType("application/octet-stream;charset=utf-8");            response.setHeader("Content-Disposition","attachment;filename=" + fileName);            response.setContentLength(content.length);            outputStream = response.getOutputStream();            bis = new BufferedInputStream(is);            bos = new BufferedOutputStream(outputStream);            byte[] buff = new byte[8192];            int bytesRead;            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {                bos.write(buff, 0, bytesRead);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                bis.close();            } catch (Exception e) {                e.printStackTrace();            }            try {                bos.close();            } catch (Exception e) {                e.printStackTrace();            }            try {                outputStream.flush();            } catch (Exception e) {                e.printStackTrace();            }            try {                outputStream.close();            } catch (Exception e) {                e.printStackTrace();            }        }    }    /**     * (2007 xlsx excel 组装)     * @param fromFilePath 模板文件路径     * @param out 加载后导出文件路径     * @param dataMap 填充数据     */    public void createXLSX(String fromFilePath,OutputStream out,Map<String,Object> dataMap) {        try{            //excel模板路径            File fi = new File(fromFilePath);            InputStream in = new FileInputStream(fi);            //读取excel模板            XSSFWorkbook wb = new XSSFWorkbook(in);            //读取了模板内所有sheet内容            XSSFSheet sheet = wb.getSheetAt(0);            //如果这行没有了,整个公式都不会有自动计算的效果的            sheet.setForceFormulaRecalculation(true);            //在相应的单元格进行赋值            //11行 第6            for(int i = sheet.getFirstRowNum();i <= sheet.getLastRowNum();i++){                XSSFRow row = sheet.getRow(i);                for(int j = 0 ; j < row.getLastCellNum(); j ++){                    XSSFCell cell = row.getCell(j);                    if(cell != null){                        cellDataAssemble(dataMap,cell,wb);                    }                }            }            //修改模板内容导出新模板            wb.write(out);        }catch (Exception e){            e.printStackTrace();        }finally {        }    }    /**     * 单元格内容组装     * @param dataMap     * @param cell     * @return     */    private void cellDataAssemble(Map<String,Object> dataMap,XSSFCell cell,XSSFWorkbook wb){        String cellStr = getStringXLSXCellValue(cell);        int m = cellStr.indexOf("{");        int n = cellStr.indexOf("}");        if(m != -1 && n != -1){            String key = cellStr.substring(m,n)                    .replace("{","")                    .replace("}","")                    .replace(" ","");            Object mapData = dataMap.get(key);            if(mapData != null){                if(m == 0 && n + 1 == cellStr.length()){                    setCellValue(cell,mapData,wb);                }else if(cellStr.length() > n+1){                    cell.setCellValue(cellStr.substring(0,m)+mapData.toString()+cellStr.substring(n+1));                }else{                    cell.setCellValue(cellStr.substring(0,m)+mapData.toString());                }            }        }{        }    }    /**     * 获取单元格数据内容为字符串类型的数据     * @param cell 单元格     * @return 内容     */    private String getStringXLSCellValue(HSSFCell cell) {        String strCell = "";        if (cell == null) {            return "";        }        //将数值型参数转成文本格式,该算法不能保证1.00这种类型数值的精确度        DecimalFormat df = (DecimalFormat) NumberFormat.getPercentInstance();        StringBuffer sb = new StringBuffer();        sb.append("0");        df.applyPattern(sb.toString());        switch (cell.getCellType()) {            case HSSFCell.CELL_TYPE_STRING:                strCell = cell.getStringCellValue();                break;            case HSSFCell.CELL_TYPE_NUMERIC:                double value = cell.getNumericCellValue();                while(Double.parseDouble(df.format(value))!=value){                    if("0".equals(sb.toString())){                        sb.append(".0");                    }else{                        sb.append("0");                    }                    df.applyPattern(sb.toString());                }                strCell = df.format(value);                break;            case HSSFCell.CELL_TYPE_BOOLEAN:                strCell = String.valueOf(cell.getBooleanCellValue());                break;            case HSSFCell.CELL_TYPE_BLANK:                strCell = "";                break;            default:                strCell = "";                break;        }        if (strCell == null || "".equals(strCell)) {            return "";        }        return strCell;    }    /**     * 获取单元格数据内容为字符串类型的数据     * @param cell 单元格     * @return 内容     */    private String getStringXLSXCellValue(XSSFCell cell) {        String strCell = "";        if (cell == null) {            return "";        }        //将数值型参数转成文本格式,该算法不能保证1.00这种类型数值的精确度        DecimalFormat df = (DecimalFormat) NumberFormat.getPercentInstance();        StringBuffer sb = new StringBuffer();        sb.append("0");        df.applyPattern(sb.toString());        switch (cell.getCellType()) {            case XSSFCell.CELL_TYPE_STRING:                strCell = cell.getStringCellValue();                break;            case XSSFCell.CELL_TYPE_NUMERIC:                double value = cell.getNumericCellValue();                while(Double.parseDouble(df.format(value))!=value){                    if("0".equals(sb.toString())){                        sb.append(".0");                    }else{                        sb.append("0");                    }                    df.applyPattern(sb.toString());                }                strCell = df.format(value);                break;            case XSSFCell.CELL_TYPE_BOOLEAN:                strCell = String.valueOf(cell.getBooleanCellValue());                break;            case XSSFCell.CELL_TYPE_BLANK:                strCell = "";                break;            default:                strCell = "";                break;        }        if (strCell == null || "".equals(strCell)) {            return "";        }        return strCell;    }    /**     * 给单元格Cell设置内容     * @param cell 单元格     * @param obj      * @param wb excel对象     */    private void setCellValue(XSSFCell cell,Object obj,XSSFWorkbook wb) {        CellStyle cellStyle = wb.createCellStyle();        DataFormat formatDate = wb.createDataFormat();        cellStyle.setDataFormat(formatDate.getFormat("yyyy/mm/dd hh:mm:ss"));        if(obj instanceof Double){            cell.setCellValue((Double)obj);        }else if(obj instanceof Date){            cell.setCellStyle(cellStyle);            cell.setCellValue((Date)obj);        }else{            cell.setCellValue(obj.toString());        }    }    public static void main(String[] args) throws IOException {        //excle 2003        //ExcelUtil.getExcelUtil().createXLS();        //excle 2007        //{userName}   {userAge}  {userBirthday}        Map<String,Object> dataMap = new HashMap<>();        dataMap.put("userName","海海");        dataMap.put("userAge","20");        dataMap.put("bankName","交通银行");        dataMap.put("bankNo","6111111111119");        dataMap.put("yongtu","话费");        dataMap.put("jine","100.00");        dataMap.put("zongjine","一百元整");        dataMap.put("zongjineDouble","100.00");        dataMap.put("userBirthday",new Date());        ExcelUtil.getExcelUtil().createXLSX("D:/财务部付款凭单.xlsx","D:/to.xlsx",dataMap);    }}
controller
/** * 下载excel * http://localhost:8005/invoicetitle/downloadExcel */@RequestMapping(value = "/downloadExcel", method = {RequestMethod.POST, RequestMethod.GET})public Object downloadExcel(HttpServletRequest request, HttpServletResponse response)throws Exception{    System.out.println("in downloadExcel111222");    Map<String,Object> dataMap = new HashMap<>();    dataMap.put("userName","海海");    dataMap.put("userAge","20");    dataMap.put("bankName","交通银行");    dataMap.put("bankNo","6111111111119");    dataMap.put("yongtu","话费");    dataMap.put("jine","100.00");    dataMap.put("zongjine","一百元整");    dataMap.put("zongjineDouble","100.00");    dataMap.put("userBirthday",new Date());    ExcelUtil.getExcelUtil().downloadXLSXFile(            "D:/财务部付款凭单.xlsx",            dataMap,            "财务部付款凭单.xlsx",response);    return null;}

excel模板中占位:
{userName}
{bankName}
{bankNo}