I listened to my friends today, I learned an open source project of Poi, Apache, provided pure Java for Microsoft Excel, Word.
solution,
Http://jakarta.apache.org/poi/ can be downloaded to the development kit, and there is an example program, it is still very convenient!
Below is a program that exports the Oracle database table as an Excel:
Import java.io.fileoutputstream;
Import org.apache.poi.hssf.usermodel.hssfcell; // cell
Import org.apache.poi.hssf.usermodel.hssfcellstyle; // Universal pattern
Import org.apache.poi.hssf.usermodel.hssfdataFormat; // Data format
Import org.apache.poi.hssf.usermodel.hssffont; // font
Import org.apache.poi.hssf.usermodel.hssfheader; //
Import org.apache.poi.hssf.usermodel.hssfrow; //
Import org.apache.poi.hssf.usermodel.hssfsheet; // A Notepad
Import org.apache.poi.hssf.usermodel.hssfworkbook; // an Excel
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; // precompiled Connection conn = null; // connected Statement SMT = NULL; // SQL statement resultset = null, rs1 = null; / / result collection resultTMetAdata MD = NULL; long rowcount = 0L; // Total number 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 (); // Get a total record number String SQL = "SELECT Deptno, DNAME, LOC from DEPT"; string sqlqeury = "SELECT COUNT (*) As Total from (" SQL ") "; try {r = SMT.executeQuery (SQLQEU RY); if (rs.next ()) {rowcount = rs.getlong ("total"); system.out.print (rowcount); // Total number}} catch (Exception E) {system.Print "AAA"); E.PrintStackTrace (); system.out.println (E.GETMESSAGE ()); conn.close (); // After abnormal closing connection throw new exception ("Get Total Record Total Failure");} / / Processing SQL statement try {RS1 = SMT.executeQuery (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 (); // Reconnect Throw New Exception ("Inferred SQL Unable Process");} // Generate Excel Code HSSFWorkbook WB = New HssfworkBook (); hssfsheet sheet = wb.createsheet ("New Sheet "); Hssfrow rot = null; hssfcell cell = null; for (long (long i = 0; i <= rowcount; i ) {row = sheet.createrow (short); // Create a line for (int J = 0; j