Java 從數據庫中查找信息導入Excel表格中
阿新 • • 發佈:2017-06-13
多結果 tput session false 狀態 結果 mmu call excel表格
前端js
function Excel (){ //ajax請求 $.ajax({ url : "outPutAboutShopInfo", type : "post", dataType : "json", data:{ "basicShop.shopId" : shopId, "basicShop.shopMemo" : stringType //不方便增加字段所以使用門店的一個“備註”字段來接收‘類型‘ }, success :function(data) { window.location.href = data.communal.data; } }); }
後端 Java
該方法是將數據插入excel中,將excel保存到服務器中,然後訪問服務器下載文件,方法不怎麽好
需要導入的jar包
第一:獲得數據的方法
使用存儲過程獲得數據
舉例:
// 這個存儲過程的核心就是一條簡單的sql語句
DELIMITER $$ DROP PROCEDURE IF EXISTS `pahung82`.`ht_out_put_mem_phone`$$CREATE DEFINER=`root`@`%` PROCEDURE `ht_out_put_mem_phone`( in shopId varchar(40) -- 門店Id ) BEGIN SELECT VIP_NAME ,VIP_TELEPHONE from mem_vip where SHOP_ID = shopId; END$$ DELIMITER ;
結果類似如下:
第二:調用存儲過程,將數據寫入excel表格
1.調用存儲過程
Session session = null; Connection conn = null; CallableStatement cs= null; ResultSet rs = null; // 存儲過程 條件 門店Id String shopId = basicShop.getShopId(); try { session = this.hibernateTemplate.getSessionFactory().openSession(); session.beginTransaction(); conn = session.connection();
// 存儲過程調用 cs = conn.prepareCall("{call ht_out_put_mem_phone(?)}"); cs.setString(1, shopId);// 填充參數 boolean hadResults = false;
// 運行存儲過程 hadResults = cs.execute(); int index = 0, no = 0; while (hadResults) { rs = (ResultSet) cs.getResultSet(); if (index == 0) { while (rs.next()) { no++; row = sheet.createRow((int) no); // 創建單元格,設置值
// 這裏就是循環結果集的值
} } hadResults = cs.getMoreResults(); // 檢查是否存在更多結果集 index++; } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); cs.close(); session.clear(); conn.close(); session.close(); }
2.將數據寫入excel表格
Session session = null; Connection conn = null; CallableStatement cs = null; ResultSet rs = null; // 存儲過程 條件 門店Id String shopId = basicShop.getShopId(); // 1.創建一個workbook,對應一個Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 2.在workbook中添加一個sheet,對應Excel中的一個sheet HSSFSheet sheet = wb.createSheet("手機號"); // 3.在sheet中添加表頭第0行,老版本poi對excel行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 4.創建單元格,設置值表頭,設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); // 居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 設置表頭 HSSFCell cell = row.createCell(0); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("手機號"); cell.setCellStyle(style); try { session = this.hibernateTemplate.getSessionFactory().openSession(); session.beginTransaction(); conn = session.connection(); cs = conn.prepareCall("{call ht_out_put_mem_phone(?)}"); cs.setString(1, shopId); boolean hadResults = false; hadResults = cs.execute(); int index = 0, no = 0; while (hadResults) { rs = (ResultSet) cs.getResultSet(); if (index == 0) { while (rs.next()) { no++; row = sheet.createRow((int) no); // 創建單元格,設置值 row.createCell(0).setCellValue(rs.getString(1)); row.createCell(1).setCellValue(rs.getString(2)); } } hadResults = cs.getMoreResults(); // 檢查是否存在更多結果集 index++; } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); cs.close(); session.clear(); conn.close(); session.close(); }
註意:當你要插入對個sheet時 則只需再來一遍 HSSFSheet sheet_qq = wb.createSheet("QQ號"); 後面即重復
3.保存文件
FileOutputStream out = new FileOutputStream(request.getSession().getServletContext().getRealPath("") + "/" + "xxx.xls"); //服務器的絕對路徑 request.getSession().getServletContext().getRealPath("") // 例如:D:\Tomcat\apache-tomcat-8.0.37\me-webapps\Test // Test是項目名 wb.write(out); out.close();
4.訪問下載
HttpServletRequest request = ServletActionContext.getRequest(); // request.getRequestURL() 是StringBuffer類型,所以要轉換一下 String url = request.getRequestURL().toString(); //文件路徑地址 request.getRequestURL() + "xxx.xls";
文件路徑地址 url = request.getRequestURL() + "xxx.xls";
只需要將路徑地址傳給js,success 函數中使用 window.location.href = url ; 可得到下載框
附上一個老長的代碼,僅空參考
public Communal outPutAboutShopInfo(BasicShop basicShop) throws Exception { //存儲escel文件名 String excel_shop_name = "shop_info.xls"; HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); Communal communal = new Communal(); String [] out_type = new String []{}; // 接收導出數據類型 (1 手機號,2 會員信息,3 會員基卡,4記次卡次數信息) if(null != basicShop.getShopMemo() && !"".equals(basicShop.getShopMemo())){ out_type = basicShop.getShopMemo().toString().split(","); } Session session = null; Connection conn = null; CallableStatement cs = null; ResultSet rs = null; // 存儲過程 條件 門店Id String shopId = basicShop.getShopId(); // 1.創建一個workbook,對應一個Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); for(int i=0;i<out_type.length;i++) { // 導出會員手機號碼 if ( out_type[i] .equals("1")) { System.out.println(); // 2.在workbook中添加一個sheet,對應Excel中的一個sheet HSSFSheet sheet = wb.createSheet("手機號"); // 3.在sheet中添加表頭第0行,老版本poi對excel行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 4.創建單元格,設置值表頭,設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); // 居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 設置表頭 HSSFCell cell = row.createCell(0); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("手機號"); cell.setCellStyle(style); try { session = this.hibernateTemplate.getSessionFactory().openSession(); session.beginTransaction(); conn = session.connection(); cs = conn.prepareCall("{call ht_out_put_mem_phone(?)}"); cs.setString(1, shopId); boolean hadResults = false; hadResults = cs.execute(); int index = 0, no = 0; while (hadResults) { rs = (ResultSet) cs.getResultSet(); if (index == 0) { while (rs.next()) { no++; row = sheet.createRow((int) no); // 創建單元格,設置值 row.createCell(0).setCellValue(rs.getString(1)); row.createCell(1).setCellValue(rs.getString(2)); } } hadResults = cs.getMoreResults(); // 檢查是否存在更多結果集 index++; } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); cs.close(); session.clear(); conn.close(); session.close(); } } //導出會員信息 if (out_type[i] .equals("2")) { // 2.在workbook中添加一個sheet,對應Excel中的一個sheet HSSFSheet sheet = wb.createSheet("會員信息"); // 3.在sheet中添加表頭第0行,老版本poi對excel行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 4.創建單元格,設置值表頭,設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); // 居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 設置表頭 HSSFCell cell = row.createCell(0); cell.setCellValue("會員編號"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("會員號"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("狀態(01掛失2註銷)"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("會員名稱"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("大寫首字母"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("出生日期"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("入會時間"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("是否開通短信(1是0否)"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("每月短信費用"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("短信指向"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("電話號碼"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("QQ"); cell.setCellStyle(style); cell = row.createCell(12); cell.setCellValue("性別"); cell.setCellStyle(style); cell = row.createCell(13); cell.setCellValue("證件類型(0身份證 1學生證 2工作證 3軍官證)"); cell.setCellStyle(style); cell = row.createCell(14); cell.setCellValue("證件號碼"); cell.setCellStyle(style); cell = row.createCell(15); cell.setCellValue("郵箱"); cell.setCellStyle(style); cell = row.createCell(16); cell.setCellValue("職業"); cell.setCellStyle(style); cell = row.createCell(17); cell.setCellValue("郵編"); cell.setCellStyle(style); cell = row.createCell(18); cell.setCellValue("地址"); cell.setCellStyle(style); cell = row.createCell(19); cell.setCellValue("頭像"); cell.setCellStyle(style); cell = row.createCell(20); cell.setCellValue("密碼"); cell.setCellStyle(style); cell = row.createCell(21); cell.setCellValue("介紹人"); cell.setCellStyle(style); cell = row.createCell(22); cell.setCellValue("備註"); cell.setCellStyle(style); cell = row.createCell(23); cell.setCellValue("會員積分"); cell.setCellStyle(style); cell = row.createCell(24); cell.setCellValue("消費總額"); cell.setCellStyle(style); cell = row.createCell(25); cell.setCellValue("最後消費時間"); cell.setCellStyle(style); cell = row.createCell(26); cell.setCellValue("卡名稱"); cell.setCellStyle(style); cell = row.createCell(27); cell.setCellValue("會員卡編號"); cell.setCellStyle(style); cell = row.createCell(28); cell.setCellValue("卡狀態(0掛失1啟用)"); cell.setCellStyle(style); cell = row.createCell(29); cell.setCellValue("卡類型0是儲值 1是折扣積分 2是計次"); cell.setCellStyle(style); cell = row.createCell(30); cell.setCellValue("卡售價"); cell.setCellStyle(style); cell = row.createCell(31); cell.setCellValue("卡有效期限"); cell.setCellStyle(style); cell = row.createCell(32); cell.setCellValue("積分"); cell.setCellStyle(style); cell = row.createCell(33); cell.setCellValue("卡金"); cell.setCellStyle(style); cell = row.createCell(34); cell.setCellValue("卡抵用金"); cell.setCellStyle(style); cell = row.createCell(35); cell.setCellValue("會員卡消費總額"); cell.setCellStyle(style); cell = row.createCell(36); cell.setCellValue("最後消費日期"); cell.setCellStyle(style); cell = row.createCell(37); cell.setCellValue("計次卡次數"); cell.setCellStyle(style); try { session = this.hibernateTemplate.getSessionFactory().openSession(); session.beginTransaction(); conn = session.connection(); cs = conn.prepareCall("{call ht_out_put_mem_info(?)}"); cs.setString(1, shopId); boolean hadResults = false; hadResults = cs.execute(); int index = 0, no = 0; while (hadResults) { rs = (ResultSet) cs.getResultSet(); if (index == 0) { while (rs.next()) { no++; row = sheet.createRow((int) no); // 創建單元格,設置值 row.createCell(0).setCellValue(rs.getString(1)); row.createCell(1).setCellValue(rs.getString(2)); row.createCell(2).setCellValue(rs.getString(3)); row.createCell(3).setCellValue(rs.getString(4)); row.createCell(4).setCellValue(rs.getString(5)); row.createCell(5).setCellValue(rs.getString(6)); row.createCell(6).setCellValue(rs.getString(7)); row.createCell(7).setCellValue(rs.getString(8)); row.createCell(8).setCellValue(rs.getString(9)); row.createCell(9).setCellValue(rs.getString(10)); row.createCell(10).setCellValue(rs.getString(11)); row.createCell(11).setCellValue(rs.getString(12)); row.createCell(12).setCellValue(rs.getString(13)); row.createCell(13).setCellValue(rs.getString(14)); row.createCell(14).setCellValue(rs.getString(15)); row.createCell(15).setCellValue(rs.getString(16)); row.createCell(16).setCellValue(rs.getString(17)); row.createCell(17).setCellValue(rs.getString(18)); row.createCell(18).setCellValue(rs.getString(19)); row.createCell(19).setCellValue(rs.getString(20)); row.createCell(20).setCellValue(rs.getString(21)); row.createCell(21).setCellValue(rs.getString(22)); row.createCell(22).setCellValue(rs.getString(23)); row.createCell(23).setCellValue(rs.getString(24)); row.createCell(24).setCellValue(rs.getString(25)); row.createCell(25).setCellValue(rs.getString(26)); row.createCell(26).setCellValue(rs.getString(27)); row.createCell(27).setCellValue(rs.getString(28)); row.createCell(28).setCellValue(rs.getString(29)); row.createCell(29).setCellValue(rs.getString(30)); row.createCell(30).setCellValue(rs.getString(31)); row.createCell(31).setCellValue(rs.getString(32)); row.createCell(32).setCellValue(rs.getString(33)); row.createCell(33).setCellValue(rs.getString(34)); row.createCell(34).setCellValue(rs.getString(35)); row.createCell(35).setCellValue(rs.getString(36)); row.createCell(36).setCellValue(rs.getString(37)); row.createCell(37).setCellValue(rs.getString(38)); } } hadResults = cs.getMoreResults(); // 檢查是否存在更多結果集 index++; } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); cs.close(); session.clear(); conn.close(); session.close(); } } if (out_type[i] .equals("3")) { // 2.在workbook中添加一個sheet,對應Excel中的一個sheet HSSFSheet sheet = wb.createSheet("會員基卡"); // 3.在sheet中添加表頭第0行,老版本poi對excel行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 4.創建單元格,設置值表頭,設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); // 居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 設置表頭 HSSFCell cell = row.createCell(0); cell.setCellValue("卡名稱"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("卡類型0是儲值;1是折扣積分比例;2是計次"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("有效期)"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("初始面值"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("卡贈送金"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("服務折扣"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("產品折扣"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("服務積分比率"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("產品積分比率"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("初始積分"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("充值積分比例"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("狀態(1啟用,0不啟用)"); cell.setCellStyle(style); cell = row.createCell(12); cell.setCellValue("是否儲值(0否 1是)"); cell.setCellStyle(style); cell = row.createCell(13); cell.setCellValue("辦卡提成"); cell.setCellStyle(style); cell = row.createCell(14); cell.setCellValue("辦卡提成方式(0是比例1是固定)"); cell.setCellStyle(style); cell = row.createCell(15); cell.setCellValue("辦卡業績比例"); cell.setCellStyle(style); cell = row.createCell(16); cell.setCellValue("充值提成"); cell.setCellStyle(style); cell = row.createCell(17); cell.setCellValue("充值提成方式(0是比例1是固定)"); cell.setCellStyle(style); cell = row.createCell(18); cell.setCellValue("充值業績比例"); cell.setCellStyle(style); cell = row.createCell(19); cell.setCellValue("還款提成"); cell.setCellStyle(style); cell = row.createCell(20); cell.setCellValue("還款提成方式(0是比例1是固定)"); cell.setCellStyle(style); cell = row.createCell(21); cell.setCellValue("還款業績比例"); cell.setCellStyle(style); cell = row.createCell(22); cell.setCellValue("備註"); cell.setCellStyle(style); cell = row.createCell(23); cell.setCellValue("卡售價"); cell.setCellStyle(style); cell = row.createCell(24); cell.setCellValue("抵用金狀態 1是啟用;0是不啟用"); cell.setCellStyle(style); cell = row.createCell(25); cell.setCellValue("初始抵用金"); cell.setCellStyle(style); cell = row.createCell(26); cell.setCellValue("充值滿多少"); cell.setCellStyle(style); cell = row.createCell(27); cell.setCellValue("送多少"); cell.setCellStyle(style); cell = row.createCell(28); cell.setCellValue("消費滿多少"); cell.setCellStyle(style); cell = row.createCell(29); cell.setCellValue("消費滿送多少"); cell.setCellStyle(style); try { session = this.hibernateTemplate.getSessionFactory().openSession(); session.beginTransaction(); conn = session.connection(); cs = conn.prepareCall("{call ht_out_put_mem_card(?)}"); cs.setString(1, shopId); boolean hadResults = false; hadResults = cs.execute(); int index = 0, no = 0; while (hadResults) { rs = (ResultSet) cs.getResultSet(); if (index == 0) { while (rs.next()) { no++; row = sheet.createRow((int) no); // 創建單元格,設置值 row.createCell(0).setCellValue(rs.getString(1)); row.createCell(1).setCellValue(rs.getString(2)); row.createCell(2).setCellValue(rs.getString(3)); row.createCell(3).setCellValue(rs.getString(4)); row.createCell(4).setCellValue(rs.getString(5)); row.createCell(5).setCellValue(rs.getString(6)); row.createCell(6).setCellValue(rs.getString(7)); row.createCell(7).setCellValue(rs.getString(8)); row.createCell(8).setCellValue(rs.getString(9)); row.createCell(9).setCellValue(rs.getString(10)); row.createCell(10).setCellValue(rs.getString(11)); row.createCell(11).setCellValue(rs.getString(12)); row.createCell(12).setCellValue(rs.getString(13)); row.createCell(13).setCellValue(rs.getString(14)); row.createCell(14).setCellValue(rs.getString(15)); row.createCell(15).setCellValue(rs.getString(16)); row.createCell(16).setCellValue(rs.getString(17)); row.createCell(17).setCellValue(rs.getString(18)); row.createCell(18).setCellValue(rs.getString(19)); row.createCell(19).setCellValue(rs.getString(20)); row.createCell(20).setCellValue(rs.getString(21)); row.createCell(21).setCellValue(rs.getString(22)); row.createCell(22).setCellValue(rs.getString(23)); row.createCell(23).setCellValue(rs.getString(24)); row.createCell(24).setCellValue(rs.getString(25)); row.createCell(25).setCellValue(rs.getString(26)); row.createCell(26).setCellValue(rs.getString(27)); row.createCell(27).setCellValue(rs.getString(28)); row.createCell(28).setCellValue(rs.getString(29)); row.createCell(29).setCellValue(rs.getString(30)); } } hadResults = cs.getMoreResults(); // 檢查是否存在更多結果集 index++; } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); cs.close(); session.clear(); conn.close(); session.close(); } } if (out_type[i] .equals("4")) { // 2.在workbook中添加一個sheet,對應Excel中的一個sheet HSSFSheet sheet = wb.createSheet("記次卡信息"); // 3.在sheet中添加表頭第0行,老版本poi對excel行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 4.創建單元格,設置值表頭,設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); // 居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 設置表頭 HSSFCell cell = row.createCell(0); cell.setCellValue("卡名稱"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("服務名稱"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("剩余次數)"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("會員號"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("原單價"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("提成價"); cell.setCellStyle(style); try { session = this.hibernateTemplate.getSessionFactory().openSession(); session.beginTransaction(); conn = session.connection(); cs = conn.prepareCall("{call ht_out_put_mem_card_times_info(?)}"); cs.setString(1, shopId); boolean hadResults = false; hadResults = cs.execute(); int index = 0, no = 0; while (hadResults) { rs = (ResultSet) cs.getResultSet(); if (index == 0) { while (rs.next()) { no++; row = sheet.createRow((int) no); // 創建單元格,設置值 row.createCell(0).setCellValue(rs.getString(1)); row.createCell(1).setCellValue(rs.getString(2)); row.createCell(2).setCellValue(rs.getString(3)); row.createCell(3).setCellValue(rs.getString(4)); row.createCell(4).setCellValue(rs.getString(5)); row.createCell(5).setCellValue(rs.getString(6)); } } hadResults = cs.getMoreResults(); // 檢查是否存在更多結果集 index++; } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); cs.close(); session.clear(); conn.close(); session.close(); } } } System.out.println(System.getProperty("user.dir") + "\\XXX.xls"); System.out.println(request.getRequestURL() + "/XXX.xls"); System.out.println(request.getSession().getServletContext().getRealPath("") + "/XXX.xls"); //response.setHeader("Content-Disposition" ,"attachment;filename="+new String((excel_shop_name).getBytes(),"UTF-8")); //response.setContentType("application/msexcel;charset=UTF-8"); FileOutputStream out = new FileOutputStream(request.getSession().getServletContext().getRealPath("") + "/" + excel_shop_name); wb.write(out); out.close(); // request.getRequestURL() 是StringBuffer類型,所以要轉換一下 String url = request.getRequestURL().toString(); String [] root_path = url.split("outPutAboutShopInfo"); //路徑地址 request.getRequestURL() + "xxx.xls"; communal.setData(root_path[0] + excel_shop_name); return communal; }僅供參考
Java 從數據庫中查找信息導入Excel表格中