Today, I learned about poi, an open-source project organized by Apache, and provided pure Java for Microsoft Excel and Word.
Solution, the http://jakarta.apache.org/poi/ can be downloaded to the Development Kit, there are examples of programs, It is very convenient to use!
The following is a program for exporting Oracle database tables to excel:
Import java. Io. fileoutputstream;
Import org. Apache. Poi. hssf. usermodel. hssfcell; // Cell
Import org. Apache. Poi. hssf. usermodel. hssfcellstyle; // cell style
Import org. Apache. Poi. hssf. usermodel. hssfdataformat; // data format
Import org. Apache. Poi. hssf. usermodel. hssffont; // font
Import org. Apache. Poi. hssf. usermodel. hssfheader; // Header
Import org. Apache. Poi. hssf. usermodel. hssfrow; // line
Import org. Apache. Poi. hssf. usermodel. hssfsheet; // a notebook
Import org. Apache. Poi. hssf. usermodel. hssfworkbook; // an Excel file
Import org. Apache. Poi. hssf. util. hssfcolor; // color
Import java. SQL .*;
Public class test {
Public static void main (string [] ARGs) throws exception
{
String filename = "dept.xls ";
String column_name = "";
Preparedstatement PS = NULL; // pre-compiled
Connection conn = NULL; // connection
Statement SMT = NULL; // SQL statement
Resultset rs = NULL, RS1 = NULL; // result set
Resultsetmetadata MD = NULL;
Long rowcount = 0l; // total number of rows
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
} Catch (exception e ){
E. printstacktrace ();
}
String url = "JDBC: oracle: thin: @ 172.18.2.114: 1521: Mis ";
String username = "Scott ";
String Password = "tiger ";
Conn = drivermanager. getconnection (URL, username, password );
SMT = conn. createstatement ();
// Obtain the total number of records
String SQL = "select deptno, dname, LOC from Dept ";
String sqlqeury = "select count (*) as total from (" + SQL + ")";
Try {
Rs = smt.exe cutequery (sqlqeury );
If (Rs. Next ()){
Rowcount = Rs. getlong ("Total ");
System. Out. Print (rowcount); // total number of records
}
} Catch (exception e ){
System. Out. Print ("AAA ");
E. printstacktrace ();
System. Out. println (E. getmessage ());
Conn. Close (); // close the connection after an exception
Throw new exception ("failed to get the total number of records ");
}
// Process SQL statements
Try {
RS1 = smt.exe cutequery (SQL );
MD = rs1.getmetadata ();
System. Out. Print (Md. getcolumncount ());
} Catch (exception e ){
System. Out. Print ("BBB ");
E. printstacktrace ();
System. Out. println (E. getmessage ());
Conn. Close (); // close the connection after an exception
Throw new exception ("input SQL cannot be processed ");
}
// Generate EXCEL code
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet = WB. createsheet ("new sheet ");
Hssfrow ROW = NULL;
Hssfcell cell = NULL;
For (long I = 0; I <= rowcount; I ++ ){
Row = sheet. createrow (short) I); // create a row
For (Int J = 0; j <Md. getcolumncount (); j ++ ){
Cell = row. createcell (short) J); // create a cell
Cell. setencoding (hssfcell. encoding_utf_16 );
If (I = 0 ){
Cell. setcellvalue (Md. getcolumnname (J + 1); // set the cell value.
} Else {
Cell. setcellvalue (rs1.getstring (J + 1); // set the cell value
}
}
Rs1.next ();
}
// Write it to excel!
Fileoutputstream fileout = new fileoutputstream (filename );
WB. Write (fileout); // write to the fileout File
Fileout. Close (); // close the file output stream
Conn. Close (); // close the normal connection
}
}
2004.12.22
Ladofwind@163.com