使用jxl 工具类 导出Excel 单行导出

一、jxl工具类

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.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsplat.qrcode.commons.constant.Constants;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * Created by Juson on 2018/02/08/
 * 导出excel
 */
public class ExportExcelByJxlUtil {

    /**
     * 导出excel初级模板
     * @param request    请求体
     * @param response   响应体
     * @param titleAttr  表头数组
     * @param widthAttr  列宽数组
     * @param title      excel的title名字
     * @param dataList   数据源
     */
    public static void exportExcel(HttpServletRequest request, HttpServletResponse response,
                                   String titleAttr[], int widthAttr[], String title, List<LinkedHashMap<String, Object>> dataList) {
        // 创建Excel工作薄
        WritableWorkbook wwb = null;
        try {
            Calendar calendar1 = Calendar.getInstance();
            String cal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar1.getTime());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String day = format.format(calendar1.getTime());
            OutputStream os = response.getOutputStream();
            response.reset();//清空输出流
            response.setHeader("Content-disposition", "attachment;filename=" +new String(title.getBytes("gbk"), "iso8859-1")+ cal + ".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel;charset=GBK");// 定义输出类型
            wwb = Workbook.createWorkbook(os);
            // 添加第一个工作表并设置第一个Sheet的名字
            WritableSheet sheet = wwb.createSheet(title, 0);
            Label label;
            /**
             * 设置单元格格式(wc)
             */
            WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 16, WritableFont.BOLD);
            WritableCellFormat wc = new WritableCellFormat(font1);
            // 设置居中
            wc.setAlignment(jxl.format.Alignment.CENTRE);
            wc.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wc.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // wc.setBackground(jxl.format.Colour.GREEN);
            /*
             * 合并单元格 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的
             * 表示将从第x+1列,y+1行到m+1列,n+1行合并
             */
            sheet.mergeCells(0, 0, titleAttr.length-1, 0);
            label = new Label(0, 0, title+" " + day, wc);
            sheet.addCell(label);
            /**
             * 设置单元格格式(w)
             */
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat w = new WritableCellFormat(font);
            // 设置居中
            w.setAlignment(Alignment.RIGHT);
            // 设置边框线
            w.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // w.setBackground(jxl.format.Colour.GREEN);
            sheet.mergeCells(0, 1, titleAttr.length-1, 1);
            label = new Label(0, 1, "导出时间:" + cal, w);
            sheet.addCell(label);
            WritableFont wf = new WritableFont(WritableFont.createFont("微软雅黑"),
                    10, WritableFont.BOLD);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            // 设置居中
            wcf.setAlignment(jxl.format.Alignment.CENTRE);
            wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
            //增加背景色
            wcf.setBackground(jxl.format.Colour.GRAY_25);
            for (int i = 0; i < titleAttr.length; i++) {
                label = new Label(i, 2, titleAttr[i], wcf);
                // 将定义好的单元格添加到工作表中
                sheet.addCell(label);
            }
            // setColumnView(参数1, 参数2) 参数1:第几列,参数2:宽度
            for (int i = 0; i < widthAttr.length; i++) {
                sheet.setColumnView(i, widthAttr[i]);
            }
            // 写入数据
            WritableFont wFont = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat wCell = new WritableCellFormat(wFont);
            // 设置边框线
            wCell.setBorder(Border.ALL, BorderLineStyle.THIN);
            wCell.setAlignment(Alignment.CENTRE);
            int j = 3;
            for (int i = 0; i < dataList.size(); i++) {
                int k = j++;
                Set<String> keys = dataList.get(i).keySet() ;// 得到全部的key
                Iterator<String> iter = keys.iterator() ;
                int m = 0;
                while(iter.hasNext()){
                    String str = iter.next() ;
                    label = new Label(m, k, Constants.checkNull(dataList.get(i).get(str)), wCell);
                    sheet.addCell(label);
                    m++;
                }
            }
            wwb.write();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                try {
                    if(wwb != null){
                        // 关闭文件
                        wwb.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 导出excel初级模板
     * @param request    请求体
     * @param response   响应体
     * @param titleAttr  表头数组
     * @param widthAttr  列宽数组
     * @param title      excel的title名字
     * @param dataList   数据源
     */
    public static void exportExcel2(HttpServletRequest request, HttpServletResponse response,
                                   String titleAttr[], int widthAttr[], String title, List<List<Object>> dataList) {
        // 创建Excel工作薄
        WritableWorkbook wwb = null;
        try {
            Calendar calendar1 = Calendar.getInstance();
            String cal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar1.getTime());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String day = format.format(calendar1.getTime());
            OutputStream os = response.getOutputStream();
            response.reset();//清空输出流
            response.setHeader("Content-disposition", "attachment;filename=" + cal + ".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel;charset=GBK");// 定义输出类型
            wwb = Workbook.createWorkbook(os);
            // 添加第一个工作表并设置第一个Sheet的名字
            WritableSheet sheet = wwb.createSheet(title, 0);
            Label label;
            /**
             * 设置单元格格式(wc)
             */
            WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 16, WritableFont.BOLD);
            WritableCellFormat wc = new WritableCellFormat(font1);
            // 设置居中
            wc.setAlignment(jxl.format.Alignment.CENTRE);
            wc.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wc.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // wc.setBackground(jxl.format.Colour.GREEN);
            /*
             * 合并单元格 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的
             * 表示将从第x+1列,y+1行到m+1列,n+1行合并
             */
            sheet.mergeCells(0, 0, titleAttr.length-1, 0);
            label = new Label(0, 0, title+" " + day, wc);
            sheet.addCell(label);
            /**
             * 设置单元格格式(w)
             */
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat w = new WritableCellFormat(font);
            // 设置居中
            w.setAlignment(Alignment.RIGHT);
            // 设置边框线
            w.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // w.setBackground(jxl.format.Colour.GREEN);
            sheet.mergeCells(0, 1, titleAttr.length-1, 1);
            label = new Label(0, 1, "导出时间:" + cal, w);
            sheet.addCell(label);
            WritableFont wf = new WritableFont(WritableFont.createFont("微软雅黑"),
                    10, WritableFont.BOLD);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            // 设置居中
            wcf.setAlignment(jxl.format.Alignment.CENTRE);
            wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
            //增加背景色
            wcf.setBackground(jxl.format.Colour.GRAY_25);
            for (int i = 0; i < titleAttr.length; i++) {
                label = new Label(i, 2, titleAttr[i], wcf);
                // 将定义好的单元格添加到工作表中
                sheet.addCell(label);
            }
            // setColumnView(参数1, 参数2) 参数1:第几列,参数2:宽度
            for (int i = 0; i < widthAttr.length; i++) {
                sheet.setColumnView(i, widthAttr[i]);
            }
            // 写入数据
            WritableFont wFont = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat wCell = new WritableCellFormat(wFont);
            // 设置边框线
            wCell.setBorder(Border.ALL, BorderLineStyle.THIN);
            wCell.setAlignment(Alignment.CENTRE);
            int j = 3;
            for (int i = 0; i < dataList.size(); i++) {
                int k = j++;
                //Set<String> keys = dataList.get(i).keySet() ;// 得到全部的key
                //Iterator<String> iter = keys.iterator() ;
                List<Object> list=dataList.get(i);
                int m = 0;
                //while(list.hasNext()){
                for(int d=0;d<list.size();d++){
                    String str = String.valueOf(list.get(d));
                    label = new Label(m, k, Constants.checkNull(str), wCell);
                    sheet.addCell(label);
                    m++;
                }

               // }
            }
            wwb.write();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                try {
                    if(wwb != null){
                        // 关闭文件
                        wwb.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 导出excel初级模板
     * @param request    请求体
     * @param response   响应体
     * @param titleAttr  表头数组
     * @param widthAttr  列宽数组
     * @param title      excel的title名字
     * @param dataList   数据源
     */
    public static void exportExcel3(HttpServletRequest request, HttpServletResponse response,
                                    String titleAttr[], int widthAttr[], String title, List<List<Object>> dataList) {
        // 创建Excel工作薄
        WritableWorkbook wwb = null;
        try {
            Calendar calendar1 = Calendar.getInstance();
            String cal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar1.getTime());
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            String day = format.format(calendar1.getTime());
            OutputStream os = response.getOutputStream();
            response.reset();//清空输出流
            response.setHeader("Content-disposition", "attachment;filename=" + cal + ".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel;charset=GBK");// 定义输出类型
            wwb = Workbook.createWorkbook(os);
            // 添加第一个工作表并设置第一个Sheet的名字
            WritableSheet sheet = wwb.createSheet(title, 0);
            Label label;
            /**
             * 设置单元格格式(wc)
             */
            WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 16, WritableFont.BOLD);
            WritableCellFormat wc = new WritableCellFormat(font1);
            // 设置居中
            wc.setAlignment(jxl.format.Alignment.CENTRE);
            wc.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wc.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // wc.setBackground(jxl.format.Colour.GREEN);
            /*
             * 合并单元格 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的
             * 表示将从第x+1列,y+1行到m+1列,n+1行合并
             */
           /* sheet.mergeCells(0, 0, titleAttr.length-1, 0);
            label = new Label(0, 0, title+" " + day, wc);
            sheet.addCell(label);*/
            /**
             * 设置单元格格式(w)
             */
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat w = new WritableCellFormat(font);
            // 设置居中
            w.setAlignment(Alignment.RIGHT);
            // 设置边框线
            w.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // w.setBackground(jxl.format.Colour.GREEN);
            //sheet.mergeCells(0, 1, titleAttr.length-1, 1);
           // label = new Label(0, 1, "导出时间:" + cal, w);
           // sheet.addCell(label);

            WritableFont wf = new WritableFont(WritableFont.createFont("微软雅黑"),
                    10, WritableFont.BOLD);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            // 设置居中
            wcf.setAlignment(jxl.format.Alignment.CENTRE);
            wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
            //增加背景色
            wcf.setBackground(Colour.SKY_BLUE);
            for (int i = 0; i < titleAttr.length; i++) {
                label = new Label(i, 0, titleAttr[i], wcf);
                // 将定义好的单元格添加到工作表中
                sheet.addCell(label);
            }
            for (int i = 0; i < widthAttr.length; i++) {
                sheet.setColumnView(i, widthAttr[i]);
            }
            wwb.write();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                try {
                    if(wwb != null){
                        // 关闭文件
                        wwb.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 导出excel初级模板
     * @param request    请求体
     * @param response   响应体
     * @param titleAttr  表头数组
     * @param widthAttr  列宽数组
     * @param title      excel的title名字
     * @param dataList   数据源
     */
    public static void exportExcel4(HttpServletRequest request, HttpServletResponse response,
                                   String titleAttr[], int widthAttr[], String title, List<LinkedHashMap<String, Object>> dataList) {
        // 创建Excel工作薄
        WritableWorkbook wwb = null;
        try {
            Calendar calendar1 = Calendar.getInstance();
            String cal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar1.getTime());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String day = format.format(calendar1.getTime());
            OutputStream os = response.getOutputStream();
            response.reset();//清空输出流
            response.setHeader("Content-disposition", "attachment;filename=" + cal + ".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel;charset=GBK");// 定义输出类型
            wwb = Workbook.createWorkbook(os);
            // 添加第一个工作表并设置第一个Sheet的名字
            WritableSheet sheet = wwb.createSheet(title, 0);
            Label label;
            /**
             * 设置单元格格式(wc)
             */
            WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 16, WritableFont.BOLD);
            WritableCellFormat wc = new WritableCellFormat(font1);
            // 设置居中
            wc.setAlignment(jxl.format.Alignment.CENTRE);
            wc.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wc.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // wc.setBackground(jxl.format.Colour.GREEN);
            /*
             * 合并单元格 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的
             * 表示将从第x+1列,y+1行到m+1列,n+1行合并
             */
            sheet.mergeCells(0, 0, titleAttr.length-1, 0);
            label = new Label(0, 0, title+" " + day, wc);
            sheet.addCell(label);
            /**
             * 设置单元格格式(w)
             */
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat w = new WritableCellFormat(font);
            // 设置居中
            w.setAlignment(Alignment.RIGHT);
            // 设置边框线
            w.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // w.setBackground(jxl.format.Colour.GREEN);
            sheet.mergeCells(0, 1, titleAttr.length-1, 1);
            label = new Label(0, 1, "导出时间:" + cal, w);
            sheet.addCell(label);
            WritableFont wf = new WritableFont(WritableFont.createFont("微软雅黑"),
                    10, WritableFont.BOLD);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            // 设置居中
            wcf.setAlignment(jxl.format.Alignment.CENTRE);
            wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
            //增加背景色
            wcf.setBackground(jxl.format.Colour.GRAY_25);
            for (int i = 0; i < titleAttr.length; i++) {
                label = new Label(i, 2, titleAttr[i], wcf);
                // 将定义好的单元格添加到工作表中
                sheet.addCell(label);
            }
            // setColumnView(参数1, 参数2) 参数1:第几列,参数2:宽度
            for (int i = 0; i < widthAttr.length; i++) {
                sheet.setColumnView(i, widthAttr[i]);
            }
            // 写入数据
            WritableFont wFont = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat wCell = new WritableCellFormat(wFont);
            // 设置边框线
            wCell.setBorder(Border.ALL, BorderLineStyle.THIN);
            wCell.setAlignment(Alignment.CENTRE);
            int j = 3;
            for (int i = 0; i < dataList.size(); i++) {
                int k = j++;
                Set<String> keys = dataList.get(i).keySet() ;// 得到全部的key
                Iterator<String> iter = keys.iterator() ;
                int m = 0;
                while(iter.hasNext()){
                    String str = iter.next() ;
                    label = new Label(m, k, Constants.checkNull(dataList.get(i).get(str)), wCell);
                    //System.out.println(label.getContents());
                    sheet.addCell(label);
                    m++;
                }
            }

            wwb.write();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                try {
                    if(wwb != null){
                        // 关闭文件
                        wwb.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }



    /**
     * 导出excel初级模板
     * @param request    请求体
     * @param response   响应体
     * @param titleAttr  表头数组
     * @param widthAttr  列宽数组
     * @param title      excel的title名字
     * @param dataList   数据源
     */
    public static void exportExcel5(HttpServletRequest request, HttpServletResponse response,
                                    String titleAttr[], int widthAttr[], String title, List<LinkedHashMap<String, Object>> dataList,String total) {
        // 创建Excel工作薄
        WritableWorkbook wwb = null;
        try {
            Calendar calendar1 = Calendar.getInstance();
            String cal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar1.getTime());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String day = format.format(calendar1.getTime());
            OutputStream os = response.getOutputStream();
            response.reset();//清空输出流
            response.setHeader("Content-disposition", "attachment;filename=" + cal + ".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel;charset=GBK");// 定义输出类型
            wwb = Workbook.createWorkbook(os);
            // 添加第一个工作表并设置第一个Sheet的名字
            WritableSheet sheet = wwb.createSheet(title, 0);
            Label label;
            /**
             * 设置单元格格式(wc)
             */
            WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 16, WritableFont.BOLD);
            WritableCellFormat wc = new WritableCellFormat(font1);
            // 设置居中
            wc.setAlignment(jxl.format.Alignment.CENTRE);
            wc.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wc.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // wc.setBackground(jxl.format.Colour.GREEN);
            /*
             * 合并单元格 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的
             * 表示将从第x+1列,y+1行到m+1列,n+1行合并
             */
            sheet.mergeCells(0, 0, titleAttr.length-1, 0);
            label = new Label(0, 0, title+" " + day, wc);
            sheet.addCell(label);
            /**
             * 设置单元格格式(w)
             */
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat w = new WritableCellFormat(font);
            // 设置居中
            w.setAlignment(Alignment.RIGHT);
            // 设置边框线
            w.setBorder(Border.ALL, BorderLineStyle.THIN);
            // 设置单元格的背景颜色
            // w.setBackground(jxl.format.Colour.GREEN);
            sheet.mergeCells(0, 1, titleAttr.length-1, 1);
            label = new Label(0, 1, "导出时间:" + cal, w);
            sheet.addCell(label);
            WritableFont wf = new WritableFont(WritableFont.createFont("微软雅黑"),
                    10, WritableFont.BOLD);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            // 设置居中
            wcf.setAlignment(jxl.format.Alignment.CENTRE);
            wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框线
            wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
            //增加背景色
            wcf.setBackground(jxl.format.Colour.GRAY_25);
            for (int i = 0; i < titleAttr.length; i++) {
                label = new Label(i, 2, titleAttr[i], wcf);
                // 将定义好的单元格添加到工作表中
                sheet.addCell(label);
            }
            // setColumnView(参数1, 参数2) 参数1:第几列,参数2:宽度
            for (int i = 0; i < widthAttr.length; i++) {
                sheet.setColumnView(i, widthAttr[i]);
            }
            // 写入数据
            WritableFont wFont = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat wCell = new WritableCellFormat(wFont);
            // 设置边框线
            wCell.setBorder(Border.ALL, BorderLineStyle.THIN);
            wCell.setAlignment(Alignment.CENTRE);
            int j = 3;
            for (int i = 0; i < dataList.size(); i++) {
                int k = j++;
                Set<String> keys = dataList.get(i).keySet() ;// 得到全部的key
                Iterator<String> iter = keys.iterator() ;
                int m = 0;
                while(iter.hasNext()){
                    String str = iter.next() ;
                    label = new Label(m, k, Constants.checkNull(dataList.get(i).get(str)), wCell);
                    //System.out.println(label.getContents());
                    sheet.addCell(label);
                    m++;
                }
            }


            /*
             * 合并单元格 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的
             * 表示将从第x+1列,y+1行到m+1列,n+1行合并
             */
            //sheet.mergeCells(1, dataList.size()+1, 2, dataList.size()+3);
            label = new Label(1, dataList.size()+3, "总计", wc);
            sheet.addCell(label);

            //sheet.mergeCells(3, dataList.size()+1, 4, dataList.size()+3);
            label = new Label(2, dataList.size()+3, total, wc);
            sheet.addCell(label);

            wwb.write();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                try {
                    if(wwb != null){
                        // 关闭文件
                        wwb.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }



}

二 controller 中

@ResponseBody
    @RequestMapping(value = "/exportSuggestionDetail", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8" })
    public void exportSuggestionDetail(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<SuggestionDetail> resultList = suggestionDetailService.querySuggestionDetail(paramsValue);
        String titleAttr[] = { "建议类型", "建议内容", "发出时间", "更新时间", "发出人员", "审批人员", "审批时间", "审批", "处理情况", "处理内容" };
        int widthAttr[] = { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 };
        String title = "建议内容";
        List<LinkedHashMap<String, Object>> dataList = new ArrayList<LinkedHashMap<String, Object>>();
        for (SuggestionDetail suggestionDetail : resultList) {
            LinkedHashMap<String, Object> temp = new LinkedHashMap<String, Object>();
            temp.put("key_1", suggestionDetail.getSuggestName());
            temp.put("key_2", suggestionDetail.getSuggestDetail());
            temp.put("key_3", suggestionDetail.getCreateTime());
            temp.put("key_4", suggestionDetail.getUpdateTime());
            temp.put("key_5", suggestionDetail.getCreateName());
            temp.put("key_6", suggestionDetail.getAgreeName());
            temp.put("key_7", suggestionDetail.getAgreeTime());
            String string = "";
            if (suggestionDetail.getIsAgree().equals("0")) {
                string = "未审批";
            } else if (suggestionDetail.getIsAgree().equals("1")) {
                string = "已拒绝";
            } else if (suggestionDetail.getIsAgree().equals("2")) {
                string = "已同意";
            }
            temp.put("key_8", string);
            if (suggestionDetail.getIsAgree().equals("2")) {
                temp.put("key_9", suggestionDetail.getHandleStatus().equals("0") ? "未处理" : "已处理");
            } else {
                temp.put("key_9", "");
            }
            temp.put("key_10", suggestionDetail.getHandleContent());
            dataList.add(temp);
        }
        ExportExcelByJxlUtil.exportExcel(request, response, titleAttr, widthAttr, title, dataList);
    }
原文地址:https://www.cnblogs.com/zhou-pan/p/10341237.html