Memo: JSP uses POI to export EXCEL__JS

Source: Internet
Author: User
Tags border color dname first row

First thing, from the online next POI package and then import to MyEclipse.

And then the code:

Public ActionResult Excelprint () {Hssfworkbook workbook = new Hssfworkbook ();//Create an Excel file Hssfsheet sheet = Workbook.createsheet ()///Create an Excel sheet Sheet.createfreezepane (1, 3);//freeze/Set column width Sheet.setcolu   
    Mnwidth (0, 1000);   
    Sheet.setcolumnwidth (1, 3500);   
    Sheet.setcolumnwidth (2, 3500);   
    Sheet.setcolumnwidth (3, 6500);   
    Sheet.setcolumnwidth (4, 6500);   
    Sheet.setcolumnwidth (5, 6500);   
    Sheet.setcolumnwidth (6, 6500);   
    Sheet.setcolumnwidth (7, 2500);   
    Sheet style Hssfcellstyle Sheetstyle = Workbook.createcellstyle ();   
    setting of background color sheetstyle.setfillbackgroundcolor (HSSFColor.GREY_25_PERCENT.index);   
    The setting of the foreground color sheetstyle.setfillforegroundcolor (HSSFColor.GREY_25_PERCENT.index);   
    Fill mode Sheetstyle.setfillpattern (hssfcellstyle.fine_dots);   
    Sets the style for the column for (int i = 0; I <= i++) {Sheet.setdefaultcolumnstyle ((short) I, Sheetstyle);   
  }  Set font Hssffont Headfont = Workbook.createfont ();   
    Headfont.setfontname ("Blackbody");  Headfont.setfontheightinpoints ((short) 22),//font size headfont.setboldweight (hssffont.boldweight_bold),//bold//   
    Another style Hssfcellstyle Headstyle = Workbook.createcellstyle ();   
    Headstyle.setfont (Headfont); Headstyle.setalignment (hssfcellstyle.align_center);/Around center headstyle.setverticalalignment (HSSFCellStyle.VERTICAL   
    _center);//Up and Down center headstyle.setlocked (true);   
    Headstyle.setwraptext (TRUE);//auto wrap/Another font style hssffont Columnheadfont = Workbook.createfont ();   
    Columnheadfont.setfontname ("Song Body");   
    Columnheadfont.setfontheightinpoints ((short) 10);   
    Columnheadfont.setboldweight (Hssffont.boldweight_bold);   
    The style of the column head Hssfcellstyle columnheadstyle = Workbook.createcellstyle ();   
    Columnheadstyle.setfont (Columnheadfont); Columnheadstyle.setalignment (hssfcellstyle.align_center)/left center COLUMNHEadstyle.setverticalalignment (Hssfcellstyle.vertical_center);/Up and Down center columnheadstyle.setlocked (true);   
    Columnheadstyle.setwraptext (TRUE); Columnheadstyle.setleftbordercolor (HSSFColor.BLACK.index)//left Border color columnheadstyle.setborderleft ((short) 1);//bounding box The size columnheadstyle.setrightbordercolor (HSSFColor.BLACK.index);//Right Border color columnheadstyle.setborderright (sho RT) 1;//the size of the border Columnheadstyle.setborderbottom (Hssfcellstyle.border_thin); Set the border of the cell to Bold Columnheadstyle.setbottombordercolor (HSSFColor.BLACK.index);   
  
    Sets the border color of cells//sets the background color of a cell (the cell's style overrides the column or row's style) Columnheadstyle.setfillforegroundcolor (HSSFColor.WHITE.index);   
    Hssffont font = Workbook.createfont ();   
    Font.setfontname ("Song Body");   
    Font.setfontheightinpoints ((short) 10);   
    Normal cell style Hssfcellstyle style = Workbook.createcellstyle ();   
    Style.setfont (font); Style.setalignment (hssfcellstyle.align_left)/left center Style.setvertiCalalignment (hssfcellstyle.vertical_top);/Up and Down center Style.setwraptext (true);   
    Style.setleftbordercolor (HSSFColor.BLACK.index);   
    Style.setborderleft ((short) 1);   
    Style.setrightbordercolor (HSSFColor.BLACK.index);   
    Style.setborderright ((short) 1); Style.setborderbottom (Hssfcellstyle.border_thin); Set the border of the cell to Bold Style.setbottombordercolor (HSSFColor.BLACK.index);   
    Sets the border color of the cell.   
    Style.setfillforegroundcolor (HSSFColor.WHITE.index);//sets the background color of the cell.   
    Another style Hssfcellstyle Centerstyle = Workbook.createcellstyle ();   
    Centerstyle.setfont (font); Centerstyle.setalignment (hssfcellstyle.align_center);/Around Center centerstyle.setverticalalignment (   
    Hssfcellstyle.vertical_center);//Up and Down center Centerstyle.setwraptext (true);   
    Centerstyle.setleftbordercolor (HSSFColor.BLACK.index);   
    Centerstyle.setborderleft ((short) 1);   
    Centerstyle.setrightbordercolor (HSSFColor.BLACK.index); Centerstyle.setborderright (short) 1); Centerstyle.setborderbottom (Hssfcellstyle.border_thin); Set the border of the cell to Bold Centerstyle.setbottombordercolor (HSSFColor.BLACK.index);   
    Sets the border color of the cell.   
  
    Centerstyle.setfillforegroundcolor (HSSFColor.WHITE.index);//sets the background color of the cell.   
      try {//Create first row Hssfrow row0 = sheet.createrow (0);   
      Set Row height row0.setheight ((short) 900);   
      Create the first column Hssfcell cell0 = Row0.createcell (0);   
      Cell0.setcellvalue (New Hssfrichtextstring ("The implementation of the project schedule for the investment projects of the Central African Development Fund"));   
      Cell0.setcellstyle (Headstyle); /** * Merge cell * First argument: number of rows in first cell (starting from 0) * Second argument: number of rows in second cell (starting at 0) * Third parameter: Number of columns in first cell   
      (starting from 0) * Fourth parameter: Number of columns in the second cell (starting from 0)/cellrangeaddress range = new cellrangeaddress (0, 0, 0, 7);   
      Sheet.addmergedregion (range);   
      Create a second row Hssfrow row1 = Sheet.createrow (1);   
      Hssfcell cell1 = Row1.createcell (0); Cell1.setcellvalue (New HssfrichtExtstring ("This meeting time: August 31, 2009 Previous meeting time: August 24, 2009"));   
      Cell1.setcellstyle (Centerstyle);   
      Merge cell range = new Cellrangeaddress (1, 2, 0, 7);   
      Sheet.addmergedregion (range);   
      The third line Hssfrow Row2 = Sheet.createrow (3);   
      Row2.setheight ((short) 750);   
      Hssfcell cell = Row2.createcell (0);   
      Cell.setcellvalue (New hssfrichtextstring ("Responsible person"));   
      Cell.setcellstyle (Columnheadstyle);   
      Cell = Row2.createcell (1);   
      Cell.setcellvalue ("Order of Maturity") (new hssfrichtextstring);   
      Cell.setcellstyle (Columnheadstyle);   
      Cell = Row2.createcell (2);   
      Cell.setcellvalue (New hssfrichtextstring ("Matters"));   
      Cell.setcellstyle (Columnheadstyle);   
      Cell = Row2.createcell (3);   
      Cell.setcellvalue ("\n/project summary of the new project requested by the previous meeting") (hssfrichtextstring));   
      Cell.setcellstyle (Columnheadstyle);   
      Cell = Row2.createcell (4);   
      Cell.setcellvalue ("Progress of work last week") (new hssfrichtextstring); Cell. Setcellstyle (Columnheadstyle);   
      Cell = Row2.createcell (5);   
      Cell.setcellvalue (New hssfrichtextstring ("This week's work plan"));   
      Cell.setcellstyle (Columnheadstyle);   
      Cell = Row2.createcell (6);   
      Cell.setcellvalue (New hssfrichtextstring ("Issues and Recommendations"));   
      Cell.setcellstyle (Columnheadstyle);   
      Cell = Row2.createcell (7);   
      Cell.setcellvalue ("Remark") (new hssfrichtextstring);   
      Cell.setcellstyle (Columnheadstyle);   
      Access to the database, get the dataset list<deitelvo> Deitelvolist = Getentitymanager (). Querydeitelvolist ();   
      int m = 4;   
      int k = 4;   
        for (int i = 0; i < deitelvolist.size (); i++) {Deitelvo vo = Deitelvolist.get (i);   
        String dname = Vo.getdname ();   
        List<workinfo> worklist = Vo.getworkinfolist ();   
        Hssfrow row = Sheet.createrow (m);   
        Cell = Row.createcell (0);   
        Cell.setcellvalue (New hssfrichtextstring (dname)); Cell.setcellstyle (centerstyle);   
        Merge cell range = new cellrangeaddress (m, M + worklist.size ()-1, 0, 0);   
        Sheet.addmergedregion (range);   
  
        m = m + worklist.size ();   
          for (int j = 0; J < Worklist.size (); j + +) {Workinfo w = worklist.get (j);   
          Walk the dataset to create Excel's Row = Sheet.getrow (k + j);   
          if (null = = row) {row = Sheet.createrow (k + j);   
          Cell = Row.createcell (1);   
          Cell.setcellvalue (W.getwnumber ());   
          Cell.setcellstyle (Centerstyle);   
          Cell = Row.createcell (2);   
          Cell.setcellvalue (New Hssfrichtextstring (W.getwitem ()));   
          Cell.setcellstyle (style);   
          Cell = Row.createcell (3);   
          Cell.setcellvalue (New Hssfrichtextstring (W.getwmeting ()));   
          Cell.setcellstyle (style);   
          Cell = Row.createcell (4);   
          Cell.setcellvalue (New Hssfrichtextstring (W.getwbweek ())); Cell. Setcellstyle (style);   
          Cell = Row.createcell (5);   
          Cell.setcellvalue (New Hssfrichtextstring (W.getwtweek ()));   
          Cell.setcellstyle (style);   
          Cell = Row.createcell (6);   
          Cell.setcellvalue (New Hssfrichtextstring (W.getwproblem ()));   
          Cell.setcellstyle (style);   
          Cell = Row.createcell (7);   
          Cell.setcellvalue (New Hssfrichtextstring (W.getwremark ()));   
        Cell.setcellstyle (style);   
      K = k + worklist.size ();   
      }//Lieli int footrownumber = Sheet.getlastrownum ();   
      Hssfrow Footrow = Sheet.createrow (footrownumber + 1);   
      Hssfcell Footrowcell = Footrow.createcell (0);   
      Footrowcell.setcellvalue (New Hssfrichtextstring ("Validation: XXX Audit: XXX summary: XX"));   
      Footrowcell.setcellstyle (Centerstyle);   
      Range = new Cellrangeaddress (Footrownumber + 1, footrownumber + 1, 0, 7); Sheet.addmergedregion (Range);   
      HttpServletResponse response = GetResponse ();   
      HttpServletRequest request = Getrequest (); String filename = "unnamed. xls";//Set the name of the client Excel on download//See: http://zmx.iteye.com/blog/622529 filename = util.en   
      Codefilename (filename, request);   
      Response.setcontenttype ("application/vnd.ms-excel");   
      Response.setheader ("Content-disposition", "attachment;filename=" + filename);   
      OutputStream Ouputstream = Response.getoutputstream ();   
      Workbook.write (Ouputstream);   
      Ouputstream.flush ();   
  
    Ouputstream.close ();   
    catch (Exception e) {e.printstacktrace ();   
  return null;   }


Another example:

In a JSP page there is a button for export Excel button

<button onclick = window.open ('.. /toexcel/yuyingeverymtoexcel.jsp?yearn=<%yearn%>&monthn=<%monthn%>&typesql=<%typesql% >&num=math.random () > Exported to excel</button>


And then the yunyingeverymtoexcel.jsp.

<%@ page contenttype= "application/vnd.ms-excel; Charset=utf-8 "
	

This is to be changed.

The following is named as an export.

String name = new String ("Tab 3". GetBytes (), "iso8859-1");
	Response.setheader ("Content-disposition",
			"attachment; filename=" +name+time+ ". xls");


The following is the export of Excel.

Yunyingeverymtoexcel we = new Yunyingeverymtoexcel ();
		We.getexcel ("111.xls", Response.getoutputstream (), yearn,monthn);


Here are some of the code in Yunyingeverymtoexcel java.

Formatting cells

		
		
		Set Cell Center
		hssfcellstyle cs0 = Wb.createcellstyle ();
		Cs0.setalignment (Hssfcellstyle.align_center); Center 
		cs0.setverticalalignment (Hssfcellstyle.vertical_center);
		

Set Cell Center plus border

Hssfcellstyle cs = Wb.createcellstyle ();
		Cs.setalignment (Hssfcellstyle.align_center); Center 
		cs.setverticalalignment (hssfcellstyle.vertical_center);
		Cs.setbordertop (Hssfcellstyle.border_thin);
		Cs.setleftbordercolor (HSSFColor.BLACK.index);
		Cs.setborderleft ((short) 1);   
		Cs.setrightbordercolor (HSSFColor.BLACK.index);   
		Cs.setborderright ((short) 1);   
		Cs.setborderbottom (Hssfcellstyle.border_thin); Set the border of a cell to bold   

Set font

Set font
		//title 1
        hssffont font = Wb.createfont ();
        Font.setfontheightinpoints ((short) 20); Font height 
    //    Font.setcolor (Hssffont.color_black);//Font Color 
        font.setfontname ("XXFarEastFont-Arial");//Font 
        Font.setboldweight (Hssffont.boldweight_bold); Width 
       //Font.setitalic (TRUE);//whether to use italic


Cell Assignment and formatting

Set font
		//title 1
        hssffont font = Wb.createfont ();
        Font.setfontheightinpoints ((short) 20); Font height 
    //    Font.setcolor (Hssffont.color_black);//Font Color 
        font.setfontname ("XXFarEastFont-Arial");//Font 
        Font.setboldweight (Hssffont.boldweight_bold); Width 
       //Font.setitalic (TRUE);//whether to use italic





Related Article

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.