使用Apache POI建立Excel,並從數據庫中讀取數據寫入到Excel文件中html
介紹:java
Apache POI是Apache軟件基金會的開放源碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。mysql
結構:sql
HSSF - 提供讀寫Microsoft Excel格式檔案的功能。數據庫
XSSF - 提供讀寫Microsoft Excel OOXML格式檔案的功能。apache
HWPF - 提供讀寫Microsoft Word格式檔案的功能。數組
HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。測試
HDGF - 提供讀寫Microsoft Visio格式檔案的功能。this
詳情:http://poi.apache.org/ url
步驟:
數據庫字段
建立一個鏈接數據庫(UtilsUtils.java)
package org.html.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Utils {
private static Connection con;
private static String Driver ="com.mysql.jdbc.Driver";
private static String url ="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf8";
private static String username ="root";
private static String password ="123";
public static Connection getConnection(){
try {
Class.forName(Driver);
con =DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void closeDB(ResultSet rs ,Statement st ,Connection con){
try {
if(rs !=null){
rs.close();
rs =null;
}
if(st !=null){
st.close();
st =null;
}
if(con !=null){
con.close();
con =null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
建立一個Model(PersonModel.java)
public class PersonModel {
private int id ;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
建立一個測試類(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();
}
}
來自 <http://www.cnblogs.com/kanjingcai/archive/2010/12/01/1893603.html>