Java 利用poi把資料庫中資料匯入Excel
效果:
使用時先把poi包匯入工程的path,注意只需要匯入poi包即可,下載後有三個jar包
核心代碼:
串連資料庫:DBConnection.java
複製代碼 代碼如下:
package org.xg.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection {
private final String DBUrl ="jdbc:mysql://localhost:3306/notebook" ;
private final String DBDriver ="com.mysql.jdbc.Driver" ;
private final String username ="root" ;
private final String password ="riskfitfeng" ;
private Connection con ;
public DBConnection()
{
try {
Class.forName(DBDriver) ;
con = DriverManager.getConnection(DBUrl,username,password) ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getDB()
{
return con ;
}
public void closeDb(ResultSet rs,PreparedStatement ps)
{
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();
}
}
}
}
匯入excel類:MySql2Excel.java
package org.xg.db;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class MySql2Excel {
public MySql2Excel() throws Exception
{
Connection con = null ;
DBConnection db = new DBConnection() ;
con = db.getDB() ;
String sql ="select * from students" ;
ResultSet rs = con.createStatement().executeQuery(sql) ;
// 擷取總列數
int CountColumnNum = rs.getMetaData().getColumnCount() ;
int i =1 ;
// 建立Excel文檔
HSSFWorkbook wb = new HSSFWorkbook() ;
// sheet 對應一個工作頁
HSSFSheet sheet = wb.createSheet("student表中的資料") ;
HSSFRow firstrow = sheet.createRow(0); //下標為0的行開始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] ="ID";
names[1] ="學號";
names[2] ="姓名";
names[3] ="性別";
names[4] ="班級";
for(int j= 0 ;j<CountColumnNum; j++){
firstcell[j] = firstrow.createCell((short)j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
while(rs.next())
{
// 建立試算表的一行
HSSFRow row = sheet.createRow(i) ; // 下標為1的行開始
for(int j=0;j<CountColumnNum;j++)
{
// 在一行內迴圈
HSSFCell cell = row.createCell((short) j) ;
// 設定表格的編碼集,使支援中文
//// 先判斷資料庫中的資料類型
// 將結果集裡的值放入試算表中
cell.setCellValue(new HSSFRichTextString(rs.getString(j+1))) ;
}
i++ ;
}
// 建立檔案輸出資料流,準備輸出試算表
OutputStream out = new FileOutputStream("E:\\person.xls") ;
wb.write(out) ;
out.close() ;
System.out.println("資料庫匯出成功") ;
rs.close() ;
con.close() ;
}
public static void main(String[] args)
{
try {
@SuppressWarnings("unused")
MySql2Excel excel = new MySql2Excel() ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
例如你可以在前端jsp中這樣調用:
<a href="ReportServlet" onclick="return confirm('確認資料匯出到E:/下?');">匯出資料到Excel</a>
後台servlet寫上上面的代碼,注意最後需要response.sendRedirect("") 返回前端