/** * * @paramout Stream *@parammaplist Data *@paramTitle *@paramHeaders Table Header *@paramthe field name corresponding to the keys header *@return */ Public Static BooleanGetexceldao (OutputStream out, List<map<string, object>>Maplist, String title, string[] headers,string keys[]) { Try { //Create a workbookHssfworkbook Workbook =NewHssfworkbook (); //Create a work page with a nameHssfsheet sheet =Workbook.createsheet (title); Sheet.setdefaultcolumnwidth (25); //Generate a styleHssfcellstyle style =Workbook.createcellstyle (); //set these stylesStyle.setfillforegroundcolor (HSSFColor.SKY_BLUE.index); Style.setfillpattern (Hssfcellstyle.solid_foreground); Style.setborderbottom (Hssfcellstyle.border_thin); Style.setborderleft (Hssfcellstyle.border_thin); Style.setborderright (Hssfcellstyle.border_thin); Style.setbordertop (Hssfcellstyle.border_thin); Style.setalignment (Hssfcellstyle.align_center); //Generate a fontHssffont Font =Workbook.createfont (); Font.setcolor (HSSFColor.VIOLET.index); Font.setfontheightinpoints (( Short) 12); Font.setboldweight (Hssffont.boldweight_bold); //apply a font to the current styleStyle.setfont (font); //build and set another styleHssfcellstyle Style2 =Workbook.createcellstyle (); Style2.setfillforegroundcolor (HSSFColor.LIGHT_YELLOW.index); Style2.setfillpattern (Hssfcellstyle.solid_foreground); Style2.setborderbottom (Hssfcellstyle.border_thin); Style2.setborderleft (Hssfcellstyle.border_thin); Style2.setborderright (Hssfcellstyle.border_thin); Style2.setbordertop (Hssfcellstyle.border_thin); Style2.setalignment (Hssfcellstyle.align_center); Style2.setverticalalignment (Hssfcellstyle.vertical_center); //Generate another fontHssffont Font2 =Workbook.createfont (); Font2.setboldweight (Hssffont.boldweight_normal); //apply a font to the current styleStyle2.setfont (Font2); //Generating Coordinate rows//header rowHssfrow row = Sheet.createrow (0); Hssfcell Cell= Row.createcell (0); Cell.setcellstyle (style); Cell.setcellvalue (title); //Center The title, merge cells four parameters start and end of rows and columns if(Headers! =NULL&& headers.length > 0) sheet.addmergedregion (NewCellrangeaddress (0, 0, 0, Headers.length-1)); ElseSheet.addmergedregion (NewCellrangeaddress (0, 0, 0, 1)); //Generate a table headerrow = Sheet.createrow (1); for(inti = 0; i < headers.length; i++) {cell=Row.createcell (i); Cell.setcellstyle (style); Cell.setcellvalue (Headers[i]); } //Generate Content for(inti = 0; I < maplist.size (); i++) {Map<string, object> objectmap =Maplist.get (i); Row= Sheet.createrow (i + 2); intCount = 0;//Traverse Map for(String key:keys) {cell=Row.createcell (count); Cell.setcellstyle (Style2); Cell.setcellvalue (string.valueof (Objectmap.get (key)). Equals ("null")? "": O Bjectmap.get (Key) + ""); Count++; } /*for (entry<string, object> entry:objectmap.entrySet ()) {//System.out.println ("key=" + en Try.getkey () +//"and value=" + entry.getvalue ()); Cell = Row.createcell (count); Cell.setcellvalue (Entry.getvalue (). toString ()); count++; }*/ } Try{workbook.write (out); return true; } Catch(IOException e) {e.printstacktrace (); return false; }finally{workbook.close (); } } Catch(Exception e) {e.printstacktrace (); return false; } }
Use
String keys[]={"id", "mname", "O3title", "num", "userid", "useraddress", "Getphone", "Postname", "Postage", "flagname", " Buildtime "," Kjtime "}; String headers[]={"order Number", "category", "Commodity Name", "Treasure Count", "User ID", "Address", "number", "Courier", "Courier", "Order Status", "Place a single time", "Draw Time"}; // orderserviceimpl.query_querylistall (query, columns, tablename, sort) // System.out.println (maplist.tostring ()); OutputStream outputstream = response.getoutputstream (); Excelutil.getexceldao (OutputStream, Maplist2, title, headers, keys); Outputstream.close ();
Export Java data to Excel