zoukankan      html  css  js  c++  java
  • 使用POI 导出Excel 动态合并单元格

    一、存储单元格内容  行标  列标的实体类   PoiModel

    package com.zsplat.qrcode.exportexcel.model;
    
    
    /**
     * 
     * @ClassName:PoiModel
     * @Description:TODO(这里用一句话描述这个类的作用)
     * @author: ZHOUPAN
     * @date 2019年1月25日 下午3:10:30
     *
     * @Copyright: 2018 www.zsplat.com Inc. All rights reserved.
     */
    public class PoiModel {
        
        //内容
        private String content;
        //上一行同一位置内容
        private String oldContent;
        //行标
        private int rowIndex;
        //列标
        private int cellIndex;
     
        public String getOldContent() {
            return oldContent;
        }
     
        public void setOldContent(String oldContent) {
            this.oldContent = oldContent;
        }
     
        public String getContent() {
            return content;
        }
     
        public void setContent(String content) {
            this.content = content;
        }
     
        public int getRowIndex() {
            return rowIndex;
        }
     
        public void setRowIndex(int rowIndex) {
            this.rowIndex = rowIndex;
        }
     
        public int getCellIndex() {
            return cellIndex;
        }
     
        public void setCellIndex(int cellIndex) {
            this.cellIndex = cellIndex;
        }
    }

    二、导出Excel工具类

    package com.zsplat.qrcode.commons.utils;
    
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Font;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.ss.util.CellUtil;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import com.google.common.collect.Lists;
    import com.zsplat.qrcode.commons.utils.lang.DateUtils;
    import com.zsplat.qrcode.exportexcel.model.PoiModel;
    
    /**
     * 导出  动态合并单元格
     * @ClassName:ExportExcelByPoiUtil
     * @Description:TODO(这里用一句话描述这个类的作用)
     * @author: ZHOUPAN
     * @date 2019年1月25日 下午3:22:27
     *
     * @Copyright: 2018 www.zsplat.com Inc. All rights reserved.
     */
    public class ExportExcelByPoiUtil {
        /**
         * 
         * @Title: createExcel 
         * @Description: TODO(这里用一句话描述这个方法的作用) 
         * @author: ZHOUPAN
         * @date: 2019年1月30日 下午4:37:01
         * @param @param request
         * @param @param response
         * @param @param title 标题数组
         * @param @param titleHead  Excel标题
         * @param @param widthAttr  单元格宽度
         * @param @param maps  数据
         * @param @param mergeIndex  要合并的列   数组
         * @param @return    设定文件 
         * @return String    返回类型 
         * @throws
         */
        @SuppressWarnings("rawtypes")
        public static String createExcel(HttpServletRequest request, HttpServletResponse response,String[] title,String titleHead ,int[] widthAttr,Map<String/*sheet名*/, List<Map<String/*对应title的值*/, String>>> maps, int[] mergeIndex){
            if (title.length==0){
                return null;
            }
            /*初始化excel模板*/
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = null;
            int n = 0;
            /*循环sheet页*/
            for(Map.Entry<String, List<Map<String/*对应title的值*/, String>>> entry : maps.entrySet()){
                /*实例化sheet对象并且设置sheet名称,book对象*/
                try {
                    sheet = workbook.createSheet();
                    workbook.setSheetName(n, entry.getKey());
                    workbook.setSelectedTab(0);
                }catch (Exception e){
                    e.printStackTrace();
                }
                // 设置样式 头 cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
                // 水平方向的对齐方式
                CellStyle cellStyle_head = style(0, workbook);
                // 导出时间
                CellStyle cellStyle_export = style(3, workbook);
                // 标题
                CellStyle cellStyle_title = style(1, workbook);
                // 正文
                CellStyle cellStyle = style(2, workbook);
                // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
                 CellRangeAddress c1 = new CellRangeAddress(0, 0, 0, title.length-1);
                 sheet.addMergedRegion(c1);
                 CellRangeAddress c2 = new CellRangeAddress(1, 1, 0, title.length-1);
                 sheet.addMergedRegion(c2);
                 // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
                Row row0 = sheet.createRow(0);
                // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
                Cell cell1 = row0.createCell(0);
                // 设置单元格内容 标题
                cell1.setCellValue("上海明华电力———" + titleHead + "一览表");
                cell1.setCellStyle(cellStyle_head);
                // 设置合并单元格边框
                setRegionStyle(sheet, c1, cellStyle_head);
                setRegionStyle(sheet, c2, cellStyle_export);
                // 设置列宽
                for (int i = 0; i < widthAttr.length; i++) {
                    sheet.setColumnWidth((short) i, (short) widthAttr[i] * 200);
                }
                // 在sheet里创建第二行
                Row row1 = sheet.createRow(1);
                // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
                Cell cell2 = row1.createCell(0);
                // 设置单元格内容 标题
                cell2.setCellValue("导出时间:" + DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss"));
                cell2.setCellStyle(cellStyle_export);
                /*初始化标题,填值标题行(第一行)*/
                Row row2 = sheet.createRow(2);
                for(int i = 0; i<title.length; i++){
                    /*创建单元格,指定类型*/
                    Cell cell_1 = row2.createCell(i, Cell.CELL_TYPE_STRING);
                    //设置标题的值
                    cell_1.setCellValue(title[i]);
                    //设置标题样式
                    cell_1.setCellStyle(cellStyle_title);
                }
                /*得到当前sheet下的数据集合*/
                List<Map<String/*对应title的值*/, String>> list = entry.getValue();
                /*遍历该数据集合*/
                List<PoiModel> poiModels = Lists.newArrayList();
                if(null!=workbook){
                    Iterator iterator = list.iterator();
    //                int index = 1;/*这里1是从excel的第二行开始,第一行已经塞入标题了*/
                    int index = 3;/*这里3是从excel的第四行开始,前面几行已经塞入标题了*/
                    while (iterator.hasNext()){
                        Row row = sheet.createRow(index);
                        /*取得当前这行的map,该map中以key,value的形式存着这一行值*/
                        @SuppressWarnings("unchecked")
                        Map<String, String> map = (Map<String, String>)iterator.next();
                        /*循环列数,给当前行塞值*/
                        for(int i = 0; i<title.length; i++){
                            String old = "";
                            /*old存的是上一行统一位置的单元的值,第一行是最上一行了,所以从第二行开始记*/
                            if(index > 3){
                                old = poiModels.get(i)==null ? "":poiModels.get(i).getContent();
                            }
                            /*循环需要合并的列*/
                            for(int j = 0; j < mergeIndex.length; j++){
                                /* 因为标题行前还有2行  所以index从3开始    也就是第四行*/
                                if(index == 3){
                                    /*记录第一行的开始行和开始列*/
                                    PoiModel poiModel = new PoiModel();
                                    poiModel.setOldContent(map.get(title[i]));
                                    poiModel.setContent(map.get(title[i]));
                                    poiModel.setRowIndex(3);
                                    poiModel.setCellIndex(i);
                                    poiModels.add(poiModel);
                                    break;
                                }else if(i > 0 && mergeIndex[j] == i){
                                    /*这边i>0也是因为第一列已经是最前一列了,只能从第二列开始*/
                                    /*当前同一列的内容与上一行同一列不同时,把那以上的合并, 或者在当前元素一样的情况下,前一列的元素并不一样,这种情况也合并*/
                                    /*如果不需要考虑当前行与上一行内容相同,但是它们的前一列内容不一样则不合并的情况,把下面条件中||poiModels.get(i).getContent().equals(map.get(title[i])) && !poiModels.get(i - 1).getOldContent().equals(map.get(title[i-1]))去掉就行*/
                                    if(!poiModels.get(i).getContent().equals(map.get(title[i])) || poiModels.get(i).getContent().equals(map.get(title[i])) && !poiModels.get(i - 1).getOldContent().equals(map.get(title[i-1]))){
                                        /*当前行的当前列与上一行的当前列的内容不一致时,则把当前行以上的合并*/
                                        CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*从第二行开始*/, index - 1/*到第几行*/, poiModels.get(i).getCellIndex()/*从某一列开始*/, poiModels.get(i).getCellIndex()/*到第几列*/);
                                        //在sheet里增加合并单元格
                                        sheet.addMergedRegion(cra);
                                        /*重新记录该列的内容为当前内容,行标记改为当前行标记,列标记则为当前列*/
                                        poiModels.get(i).setContent(map.get(title[i]));
                                        poiModels.get(i).setRowIndex(index);
                                        poiModels.get(i).setCellIndex(i);
                                    }
                                }
                                /*处理第一列的情况*/
                                if(mergeIndex[j] == i && i == 0 && !poiModels.get(i).getContent().equals(map.get(title[i]))){
                                    /*当前行的当前列与上一行的当前列的内容不一致时,则把当前行以上的合并*/
                                    CellRangeAddress cra=new CellRangeAddress(poiModels.get(i).getRowIndex()/*从第二行开始*/, index - 1/*到第几行*/, poiModels.get(i).getCellIndex()/*从某一列开始*/, poiModels.get(i).getCellIndex()/*到第几列*/);
                                    //在sheet里增加合并单元格
                                    sheet.addMergedRegion(cra);
                                    /*重新记录该列的内容为当前内容,行标记改为当前行标记*/
                                    poiModels.get(i).setContent(map.get(title[i]));
                                    poiModels.get(i).setRowIndex(index);
                                    poiModels.get(i).setCellIndex(i);
                                }
     
                                /*最后一行没有后续的行与之比较,所有当到最后一行时则直接合并对应列的相同内容  加2是因为标题行前面还有2行*/
                                if(mergeIndex[j] == i && index == list.size()+2){
                                    CellRangeAddress cra = new CellRangeAddress(poiModels.get(i).getRowIndex()/*从第二行开始*/, index/*到第几行*/, poiModels.get(i).getCellIndex()/*从某一列开始*/, poiModels.get(i).getCellIndex()/*到第几列*/);
                                    //在sheet里增加合并单元格
                                    sheet.addMergedRegion(cra);
                                }
                            }
                            Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
                            cell.setCellValue(map.get(title[i]));
                            cell.setCellStyle(cellStyle);
                            /*在每一个单元格处理完成后,把这个单元格内容设置为old内容*/
                            poiModels.get(i).setOldContent(old);
                        }
                        index++;
                    }
                }
                n++;
            }
            
            OutputStream out = null;
            String localPath = null;
            try {
                Calendar calendar1 = Calendar.getInstance();
                String cal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar1.getTime());
                out = response.getOutputStream();
                response.reset();//清空输出流
                response.setHeader("Content-disposition", "attachment;filename=" + new String(titleHead.getBytes("gbk"), "iso8859-1") + cal + ".xlsx");// 设定输出文件头
                response.setContentType("application/vnd.ms-excel;charset=GBK");// 定义输出类型
                workbook.write(out);
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                try {
                    out.flush();
                    out.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            return localPath;
        }
        
        /**
         * 
         * @Title: style 
         * @Description: TODO(样式) 
         * @author: GMY
         * @date: 2018年5月9日 下午5:16:48
         * @param @return    设定文件  index 0:头 1:标题 2:正文
         * @return HSSFCellStyle    返回类型  
         * @throws
         */
        public static CellStyle style(int index, Workbook workbook) {
            CellStyle cellStyle = null;
            if (index == 0) {
                // 设置头部样式
                cellStyle = workbook.createCellStyle();
                // 设置字体大小 位置
                cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                // 生成一个字体
                Font font = workbook.createFont();
                //设置字体
                font.setFontName("微软雅黑");
                //字体颜色
                font.setColor(HSSFColor.BLACK.index);// HSSFColor.VIOLET.index
                font.setFontHeightInPoints((short) 12);
                font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体增粗
                cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
                cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);//背景白色
                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.setFont(font);
            }
            //标题
            if (index == 1) {
                cellStyle = workbook.createCellStyle();
                // 设置字体大小 位置
                cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                // 生成一个字体
                Font font_title = workbook.createFont();
                //设置字体
                font_title.setFontName("微软雅黑");
                font_title.setColor(HSSFColor.BLACK.index);// HSSFColor.VIOLET.index
                //字体颜色
                font_title.setFontHeightInPoints((short) 10);
                font_title.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体增粗
                cellStyle.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
                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);// 上下居中
                cellStyle.setFont(font_title);
            }
            //正文
            if (index == 2) {
                // 设置样式
                cellStyle = workbook.createCellStyle();
                cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                // 生成一个字体
                Font font_title = workbook.createFont();
                //设置字体
                font_title.setFontName("微软雅黑");
    //            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
                cellStyle.setWrapText(true); // 自动换行
                cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);//背景白色
                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);// 上下居中
            }
            //时间
            if (index == 3) {
                // 设置样式
                cellStyle = workbook.createCellStyle();
                // 居中
                cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
                // 生成一个字体
                Font font_title = workbook.createFont();
                //设置字体
                font_title.setFontName("微软雅黑");
                font_title.setColor(HSSFColor.BLACK.index);// HSSFColor.VIOLET.index
                                                            // //字体颜色
                font_title.setFontHeightInPoints((short) 10);
                font_title.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
                cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
                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.setFont(font_title);
    
            }
            if (index == 4) {
                // 设置样式
                cellStyle = workbook.createCellStyle();
                // 居中
    //            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
                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);// 上下居中
            }
            return cellStyle;
        }
        /**
         * 
         * @Title: setRegionStyle 
         * @Description: TODO(合并单元格后边框不显示问题) 
         * @author: GMY
         * @date: 2018年5月10日 上午10:46:00
         * @param @param sheet
         * @param @param region
         * @param @param cs    设定文件 
         * @return void    返回类型 
         * @throws
         */
        public static void setRegionStyle(Sheet sheet, CellRangeAddress region, CellStyle cs) {
            for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
                Row row = CellUtil.getRow(i, sheet);
                for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
                    Cell cell = CellUtil.getCell(row, (short) j);
                    cell.setCellStyle(cs);
                }
            }
        }
    }

    三、实践中调用  

    (1)controller 中导出的方法

    @ResponseBody
        @RequestMapping(value = "/exportSafeConfess", method = RequestMethod.POST, produces = {
                "application/json;charset=UTF-8" })
        public void exportSafeConfess(HttpServletRequest request, HttpServletResponse response) {
            // 获取页面时间参数
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            Map<String, Object> paramsValue = new HashMap<String, Object>();
            paramsValue.put("startTime", startTime);
            paramsValue.put("endTime", endTime);
            paramsValue.put("pageIndex", null);
            paramsValue.put("pageSize", null);
            List<SafeConfess> resultList = safeConfessService.querySafeConfessAndContent(paramsValue);
            String titleAttr[] = { "安全交底名称", "部门名称", "项目名称", "项目编号", "项目负责人", "工作地点", "工作编号", "公司联系人", "公司联系方式", "厂方联系人",
                    "厂方联系方式", "现场确认人员 ", "执行人", "执行时间", "现场试验其它危险源辨识", "针对上述危险源制定的安全措施", "现场试验其它环境因素识别", "针对上述环境因素的控制措施",
                    "措施确认检查人", "措施确认检查时间", "控制措施内容", "控制措施结果", "技术措施内容", "技术措施结果", "试验前检查人", "试验前检查时间", "试验前检查内容",
                    "试验前检查结果", "试验中检查人", "试验中检查时间", "试验中检查内容", "试验中检查结果", "试验后检查人", "试验后检查时间", "试验后检查内容", "试验后检查结果" };
            int widthAttr[] = { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                    30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 };
            String titleHead = "安全交底";
            List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
            for (SafeConfess safeConfess : resultList) {
                Map<String, String> temp = null;
                List<SafeConfessContent> safeConfessContentList = safeConfess.getSafeConfessContentList();
                if (safeConfessContentList.size() > 0) {
                    for (SafeConfessContent safeConfessContent : safeConfessContentList) {
                        temp = new HashMap<String, String>();
                        temp.put("安全交底名称", safeConfess.getSafeName());
                        temp.put("部门名称", safeConfess.getDeptName());
                        temp.put("项目名称", safeConfess.getProjectName());
                        temp.put("项目编号", safeConfess.getProjectNumber());
                        temp.put("项目负责人", safeConfess.getProjectPerson());
                        temp.put("工作地点", safeConfess.getWorkAddress());
                        temp.put("工作编号", safeConfess.getWorkNumber());
                        temp.put("公司联系人", safeConfess.getCompanyPerson());
                        temp.put("公司联系方式", safeConfess.getCompanyPhone());
                        temp.put("厂方联系人", safeConfess.getFactoryPerson());
                        temp.put("厂方联系方式", safeConfess.getFactoryPhone());
                        temp.put("现场确认人员", safeConfess.getSceneComfirmPer());
                        temp.put("执行人", safeConfess.getDangerManager());
                        temp.put("执行时间", safeConfess.getManagerTime());
                        temp.put("现场试验其它危险源辨识", safeConfess.getDangerDiscern());
                        temp.put("针对上述危险源制定的安全措施", safeConfess.getDangerMeasures());
                        temp.put("现场试验其它环境因素识别", safeConfess.getEnvironmentDiscren());
                        temp.put("针对上述环境因素的控制措施", safeConfess.getEnvironmentMeasures());
                        temp.put("措施确认检查人", safeConfess.getComfirmCheckPerson());
                        temp.put("措施确认检查时间", safeConfess.getComfirmCheckTime());
                        // 状态0:安全及环境相关控制措施 1:技术措施 2:试验前 3:试验中 4:试验后
                        if (safeConfessContent.getcStatus().equals("0")) {
                            temp.put("控制措施内容", safeConfessContent.getcContent());
                            temp.put("控制措施结果", getcCheckResult(safeConfessContent.getcCheckResult()));
    
                        } else if (safeConfessContent.getcStatus().equals("1")) {
                            temp.put("技术措施内容", safeConfessContent.getcContent());
                            temp.put("技术措施结果", getcCheckResult(safeConfessContent.getcCheckResult()));
                        } else if (safeConfessContent.getcStatus().equals("2")) {
                            temp.put("试验前检查人", safeConfess.getTestBeforePerson());
                            temp.put("试验前检查时间", safeConfess.getTestBeforeTime());
                            temp.put("试验前检查内容", safeConfessContent.getcContent());
                            temp.put("试验前检查结果", getcCheckResult(safeConfessContent.getcCheckResult()));
                        } else if (safeConfessContent.getcStatus().equals("3")) {
                            temp.put("试验中检查人", safeConfess.getTestPerson());
                            temp.put("试验中检查时间", safeConfess.getTestTime());
                            temp.put("试验中检查内容", safeConfessContent.getcContent());
                            temp.put("试验中检查结果", getcCheckResult(safeConfessContent.getcCheckResult()));
                        } else if (safeConfessContent.getcStatus().equals("4")) {
                            temp.put("试验后检查人", safeConfess.getTestAfterPerson());
                            temp.put("试验后检查时间", safeConfess.getTestAfterTime());
                            temp.put("试验后检查内容", safeConfessContent.getcContent());
                            temp.put("试验后检查结果", getcCheckResult(safeConfessContent.getcCheckResult()));
                        }
                        dataList.add(temp);
                    }
                } else {
                    temp = new HashMap<String, String>();
                }
            }
            Map<String/* 此处的key为每个sheet的名称,一个excel中可能有多个sheet页 */, List<Map<String/* 此处key对应每一列的标题 */, String>>/* 该list为每个sheet页的数据 */> map = Maps
                    .newHashMap();
            map.put("安全交底列表", dataList);
            ExportExcelByPoiUtil.createExcel(request, response, titleAttr, titleHead, widthAttr, map, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }/* 此处数组为需要合并的列,可能有的需求是只需要某些列里面相同内容合并 */);
        }

    (2)js中

         /**
             * 下载excel
             */
            excelExport:function (){
                var startTime = jQuery('#startTime').val();
                var endTime = jQuery('#endTime').val();
                // 开始时间为空或/结束时间为空
                if (startTime == "" && endTime != "") {
                    startTime = "1999-01-01";
                } else if (startTime != "" && endTime == "") {
                    //获取当前时间
                    endTime = GetDateStrFromCurrentMiuntes(new Date());
                }
                var d1 = new Date(startTime.replace(/-/g, "/"));
                var d2 = new Date(endTime.replace(/-/g, "/"));
                if (startTime != "" && endTime != "" && d1 > d2) {
                    alert("开始时间不能大于结束时间!");
                    return false;
                }
                var form = jQuery("<form>");
                form.attr("style", "display:none");
                form.attr("target", "");
                form.attr("method", "post");
                form.attr("action", "exportController/exportSafeConfess");
                var inputl = jQuery("<input>");
                inputl.attr("type", "hidden");
                inputl.attr("value", startTime);
                inputl.attr("name", "startTime");
    
                var inputl_1 = jQuery("<input>");
                inputl_1.attr("type", "hidden");
                inputl_1.attr("value", endTime);
                inputl_1.attr("name", "endTime");
                jQuery("body").append(form);
                form.append(inputl);
                form.append(inputl_1);
                form.submit();
            },

    在html中页面调用js导出的方法

  • 相关阅读:
    SQL Server 隐式转换引发的死锁
    C# List按某对象的属性分组 IGrouping
    C# 正则表达式获取json字符串中的键值
    .NET程序修改 ConfigurationManager 后,不需要重启IIS也可刷新Web.config配置文件
    相同结构的多个表合并到一个表的实现方法
    WCF系列_WCF影响客户端导出Excel文件的实现
    WCF系列_WCF如何选择不同的绑定
    WCF系列_WCF常用绑定选择
    JS生成URL二维码
    win 常用CMD命令备忘
  • 原文地址:https://www.cnblogs.com/zhou-pan/p/10341224.html
Copyright © 2011-2022 走看看