以下是js代码

  1. $(function(){
  2. //页面加载完成之后执行
  3. pageInit();
  4. });
  5. function pageInit(){
  6. //创建jqGrid组件
  7. jQuery("#list2").jqGrid(
  8. {
  9. url : \'selectAll\',//组件创建完成之后请求数据的url
  10. datatype : "json",//请求数据返回的类型。可选json,xml,txt
  11. colModel : [ //jqGrid每一列的配置信息。包括名字,索引,宽度,对齐方式.....
  12. {label : \'学号\',name : \'id\',width : 255},
  13. {label : \'姓名\',name : \'name\',width : 300},
  14. {label : \'年龄\',name : \'age\',width : 290},
  15.  
  16.  
  17. ],
  18. rowNum : 10,//一页显示多少条
  19. rowList : [ 10, 20, 30 ],//可供用户选择一页显示多少条
  20. pager : \'#pager2\',//表格页脚的占位符(一般是div)的id
  21. mtype : "post",//向后台请求数据的ajax的类型。可选post,get
  22. viewrecords : true,
  23. caption : "JSON Example"//表格的标题名字
  24. });
  25. $("#list2").jqGrid(\'navGrid\',\'#pager2\',{add:false,edit:false,del:false,search:false,refresh:false});
  26. $("#list2").jqGrid(\'navButtonAdd\',\'#pager2\',{
  27. caption: "Columns",
  28. title: "Reorder Columns",
  29. onClickButton : function (){
  30. $("#list2").jqGrid(\'columnChooser\');
  31. }
  32. });
  33.  
  34. }
  35.  
  36. $("#export").click(function () {
  37. var columns = $("#list2").jqGrid("getGridParam","colModel");
  38. var nameList = new Array();
  39. var indexList = new Array();
  40. columns.forEach(function (column) {
  41. //将非隐藏的字段名和字段抽取出来
  42. if(column.hidden == false){
  43. nameList.push(column.label);
  44. indexList.push(column.name);
  45. }
  46. })
  47. //这里之所以用跳转是因为ajax不能进行下载,数组直接拼接
  48. window.location.href = "export?indexList=" + indexList + "&nameList=" + nameList;
  49. });

  1. public void export(HttpServletRequest request, HttpServletResponse response,String[]indexList,String[]nameList){
  2. //获取数组
  3. List<Person> list = userMapper.select();
  4. //通过传入Class,列名以及字段名的数组得到service处理后的workbook
  5. HSSFWorkbook workbook = exportService.getWorkbook(Person.class,nameList,indexList,list);
  6. String fileName = "test.xls";
  7. //xls对应的content-type
  8. response.setContentType("application/x-xls");
  9. //通知浏览器下载附件
  10. response.setHeader("Content-Disposition","attachment;fileName=" + fileName);
  11. OutputStream out = null;
  12. try{
  13. out = response.getOutputStream();
  14. workbook.write(out);
  15. }catch(Exception e){
  16. e.printStackTrace();
  17. }finally {
  18. try {
  19. out.flush();
  20. out.close();
  21. workbook.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25.  
  26. }
  27. }
  1. public HSSFWorkbook getWorkbook(Class cls , String[]nameList, String[] indexList, List<Person> persons){
  2. HSSFWorkbook workbook = null;
  3. try{
  4. workbook = new HSSFWorkbook();
  5. //生成一个sheet
  6. HSSFSheet sheet = workbook.createSheet();
  7. //创建第一行标题
  8. Row row = sheet.createRow(0);
  9. //根据传入的name创建cell并赋值
  10. for(int i = 0; i < nameList.length; i++){
  11. Cell c = row.createCell(i);
  12. c.setCellValue(nameList[i]);
  13. }
  14. for(int i = 0; i < persons.size(); i++){
  15. Person p = persons.get(i);
  16. //第一行为标题,从第二行开始插入数据
  17. row = sheet.createRow(i+1);
  18. //遍历字段数组通过类的反射获取get方法得到数据
  19. for(int j = 0; j < indexList.length; j++){
  20. String methodName = "get" + indexList[j].substring(0,1).toUpperCase() + indexList[j].substring(1);
  21. Method method = cls.getDeclaredMethod(methodName);
  22. Object obj = method.invoke(p);
  23. if(obj == null){
  24. obj = "";
  25. }
  26. Cell c = row.createCell(j);
  27. c.setCellValue(obj.toString());
  28. }
  29. }
  30. }catch (Exception e){
  31. e.printStackTrace();
  32. }
  33. return workbook;
  34. }

 

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

  

版权声明:本文为guofx原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/guofx/p/11229346.html