Java dynamic Export Excel merged cells

Source: Internet
Author: User

The use of POI technology

Frame-type SSH is using the Struts1

public void Outexcel (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {

Your program needs to call something written here

try {

The first step is to create a webbook that corresponds to an Excel file

Hssfworkbook wb = new Hssfworkbook ();

In the second step, add a sheet in WebBook that corresponds to the sheet in the Excel file

Hssfsheet sheet = wb.createsheet ("name");

In the third step, add the No. 0 row of the table header to the sheet, noting that the old version of POI has a limit on the number of rows in Excel short

Hssfrow row = Sheet.createrow ((int) 0);

Fourth step, create a cell, and set the value header to center the header

Hssfcellstyle style = Wb.createcellstyle (); Style.setalignment (Hssfcellstyle.align_center);

Create a center format

Hssfcell cell = Row.createcell ((short) 0);

Cell.setcellvalue ("other");

Cell.setcellstyle (style);

Cell = Row.createcell ((short) 1);

Cell.setcellvalue ("other fetch");

Your program needs to call something written here

for (int i = 0; i < list1.size (); i++) {
for (int p = 0; p < list.size (); p++) {
if (List1.get (i). Equals (List.get (P). GETSSQX ())) {
TRACELXR sf=new TRACELXR ();
SF.SETSSQX (List.get (P). GETSSQX ());
SF.SETFG (List.get (P). GETFG ());

Newlist.add (SF);

}

}

}

for (int k1 = 0; k1 < newlist.size (); k1++) {
row = Sheet.createrow ((int) k1 + 1);
TRACELXR bgmultilevel = (TRACELXR) newlist.get (K1);

Row.createcell ((short) 0). Setcellvalue (BGMULTILEVEL.GETSSQX ());
Row.createcell ((short) 1). Setcellvalue (BGMULTILEVEL.GETFG ());

if (k1!=0) {

if (Newlist.get (k1-1). GETSSQX (). Equals (Newlist.get (K1). GETSSQX ())) {

Cellrangeaddress cra=new cellrangeaddress (K1, 1+k1, 0, 0);

Add merged cells in sheet

Sheet.addmergedregion (CRA);

}

}

}

Response.reset ();

Response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8");

Response.setheader ("Content-disposition", "attachment;filename=" + New String (("Exportexcel". ToString () + ". x ls "). GetBytes ()," iso-8859-1 "));

OutputStream OS = Response.getoutputstream ();

Wb.write (OS); Os.close ();

}catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

It's good to have a dynamic merge here. The method of invocation is written according to its own

Java dynamically export Excel merged cells

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.