创建一个测试类(PersonDao.java)
package org.poi.excel.dao;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
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;
import org.html.model.PersonModel;
import org.html.model.Utils;
public class PersonDao {
public static List<PersonModel> QueryPerson(){
Connection con =null;
Statement st =null;
ResultSet rs =null;
String sql ="select * from persons";
con =Utils.getConnection();
List<PersonModel> list =new ArrayList<PersonModel>();
try {
st =con.createStatement();
rs =st.executeQuery(sql);
while(rs.next()){
PersonModel model =new PersonModel();
model.setId(rs.getInt(1));
model.setName(rs.getString(2));
list.add(model);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
Utils.closeDB(rs, st, con);
}
return list;
}
public void CreateExcel(){
//创建一个工作簿
HSSFWorkbook workBook =new HSSFWorkbook();
//创建一个工作表,名为:第一页
HSSFSheet sheet =workBook.createSheet("第一页");
//设置单元格的宽度(0:表示第一行的第一个单元格,1:第一行的第二个单元格)
sheet.setColumnWidth((short)0, 2500);
sheet.setColumnWidth((short)1, 5000);
//创建一个单元格,从0开始
HSSFRow row =sheet.createRow((short)0);
//构造一个数组设置第一行之后的单元格
HSSFCell cell[] =new HSSFCell[2];
for(int i=0 ;i <2 ;i++){
cell[i] =row.createCell(i);
}
cell[0].setCellValue("ID");
cell[1].setCellValue("Name");
//获得从数据库中查询出来的数据
List<PersonModel> list=PersonDao.QueryPerson();
if(list !=null && list.size() >0){
//循环list中的数据
for(int i =0;i<list.size();i++){
PersonModel model =list.get(i);
HSSFRow dataRow =sheet.createRow(i +1);
HSSFCell data[] =new HSSFCell[2];
for(int j= 0;j < 2 ; j++){
data[j] =dataRow.createCell(j);
}
data[0].setCellValue(model.getId());
data[1].setCellValue(model.getName());
try {
//输出成XLS文件
File file =new File("D://person.xls");
FileOutputStream fos = new FileOutputStream(file);
//写入数据,并关闭文件
workBook.write(fos);
fos.close();
/*
//将工作薄输出到输出流
ServletOutputStream sos =response.getOutputStream();
workBook.write(sos);
fos.close();
*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]){
//测试
PersonDao dao =new PersonDao();
dao.CreateExcel();
}
}
package org.poi.excel.dao;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
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;
import org.html.model.PersonModel;
import org.html.model.Utils;
public class PersonDao {
public static List<PersonModel> QueryPerson(){
Connection con =null;
Statement st =null;
ResultSet rs =null;
String sql ="select * from persons";
con =Utils.getConnection();
List<PersonModel> list =new ArrayList<PersonModel>();
try {
st =con.createStatement();
rs =st.executeQuery(sql);
while(rs.next()){
PersonModel model =new PersonModel();
model.setId(rs.getInt(1));
model.setName(rs.getString(2));
list.add(model);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
Utils.closeDB(rs, st, con);
}
return list;
}
public void CreateExcel(){
//创建一个工作簿
HSSFWorkbook workBook =new HSSFWorkbook();
//创建一个工作表,名为:第一页
HSSFSheet sheet =workBook.createSheet("第一页");
//设置单元格的宽度(0:表示第一行的第一个单元格,1:第一行的第二个单元格)
sheet.setColumnWidth((short)0, 2500);
sheet.setColumnWidth((short)1, 5000);
//创建一个单元格,从0开始
HSSFRow row =sheet.createRow((short)0);
//构造一个数组设置第一行之后的单元格
HSSFCell cell[] =new HSSFCell[2];
for(int i=0 ;i <2 ;i++){
cell[i] =row.createCell(i);
}
cell[0].setCellValue("ID");
cell[1].setCellValue("Name");
//获得从数据库中查询出来的数据
List<PersonModel> list=PersonDao.QueryPerson();
if(list !=null && list.size() >0){
//循环list中的数据
for(int i =0;i<list.size();i++){
PersonModel model =list.get(i);
HSSFRow dataRow =sheet.createRow(i +1);
HSSFCell data[] =new HSSFCell[2];
for(int j= 0;j < 2 ; j++){
data[j] =dataRow.createCell(j);
}
data[0].setCellValue(model.getId());
data[1].setCellValue(model.getName());
try {
//输出成XLS文件
File file =new File("D://person.xls");
FileOutputStream fos = new FileOutputStream(file);
//写入数据,并关闭文件
workBook.write(fos);
fos.close();
/*
//将工作薄输出到输出流
ServletOutputStream sos =response.getOutputStream();
workBook.write(sos);
fos.close();
*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]){
//测试
PersonDao dao =new PersonDao();
dao.CreateExcel();
}
}