一、直接導出 二、導出資料庫的數據 添加列名 附jar包 ...
一、直接導出
package com.ij34.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * @author Admin * @date 創建時間:2017年8月30日 上午11:49:42 * @version 1.0 *@type_name Test03 */ public class Test03 { public static void main(String[] args) throws FileNotFoundException, IOException { // TODO Auto-generated method stub HSSFWorkbook wb= new HSSFWorkbook(); HSSFSheet sheet=wb.createSheet("table04"); Object[][] datas={{"ID","Name","age"},{1,"大B",18},{2,"小A",19},{3,"小C",21}}; HSSFRow row; HSSFCell cell; for(int i=0;i<datas.length;i++){ row=sheet.createRow(i); for(int j=0;j<datas[i].length;j++){ cell=row.createCell(j); cell.setCellValue(String.valueOf(datas[i][j])); } } wb.write(new FileOutputStream("table04.xls")); System.out.println("導出xls成功!"); } }
二、導出資料庫的數據
package com.ij34.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.NamingException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * @author Admin * @date 創建時間:2017年8月29日 上午11:35:28 * @version 1.0 *@type_name Test01 *從資料庫里導出Excel文件 */ public class Test01 { public static void main(String[] args) throws FileNotFoundException, IOException, SQLException, NamingException, ClassNotFoundException { Class.forName("oracle.jdbc.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "lyx", "123456"); String sqlStr ="select * from student"; Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(sqlStr); HSSFWorkbook wb= new HSSFWorkbook(); HSSFSheet sheet=wb.createSheet("table03"); HSSFRow row; int i=0; HSSFCell cell; while(rs.next()){ row=sheet.createRow(i); cell=row.createCell(0); //Excel里第一(A)列 cell.setCellValue(String.valueOf(rs.getInt(1))); cell=row.createCell(1); cell.setCellValue(String.valueOf(rs.getString(2))); cell=row.createCell(2); cell.setCellValue(String.valueOf(rs.getInt(3))); cell=row.createCell(3); cell.setCellValue(String.valueOf(rs.getInt(4))); cell=row.createCell(4); cell.setCellValue(String.valueOf(rs.getString(5))); i++; } wb.write(new FileOutputStream("table03.xls")); System.out.println("導出xls成功!"); rs.close(); stmt.close(); conn.close(); } }
添加列名
package com.ij34.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.NamingException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * @author Admin * @date 創建時間:2017年8月29日 上午11:35:28 * @version 1.0 *@type_name Test01 *從資料庫里導出Excel文件 */ public class Test01 { public static void main(String[] args) throws FileNotFoundException, IOException, SQLException, NamingException, ClassNotFoundException { Class.forName("oracle.jdbc.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "lyx", "123456"); String sqlStr ="select * from student"; Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(sqlStr); HSSFWorkbook wb= new HSSFWorkbook(); HSSFSheet sheet=wb.createSheet("table03"); HSSFRow row; int i=0; HSSFCell cell; //列名標柱 row=sheet.createRow(i); cell=row.createCell(0); cell.setCellValue("序號"); cell=row.createCell(1); cell.setCellValue("名字"); cell=row.createCell(2); cell.setCellValue("性別0|1"); cell=row.createCell(3); cell.setCellValue("年齡"); cell=row.createCell(4); cell.setCellValue("地址"); while(rs.next()){ i++; row=sheet.createRow(i); cell=row.createCell(0); cell.setCellValue(String.valueOf(rs.getInt(1))); cell=row.createCell(1); cell.setCellValue(String.valueOf(rs.getString(2))); cell=row.createCell(2); cell.setCellValue(String.valueOf(rs.getInt(3))); cell=row.createCell(3); cell.setCellValue(String.valueOf(rs.getInt(4))); cell=row.createCell(4); cell.setCellValue(String.valueOf(rs.getString(5))); } wb.write(new FileOutputStream("table03.xls")); System.out.println("導出xls成功!"); rs.close(); stmt.close(); conn.close(); } }
附jar包