Poi processing Excel

Source: Internet
Author: User

Package com. Test. Service;

Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. util. List;
Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import org. Apache. Poi. hssf. usermodel. hssfcellstyle;
Import org. Apache. Poi. hssf. usermodel. hssffont;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Import org. Apache. Poi. hssf. usermodel. hssfworkbook;
Import org. hibernate. query;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. Classic. Session;
Import org. springframework. Context. Support. classpathxmlapplicationcontext;

Import com. Test. model. excle;

Public class createexcel {
 
Public static void main (string [] ARGs ){
Classpathxmlapplicationcontext CTX = new classpathxmlapplicationcontext ("applicationcontext. xml ");
Sessionfactory Sf = (sessionfactory) CTX. getbean ("sessionfactory ");
Session session = SF. opensession ();
Transaction Tx = session. begintransaction ();
Query query = session. createsqlquery ("select * From excle ");
List list = query. List (); // list has 5 arrays.
New createexcel (). createexcel (list );
// New createexcel (). readexcel ("D: // test.xls ");

}

/**
* List of collections obtained from database queries
**/
Public void createexcel (list excellist ){
Fileoutputstream Fos = NULL;
Hssfworkbook WB = new hssfworkbook ();
// Create a sheet
Hssfsheet sheet = WB. createsheet (" ");
Hssfrow ROW = sheet. createrow (0 );
Row. createcell (short) 0). setcellvalue ("Serial Number ");
Row. createcell (short) 1). setcellvalue ("gender ");
Row. createcell (short) 2). setcellvalue ("Age ");
Row. createcell (short) 3). setcellvalue ("Experience Value ");
Row. createcell (short) 4). setcellvalue ("basic salary ");
For (INT I = 0; I <excellist. Size (); I ++ ){
Object OBJ [] = (object []) excellist. Get (I );
Hssfrow rows = sheet. createrow (I + 1 );
Rows. createcell (short) 0). setcellvalue (OBJ [0]. tostring ());
Rows. createcell (short) 1). setcellvalue (OBJ [1]. tostring ());
Rows. createcell (short) 2). setcellvalue (OBJ [2]. tostring ());
Rows. createcell (short) 3). setcellvalue (OBJ [3]. tostring ());
Rows. createcell (short) 4). setcellvalue (OBJ [4]. tostring ());

}
Fileoutputstream fout;
Try {
Fout = new fileoutputstream ("D: // test.xls ");
WB. Write (fout );
Fout. Flush ();
Fout. Close ();
} Catch (filenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}
/**
* Reading Excel files
**/
Public void readexcel (string excelpath ){
Try {
Hssfworkbook workbook = new hssfworkbook (New fileinputstream (excelpath ));
// Obtain the reference of a table
Hssfsheet sheet = Workbook. getsheet (" ");
Hssfrow ROW = sheet. getrow (1 );
Hssfcell cell = row. getcell (0 );
System. Out. println ("upper left unit =" + cell. getstringcellvalue ());
} Catch (filenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
 
/**
* Create and set the font and cell formats, and then apply these formats:
**/
Public void excelstyle (){
Hssfworkbook WB = new hssfworkbook ();
// Create a font and set it to red or bold:
Hssffont font = WB. createfont ();
Font. setcolor (hssffont. color_red );
Font. setboldweight (hssffont. boldweight_bold );
// Format
Hssfcellstyle cellstyle = WB. createcellstyle ();
Cellstyle. setfont (font );
// Application format
Hssfsheet SH = WB. createsheet ("SS ");
Hssfrow ROW = Sh. createrow (0 );
Hssfcell cell = row. createcell (0 );
Cell. setcellstyle (cellstyle );
Cell. setcelltype (hssfcell. cell_type_string );
Cell. setcellvalue ("title ");
}
}

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.