java导出数据到excel里:直接导出和导出数据库数据

时间:2022-01-10 19:45:40

一、直接导出

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成功!");
}
}

java导出数据到excel里:直接导出和导出数据库数据

java导出数据到excel里:直接导出和导出数据库数据

二、导出数据库的数据

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();
}
}

java导出数据到excel里:直接导出和导出数据库数据

添加列名

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();
}
}

java导出数据到excel里:直接导出和导出数据库数据

java导出数据到excel里:直接导出和导出数据库数据

附jar包

java导出数据到excel里:直接导出和导出数据库数据