Java Export Excel (complex case)

Source: Internet
Author: User

Import Java.io.FileOutputStream;
Import java.io.IOException;

Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import org.apache.poi.hssf.util.Region;

public class Exceltest {

/**
* @param args
*/
@SuppressWarnings ("deprecation")
public static void Main (string[] args) throws IOException {

try {
Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet sheet = wb.createsheet ("new sheet");
Hssfcellstyle style = Wb.createcellstyle (); Style object

Style.setverticalalignment (hssfcellstyle.vertical_center);//Vertical
Style.setalignment (hssfcellstyle.align_center);//Level
Hssfrow row = Sheet.createrow ((short) 0);
Hssfrow row2 = Sheet.createrow ((short) 1);

Sheet.addmergedregion (New Region (0, (short) 0, 2, (short) 0));
Hssfcell CE = Row.createcell ((short) 0);
Ce.setencoding (hssfcell.encoding_utf_16);//Chinese processing
Ce.setcellvalue ("Project \ Date"); Data displayed in the first column of the first row of the table
Ce.setcellstyle (style); Style, centering
int num = 0;
for (int i = 0; i < 9; i++) {//Loop 9 times, each time cross-cell display
Calculate the span from that cell to the grid
int Celln = 0;
int celle = 0;
if (i = = 0) {
Celln = 0;
Celle = 1;
} else {
Celln = (i * 2);
Celle = (I * 2 + 1);
}
Cell merging
The four parameters are: Start line, start column, end row, end column
Sheet.addmergedregion (New Region (0, (short) (Celln + 1), 0,
(short) (celle + 1)));
Hssfcell cell = Row.createcell ((short) (Celln + 1));
Cell.setcellvalue ("merging" + i); Data that is displayed across cells
Cell.setcellstyle (style); Style
Data that is not displayed across cells, such as: Two lines, one for each row, and two for the next line, quantity, amount
Hssfcell cell1 = Row2.createcell ((short) celle);
Hssfcell cell2 = Row2.createcell ((short) (Celle + 1));
Cell1.setencoding (HSSFCELL.ENCODING_UTF_16);
Cell1.setcellvalue ("Quantity");
Cell1.setcellstyle (style);
Cell2.setencoding (HSSFCELL.ENCODING_UTF_16);
Cell2.setcellvalue ("Amount");
Cell2.setcellstyle (style);
num++;
}

Add a percent of the total behind

//Totals are added last, also across a cell
Sheet.addmergedregion (new region (0, (short) (2 * num + 1), 0,
(short) (2 * num + 2));
Hssfcell cell = Row.createcell ((short) (2 * num + 1));
//Cell.setencoding (HSSFCELL.ENCODING_UTF_16);
Cell.setcellvalue ("Total");
Cell.setcellstyle (style);
Hssfcell cell1 = Row2.createcell ((short) (2 * num + 1));
Hssfcell cell2 = Row2.createcell ((short) (2 * num + 2));
//Cell1.setencoding (HSSFCELL.ENCODING_UTF_16);
Cell1.setcellvalue ("Quantity");
Cell1.setcellstyle (style);
//Cell2.setencoding (HSSFCELL.ENCODING_UTF_16);
Cell2.setcellvalue ("Amount");
Cell2.setcellstyle (style);

Percent ibid.
Sheet.addmergedregion (New Region (0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
Hssfcell cellb = Row.createcell ((short) (2 * num + 3));
Cellb.setencoding (HSSFCELL.ENCODING_UTF_16);
Cellb.setcellvalue ("percentage");
Cellb.setcellstyle (style);
Hssfcell CELLB1 = Row2.createcell ((short) (2 * num + 3));
Hssfcell cellb2 = Row2.createcell ((short) (2 * num + 4));
Cellb1.setencoding (HSSFCELL.ENCODING_UTF_16);
Cellb1.setcellvalue ("Quantity");
Cellb1.setcellstyle (style);
Cellb2.setencoding (HSSFCELL.ENCODING_UTF_16);
Cellb2.setcellvalue ("Amount");
Cellb2.setcellstyle (style);

FileOutputStream fileout = new FileOutputStream ("Workbook.xls");
Wb.write (fileout);
Fileout.close ();
System.out.print ("OK");
} catch (Exception ex) {
Ex.printstacktrace ();
}

}

}

======================================================

Jar Package Used

Poi-3.8.jar
Poi-examples-3.8.jar
Poi-excelant-3.8.jar
Poi-ooxml-3.8.jar
Poi-ooxml-schemas-3.8.jar
Poi-scratchpad-3.8.jar

======================================================

Effect

Java Export Excel (complex case)

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.