Java Web read database data written to Excel back to browser download

Source: Internet
Author: User
Tags java web

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.