jsp如下:
點擊(此處)摺疊或打開
- <a href="ReportServlet" onclick="return confirm('確認數據導出到E:/下?');">導出數據到Excel</a>
點擊(此處)摺疊或打開
-
package control;
-
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
import java.io.PrintWriter;
-
import java.sql.Connection;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
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;
-
-
import db.DB;
-
-
public class ReportServlet extends HttpServlet {
-
-
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
this.doPost(request, response);
-
}
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
request.setCharacterEncoding("utf-8");
-
//獲得要生成Excel數據的數據庫中的表名稱
-
String tableName = request.getParameter("tableName");
-
-
// 禁止數據緩存。
-
response.setHeader("Pragma", "no-cache");
-
response.setHeader("Cache-Control", "no-cache");
-
response.setDateHeader("Expires", 0);
-
Connection conn = null ;
-
DB db = new DB() ;
-
conn = db.getDB() ;
-
String sql = "select * from test_table" ;
-
-
ResultSet rs = null;
-
try {
-
rs = conn.createStatement().executeQuery(sql);
-
} catch (SQLException e1) {
-
// TODO Auto-generated catch block
-
e1.printStackTrace();
-
}
-
// 獲取總列數
-
int CountColumnNum=0;
-
try {
-
CountColumnNum = rs.getMetaData().getColumnCount();
-
} catch (SQLException e1) {
-
// TODO Auto-generated catch block
-
e1.printStackTrace();
-
}
-
int i = 1 ;
-
// 創建Excel文檔
-
HSSFWorkbook wb = new HSSFWorkbook() ;
-
// sheet 對應一個工作頁
-
HSSFSheet sheet = wb.createSheet("test_table表中的數據") ;
-
HSSFRow firstrow = sheet.createRow(0); //下標爲0的行開始
-
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
-
String[] names = new String[CountColumnNum];
-
names[0] = "ID";
-
names[1] = "學號";
-
names[2] = "姓名";
-
-
for(int j= 0 ;j<CountColumnNum; j++){
-
firstcell[j] = firstrow.createCell(j);
-
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
-
}
-
try {
-
while(rs.next())
-
{
-
// 創建電子表格的一行
-
HSSFRow row = sheet.createRow(i) ; // 下標爲1的行開始
-
for(int j=0;j<CountColumnNum;j++)
-
{
-
// 在一行內循環
-
HSSFCell cell = row.createCell( j) ;
-
// 設置表格的編碼集,使支持中文
-
//
-
// 先判斷數據庫中的數據類型
-
// 將結果集裏的值放入電子表格中
-
try {
-
cell.setCellValue(new HSSFRichTextString(rs.getString(j+1))) ;
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
i++ ;
-
}
-
} catch (SQLException e1) {
-
// TODO Auto-generated catch block
-
e1.printStackTrace();
-
}
-
// 創建文件輸出流,準備輸出電子表格
-
OutputStream out = new FileOutputStream("E:\\test.xls") ;
-
wb.write(out) ;
-
out.close() ;
-
System.out.println("數據庫導出成功") ;
-
try {
-
rs.close() ;
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
try {
-
conn.close() ;
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
response.sendRedirect("test.jsp");
-
}
-
public static void main(String[] args)
-
{
-
try {
-
@SuppressWarnings("unused")
-
ReportServlet excel = new ReportServlet() ;
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
- }
相關熱門文章
給主人留下些什麼吧!~~
評論熱議