jqGrid combination of custom POI achieve export Excel columns

JqGrid can be dynamically hidden by the columnChooser talked about before or display columns, column names and field names will display incoming back-end column, POI can be achieved through custom fields to export Excel

The following is a js code

$ (function () { 
	// execute page after loaded 
	pageInit (); 
}); 
function pageInit () { 
	// create jqGrid components 
	jQuery ( "# list2") jqGrid (. 
			{ 
				url: 'selectAll', // components url request data created after the 
				datatype: "json", // request type data returned Alternatively JSON, XML, TXT. 
				colModel: [// jqGrid configuration information including the name of each column, index, width, alignment. .... 
				             {label: 'student number', name: 'ID', width: 255}, 
				             {label: 'name', name: 'name', width: 300}, 
					{label: 'Age', name: 'Age', width: 290 is}, 


				], 
				rowNum: 10, show an // how many 
				rowList: [10, 20, 30 ], // how many to choose a display 
				pager: '# pager2', // table footer placeholder (usually div) of the above mentioned id 
				mtype: "post ", // type of requested data to the background ajax Optionally post, get
				viewrecords : true,
		if (column.hidden == false) {
				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) { 
			indexList.push (column.name);
			nameList.push (column.label);
		// non-hidden field names and extracted
		} 
	}) 
	// Jump reason here is because ajax can not be downloaded directly spliced array 
	window.location.href = + indexList + "& nameList =" "Export indexList =?" + NameList; 
});

HTML code is not posted there in the previous blog, ready to download and put on a page renderings, hide a

Controller side code

   Export void public (the HttpServletRequest Request, Response the HttpServletResponse, String [] indexList, String [] nameList) { 
        // Get array 
        List <the Person> = userMapper.select List (); 
        // by passing Class, column name and field name workbook array obtained after service processing 
        HSSFWorkbook workbook = exportService.getWorkbook (Person.class, nameList, indexList, List); 
        String fileName = "Test.xls"; 
        // XLS-type corresponding to the Content 
        the response.setContentType ( "file application / X -xls "); 
        // notify the browser to download attachments 
        response.setHeader (" the Content-Disposition "," attachment; fileName = "+ fileName); 
        the OutputStream OUT = null; 
        the 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 handling code

    HSSFWorkbook GetWorkbook public (Class CLS, String [] nameList, String [] indexList, List <the Person> persons) { 
        HSSFWorkbook Workbook = null; 
        the try { 
            Workbook new new HSSFWorkbook = (); 
            // generates a Sheet 
            HSSFSheet Sheet workbook.createSheet = ( ); 
            // Create a first row header 
            row row = sheet.createRow (0); 
            // Create the passed name and cell assignment 
            for (int I = 0; I <nameList.length; I ++) { 
                the cell C = row .createCell (I); 
                c.setCellValue (nameList [I]); 
            } 
            for (int I = 0; I <persons.size (); I ++) { 
                Person p = persons.get(i);
                // first row header, starting at the second row of data
                sheet.createRow = Row (I +. 1); 
                // iterate through the reflective array class field get method obtaining data obtained 
                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 = ""; 
                    } 
                    the Cell row.createCell = C (J); 
                    c.setCellValue (obj.toString ()); 
                }  
            }
        } the catch (Exception E) { 
            e.printStackTrace ();
        }
        return workbook;
    }

  Excel-generated preview

 

Code written in relatively coarse, service processing workbook it was only written about a specific value of the processing, and cell styles are also not covered. Just a thought derived here for your reference

  

Guess you like

Origin www.cnblogs.com/guofx/p/11229346.html