1@RequestMapping (value = "/download", method =requestmethod.get)2 Public voidDownstudents (HttpServletRequest request, httpservletresponse response)throwsIOException {3 //first, from the background to take the data4list<dept> list =NULL;5list=ds.list ();6 Try {7 //ii. data turned into Excel8Request.setcharacterencoding ("UTF-8");9Response.setcharacterencoding ("UTF-8");TenResponse.setcontenttype ("Application/x-download"); One AString fileName = "Dept.xls"; -filename = urlencoder.encode (filename, "UTF-8")); -Response.AddHeader ("Content-disposition", "attachment;filename=" +fileName); the //The first step: Define a new workbook -Hssfworkbook WB =NewHssfworkbook (); - //Step Two: Create a sheet page -Hssfsheet sheet = wb.createsheet ("Deptsheet"); +Hssfcellstyle style=Wb.createcellstyle (); -Style.setalignment (Hssfcellstyle.align_center);//Set Center (invalid) +Sheet.setdefaultrowheight (( Short) (256));//Set Row Height ASheet.setcolumnwidth (0, 2000);//Set column widths atSheet.setcolumnwidth (1, 5000); -Sheet.setcolumnwidth (2, 5500); -Sheet.setcolumnwidth (3, 5500); - -Hssffont Font =Wb.createfont (); -Font.setfontname ("The song Body");//Set Font inFont.setfontheightinpoints (( Short) 16);//Set Text Size - toHssfrow row = Sheet.createrow (0); +Hssfcell cell = Row.createcell (0); -Cell.setcellvalue ("ordinal"); theCell = Row.createcell (1); *Cell.setcellvalue ("department number")); $Cell = Row.createcell (2);Panax NotoginsengCell.setcellvalue ("Department name")); -Cell = Row.createcell (3); theCell.setcellvalue ("Address"); + A hssfrow rows; the Hssfcell cells; + for(inti = 0; I < list.size (); i++) { - //Step Three: Create a line in this sheet page $rows = Sheet.createrow (i + 1); $ //Fourth Step: Create a cell in the row -Cells = Rows.createcell (0); - //Fifth Step: Set the value in the cell theCells.setcellvalue (i+1); -Cells = Rows.createcell (1);Wuyi Cells.setcellvalue (List.get (i). Getdeptno ()); theCells = Rows.createcell (2); - Cells.setcellvalue (List.get (i). Getdname ()); WuCells = Rows.createcell (3); - Cells.setcellvalue (List.get (i). Getloc ()); About $ } - -OutputStream out =Response.getoutputstream (); - Wb.write (out); A out.close (); +Wb.clonesheet (0); the}Catch(IOException e) { - e.printstacktrace (); $ } the}
Javacode
Java Web read database data written to Excel back to browser download