/**
* Generate EXCEL files in multiple formats
**/
Package multistruct;
Import java. Io. fileoutputstream;
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 poi_multistruct
{
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs)
{
Try
{
// Create a blank workbook
Hssfworkbook WB = new hssfworkbook ();
// Create a sheet for this workbook based on the previous workbook
Hssfsheet ST = WB. createsheet ();
WB. setsheetname (0, "test page", hssfworkbook. encoding_utf_16 );
Hssfcellstyle normalstyle = createnormalstyle (WB );
// Merge cell A1-C1, fill in the content when added to the first grid can be
// But note: after merging cells, the border is only valid for the original first one,
// If You Want To apply the merged whole, you need to create cells one by one and apply the style
St. addmergedregion (new region (0, (short) 0, 0, (short) 2); // three columns in a row
Hssfrow ROW = ST. createrow (0 );
Hssfcell cell = row. createcell (short) 0 );
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellvalue ("Business List ");
// Cell. setcellvalue ("Business List"); // Business list
Cell. setcellstyle (normalstyle );
Row. createcell (short) 1). setcellstyle (normalstyle );
Row. createcell (short) 2). setcellstyle (normalstyle );
// Set the column header. Of course, you can write the column headers one by one. It seems a bit clever to use loops.
// Similarly, you must apply a style object to each cell separately.
String [] seasonname = {"first quarter", "second quarter", "third quarter", "fourth quarter "};
// String [] seasonname = {"the first quarter", "the second quarter", "the third quarter", "The fourth quarter "};
For (short I = 3; I <7; I ++)
{
Cell = row. createcell (I );
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellvalue (seasonname [I-3]);
Cell. setcellstyle (normalstyle );
}
// This is the implementation of vertical lines of text
// Currently, poi has not found a way (maybe not provided, maybe I am ignorant) to vertically arrange text,
// The hssfcellstyle. setrotation () method sets the rotation angle, which is not the same as the vertical bar.
St. addmergedregion (new region (1, (short) 0, 6, (short) 0 ));
Row = ST. createrow (1 );
Cell = row. createcell (short) 0 );
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellvalue ("region/n ");
// Cell. setcellvalue ("area/nlocation/nproxy/NER/N ");
Cell. setcellstyle (normalstyle );
For (INT I = 2; I <7; I ++)
{
St. createrow (I). createcell (short) 0). setcellstyle (normalstyle );
}
// The second-level classification of the region. The two adjacent cells are vertically merged.
String [] shopname = {"chain store a", "Chain Store B", "Chain Store C "};
// String [] shopname = {"chain store a", "Chain Store B", "Chain Store C "};
For (INT I = 1; I <6; I = I + 2)
{
Row = ST. createrow (I );
Cell = row. createcell (short) 1 );
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellvalue (shopname [(I-1)/2]);
Cell. setcellstyle (normalstyle );
St. createrow (I + 1). createcell (short) 1). setcellstyle (normalstyle );
St. addmergedregion (new region (I, (short) 1, I + 1, (short) 1 ));
}
// Belongs to the next level of the chain store, which is basically created and assigned a value + application Style
For (INT I = 1; I <7; I = I + 2)
{
Cell = ST. getrow (I). createcell (short) 2 );
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellvalue ("revenue ");
// Cell. setcellvalue ("income ");
Cell. setcellstyle (normalstyle );
Cell = ST. getrow (I + 1). createcell (short) 2 );
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellvalue ("expenditure ");
// Cell. setcellvalue ("Expenses ");
Cell. setcellstyle (normalstyle );
}
// For the data part, directly create and apply the style. If there is data, the data will be written here.
For (INT I = 1; I <7; I ++)
{
For (short J = 3; j <7; j ++)
{
Cell = ST. createrow (I). createcell (j );
Cell. setcellstyle (normalstyle );
Cell. setcellvalue (I * j );
}
}
// Freeze the Excel window. The boundary is the boundary of the Data part.
St. createfreezepane (3, 1 );
// Group by chain stores
For (INT I = 1; I <7; I = I + 2)
{
St. grouprow (I, I );
}
// Group by region
St. grouprow (1, 5 );
//--
Fileoutputstream writefile = new fileoutputstream ("D: // poi_export // multistructexcel.xls ");
// Output
WB. Write (writefile );
Writefile. Close ();
}
Catch (exception ex)
{
Ex. printstacktrace ();
}
}
Public static hssfcellstyle createnormalstyle (hssfworkbook WB)
{
Hssfcellstyle normalstyle = WB. createcellstyle ();
// Create a cell style object, and set the surrounding border and font to wrap.
// The text line feed is used to display one of the cells vertically.
// A better way is to create a separate cell style object.
// Otherwise, it may be a small problem when processing the automatic column width.
Normalstyle. setborderleft (hssfcellstyle. border_thin );
Normalstyle. setbordertop (hssfcellstyle. border_thin );
Normalstyle. setborderright (hssfcellstyle. border_thin );
Normalstyle. setborderbottom (hssfcellstyle. border_thin );
Normalstyle. setalignment (hssfcellstyle. align_center );
Normalstyle. setverticalalignment (hssfcellstyle. vertical_center );
Normalstyle. setwraptext (true );
Return normalstyle;
}
}