index.jsp:html
<a href="POIout">簡單導出數據</a>java
<a href="POIoutTemplate">根據模板導出數據</a>mysql
package com.wp.poi; import java.sql.Connection; import java.sql.DriverManager; //鏈接數據庫類 public class DbUtil { private String dbUrl = "jdbc:mysql://localhost:3306/db_easyui"; private String dbUserName = "root"; private String dbPassword = "root"; private String jdbcName = "com.mysql.jdbc.Driver"; public Connection getCon() throws Exception { Class.forName(jdbcName); Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword); return con; } public void closeCon(Connection con) throws Exception { if (con != null) { con.close(); } } }
package com.wp.poi; import java.io.IOException; import java.io.OutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; 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.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * 普通導出數據 * * @author admin * */ public class POIout extends HttpServlet { public POIout() { super(); } public void init() throws ServletException { } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); DbUtil dbUtil = new DbUtil(); Connection conn = null; try { conn = dbUtil.getCon();// 得到數據庫鏈接 Workbook wb = new HSSFWorkbook(); String headers[] = { "編號", "姓名", "電話", "Email", "QQ" };// 標題 ResultSet rs = userList(conn);// 獲得結果集 fillExcelData(rs, wb, headers); export(response, wb, "導出數據.xls"); } catch (Exception e) { e.printStackTrace(); } finally { try { dbUtil.closeCon(conn); } catch (Exception e) { e.printStackTrace(); } } } /** * 查詢數據庫 * * @param con * @return ResultSet 返回結果集 * @throws Exception */ public ResultSet userList(Connection con) throws Exception { StringBuffer sb = new StringBuffer("select * from t_user"); PreparedStatement pstmt = con.prepareStatement(sb.toString()); return pstmt.executeQuery(); } /** * 導出用戶 * * @throws Exception */ public void fillExcelData(ResultSet rs, Workbook wb, String[] headers) throws Exception { int rowIndex = 0; // 第一行 Sheet sheet = wb.createSheet(); // 建立sheet頁 Row row = sheet.createRow(rowIndex++); // 建立標題 for (int i = 0; i < headers.length; i++) { row.createCell(i).setCellValue(headers[i]); } // 導出數據庫中的數據 while (rs.next()) { row = sheet.createRow(rowIndex++); for (int i = 0; i < headers.length; i++) { row.createCell(i).setCellValue(rs.getObject(i + 1).toString()); //rs.getObject(i + 1)獲得一個對象,即數據庫中一行的結果,每一列就是屬性湊成一行變成對象。由於id是從1開始,因此要+1。 } } } /** * 把數據放入到.xls文件中並下載到本地 * * @param response * @param wb * @param fileName * @throws Exception */ public void export(HttpServletResponse response, Workbook wb, String fileName) throws Exception { response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));// 設置頭信息 response.setContentType("application/ynd.ms-excel;charset=UTF-8"); OutputStream out = response.getOutputStream(); wb.write(out);// 進行輸出,下載到本地 out.flush(); out.close(); } }
package com.wp.poi; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; 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.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * 利用模板導出數據 * * @author admin * */ public class POIoutTemplate extends HttpServlet { public POIoutTemplate() { super(); } public void init() throws ServletException { } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); DbUtil dbUtil = new DbUtil(); Connection conn = null; try { conn = dbUtil.getCon(); Workbook wb = fillExcelDataWithTemplate(userList(conn), "Template.xls"); export(response, wb, "利用模板導出的數據.xls"); } catch (Exception e) { e.printStackTrace(); } finally { try { dbUtil.closeCon(conn); } catch (Exception e) { e.printStackTrace(); } } } /** * 查詢數據庫 * * @param con * @return * @throws Exception */ public ResultSet userList(Connection con) throws Exception { StringBuffer sb = new StringBuffer("select * from t_user"); PreparedStatement pstmt = con.prepareStatement(sb.toString()); return pstmt.executeQuery(); } /** * 根據模板導出用戶 * * @throws Exception */ public Workbook fillExcelDataWithTemplate(ResultSet rs, String templateFileName) throws Exception { InputStream in = POIoutTemplate.class .getResourceAsStream("/com/wp/poi/" + templateFileName); POIFSFileSystem fs = new POIFSFileSystem(in);// 解析Excel文件 Workbook wb = new HSSFWorkbook(fs);// 以解析的excel文件格式進行建立 Sheet sheet = wb.getSheetAt(0); // 獲取列數 int cellNums = sheet.getRow(0).getLastCellNum(); int rowIndex = 1;// 從1開始的緣由,模板的第一行固定了,因此從下一行開始 while (rs.next()) { Row row = sheet.createRow(rowIndex++); for (int i = 0; i < cellNums; i++) { row.createCell(i).setCellValue(rs.getObject(i + 1).toString()); } } return wb; } /** * 導出 * * @param response * @param wb * @param fileName * @throws Exception */ public static void export(HttpServletResponse response, Workbook wb, String fileName) throws Exception { response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1")); response.setContentType("application/ynd.ms-excel;charset=UTF-8"); OutputStream out = response.getOutputStream(); wb.write(out); out.flush(); out.close(); } }
模板:sql
導出後:數據庫
Java小生店鋪:apache
Pc端:http://shop125970977.taobao.com/index.htmapp
手機端:搜索 java小生店鋪jsp
但願店鋪的資料能幫助到你!!!ui