Java Excel export example

Source: Internet
Author: User
1 package COM. NBM. export. excel; 2 3 Import Java. io. bytearrayoutputstream; 4 5 import javax. servlet. servletoutputstream; 6 7 Import Org. apache. poi. hssf. usermodel. hssfcell; 8 Import Org. apache. poi. hssf. usermodel. hssfcellstyle; 9 Import Org. apache. poi. hssf. usermodel. hssffont; 10 Import Org. apache. poi. hssf. usermodel. hssfrow; 11 import Org. apache. poi. hssf. usermodel. hssfsheet; 12 Import Org. apache. poi. hssf. usermodel. hssfworkbook; 13 Import Org. apache. poi. hssf. util. hssfcellutil; 14 Import Org. apache. poi. hssf. util. region; 15 Import Org. apache. struts2.servletactioncontext; 16 17 public class excelaction {18 19 public void exportexcel () 20 {21 22 // create a working thin 23 hssfworkbook WB = new hssfworkbook (); 24 25 26 hssfcellstyle cellborder = WB. createcellstyle (); 27 28 // set the border to 29 cellborder. setborderleft (hssfcellstyle. border_thin); 30 cellborder. setborderright (hssfcellstyle. border_thin); 31 cellborder. setbordertop (hssfcellstyle. border_thin); 32 cellborder. setborderbottom (hssfcellstyle. border_thin); 33 34 // center 35 cellborder. setalignment (hssfcellstyle. align_center); 36 37 cellborder. setwraptext (true); // automatically wrap 38 39 hssffont font2 = WB. createfont (); 40 font2.setfontname (" _ gb2312"); // font 41 font2.setboldweight (hssffont. boldweight_bold); // The Bold type is 42 font2.setfontheightinpoints (short) 12); // The cellborder size is 43. setfont (font2); // select the font format 44 45 46 47 hssfsheet sheet = WB. createsheet ("sheet1"); 48 49 // merge cells 50 region region1 = new region (0, (short) 0, 0, (short) 6); 51 sheet. addmergedregion (region1); 52 53 54 55 hssfrow Title = sheet. createrow (0); 56 hssfcell Tcell = title. createcell (0); 57 Tcell. setcellvalue ("Liu Xiangying village, tongfangyu Township, Jixian city, Tianjin"); 58 setregionstyle (sheet, region1, cellborder); // set the style of the merged cell 59 60 sheet. setcolumnwidth (0, 2500); // The first parameter indicates the column ID (starting from 0). The second parameter indicates the width value. For details, see: the width of "" is 2500 61 62 hssfrow row1 = sheet. createrow (1); 63 hssfcell cell0 = row1.createcell (0); 64 cell0.setcellvalue ("2012-11-22"); 65 66 hssfrow row2 = sheet. createrow (2); 67 hssfcell row2cell0 = row2.createcell (0); 68 row2cell0. setcellvalue ("Test automatic line feed, automatic line feed, automatic line feed"); 69 row2cell0. setcellstyle (cellborder); 70 71 72 // output data stream 73 try 74 {75 76 bytearrayoutputstream baos = new bytearrayoutputstream (); 77 WB. write (baos); 78 servletactioncontext. getresponse (). setcontenttype (79 "application/vnd. MS-excel "); 80 servletactioncontext. getresponse (). setcontentlength (baos. size (); 81 servletoutputstream out = servletactioncontext. getresponse () 82. getoutputstream (); 83 baos. writeto (out); 84 out. flush (); 85 86 87} catch (exception E) 88 {89 90} 91} 92/** set the merged cell style 93*94 * @ Param sheet 95 * @ Param region 96 * @ Param CS 97 */98 private void setregionstyle (hssfsheet sheet, region region, hssfcellstyle CS) {99 int toprownum = region. getrowfrom (); 100 for (INT I = region. getrowfrom (); I <= region. getrowto (); I ++) {101 hssfrow ROW = hssfcellutil. getrow (I, sheet); 102 for (Int J = region. getcolumnfrom (); j <= region. getcolumnto (); j ++) {103 hssfcell cell = hssfcellutil. getcell (row, (short) J); 104 cell. setcellstyle (CS); 105} 106} 107} 108 109 110 111}

 

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.