jqGrid结合POI实现自定义Excel列导出

通过之前讲过的jqGrid的columnChooser可以动态隐藏或者显示列,将显示列的列名和字段名传入后端,通过POI就可以实现自定义字段导出Excel

以下是js代码

$(function(){
	//页面加载完成之后执行
	pageInit();
});
function pageInit(){
	//创建jqGrid组件
	jQuery("#list2").jqGrid(
			{
				url : 'selectAll',//组件创建完成之后请求数据的url
				datatype : "json",//请求数据返回的类型。可选json,xml,txt
				colModel : [ //jqGrid每一列的配置信息。包括名字,索引,宽度,对齐方式.....
				             {label : '学号',name : 'id',width : 255},
				             {label : '姓名',name : 'name',width : 300},
					{label : '年龄',name : 'age',width : 290},


				],
				rowNum : 10,//一页显示多少条
				rowList : [ 10, 20, 30 ],//可供用户选择一页显示多少条
				pager : '#pager2',//表格页脚的占位符(一般是div)的id
				mtype : "post",//向后台请求数据的ajax的类型。可选post,get
				viewrecords : true,
				caption : "JSON Example"//表格的标题名字
			});
	$("#list2").jqGrid('navGrid','#pager2',{add:false,edit:false,del:false,search:false,refresh:false});
	$("#list2").jqGrid('navButtonAdd','#pager2',{
		caption: "Columns",
		title: "Reorder Columns",
		onClickButton : function (){
			$("#list2").jqGrid('columnChooser');
		}
	});

}

$("#export").click(function () {
	var columns = $("#list2").jqGrid("getGridParam","colModel");
	var nameList = new Array();
	var indexList = new Array();
	columns.forEach(function (column) {
		//将非隐藏的字段名和字段抽取出来
		if(column.hidden == false){
			nameList.push(column.label);
			indexList.push(column.name);
		}
	})
	//这里之所以用跳转是因为ajax不能进行下载,数组直接拼接
	window.location.href = "export?indexList=" + indexList + "&nameList=" + nameList;
});

HTML代码就不贴了,在之前的博客中有现成的可以下载,放一张页面的效果图,隐藏一列

Controller端代码

   public void export(HttpServletRequest request, HttpServletResponse response,String[]indexList,String[]nameList){
        //获取数组
        List<Person> list = userMapper.select();
        //通过传入Class,列名以及字段名的数组得到service处理后的workbook
        HSSFWorkbook workbook = exportService.getWorkbook(Person.class,nameList,indexList,list);
        String fileName = "test.xls";
        //xls对应的content-type
        response.setContentType("application/x-xls");
        //通知浏览器下载附件
        response.setHeader("Content-Disposition","attachment;fileName=" + fileName);
        OutputStream out = null;
        try{
            out = response.getOutputStream();
            workbook.write(out);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.flush();
                out.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

  Service的处理代码

    public HSSFWorkbook getWorkbook(Class cls , String[]nameList, String[] indexList, List<Person> persons){
        HSSFWorkbook workbook = null;
        try{
            workbook = new HSSFWorkbook();
            //生成一个sheet
            HSSFSheet sheet = workbook.createSheet();
            //创建第一行标题
            Row row = sheet.createRow(0);
            //根据传入的name创建cell并赋值
            for(int i = 0; i < nameList.length; i++){
                Cell c = row.createCell(i);
                c.setCellValue(nameList[i]);
            }
            for(int i = 0; i < persons.size(); i++){
                Person p = persons.get(i);
                //第一行为标题,从第二行开始插入数据
                row = sheet.createRow(i+1);
                //遍历字段数组通过类的反射获取get方法得到数据
                for(int j = 0; j < indexList.length; j++){
                    String methodName = "get" + indexList[j].substring(0,1).toUpperCase() + indexList[j].substring(1);
                    Method method = cls.getDeclaredMethod(methodName);
                    Object obj = method.invoke(p);
                    if(obj == null){
                        obj = "";
                    }
                    Cell c = row.createCell(j);
                    c.setCellValue(obj.toString());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return workbook;
    }

  生成的Excel预览

代码写的比较粗糙,service处理workbook那也只是写了一个大概,具体对值的处理以及cell样式也没有涉及。这里只是提供一个导出的思路供大家参考

  

猜你喜欢

转载自www.cnblogs.com/guofx/p/11229346.html