一、直接导出
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
二、导出数据库的数据
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包