1. 程式人生 > >利用poi生成excell檔案

利用poi生成excell檔案

首先建立一個實體類與資料庫查詢出來的欄位對應
package entity;
/**
* 學生實體類
* @author Administrator
*
*/
public class Student {
private int id;
private String name;
private String sex;
private int age;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(int id, String name, String sex, int age) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public Student() {
super();
}

}
第二步建立一個利用jdbc連線資料庫的工具 類
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Student;

/**
* 連線資料庫的類
* @author Administrator
*
*/
public class StudentDao {
public List queryStudentList(){
List list=new ArrayList();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String url=”jdbc:oracle:thin:@localhost:1521:xe”;
String user=”liujing”;
String password=”liujing”;
String sql=”select id,name,sex,age from student”;
try {
//載入驅動
Class.forName(“oracle.jdbc.driver.OracleDriver”);
//獲得連線
conn=DriverManager.getConnection(url,user,password);
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
Student stu=null;
while(rs.next()){
stu=new Student();
stu.setId(rs.getInt(“id”));
stu.setName(rs.getString(“name”));
stu.setSex(rs.getString(“sex”));
stu.setAge(rs.getInt(“age”));
list.add(stu);
}

} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}finally{
    if(rs!=null){
        try {
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(ps!=null){
        try {
            ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(conn!=null){
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
     return list;
}

}
}
第三部生成excell檔案的工具類
package util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import entity.Student;

/**
* 生成excell檔案的工具類
* @author Administrator
*
*/
public class ExcellUtil {
public static void createExcell(OutputStream out,List list,String title,String[] headers){
//建立一個WorkBook
HSSFWorkbook workbook=new HSSFWorkbook();
//建立一個sheet頁
HSSFSheet sheet=workbook.createSheet(title);
//設定sheet的預設列寬
sheet.setDefaultColumnWidth(14);
//設定標題樣式
HSSFCellStyle titleStyle=workbook.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設定標題的字型
HSSFFont font=workbook.createFont();
font.setBoldweight((short)10);
titleStyle.setFont(font);
//產生標題行
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell(0);
cell.setCellStyle(titleStyle);
cell.setCellValue(title);
//合併單元格
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
row=sheet.createRow(1);
for(int i=0;i