Java operation Excel, Word

xiaoxiao2021-03-06  39

I listened to my friends today, I learned an open source project in Poi, Apache, provided a pure Java solution for Microsoft Excel, Word, http://jakarta.apache.org/poi/ Can be downloaded to the development package, and Example procedures, it is still very convenient! Below is a program that exports Oracle database tables as Excel: import java.io.fileoutputstream; import org.apache.poi.hssf.usermodel.hssfcell; // cell import org.apache.poi.hssf.userModel.hssfcellstyle; // Universal 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 ; // head import org.apache.poi.hssf.usermodel.hssfrow; // line import org.apache.poi.hssf.usermodel.hssfsheet; // A Noteping IMPORT ORG.APACHE.POI.HSSF.USERMODEL. HSSFWORKBOOK; // an Excelimport 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

转载请注明原文地址:https://www.9cbs.com/read-78215.html

New Post(0)