這篇文章將為大家詳細講解有關Java實現(xiàn)數(shù)據(jù)庫中查詢出數(shù)據(jù)轉存成excel表的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
十載的田家庵網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。營銷型網(wǎng)站建設的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整田家庵建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“田家庵網(wǎng)站設計”,“田家庵網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。注意日期格式如果是以String類型的方式存到數(shù)據(jù)庫的導出時要轉換一次,直接導出格式不對
因為導出excel表格用的是get方式傳參,所以如果需要對導出的數(shù)據(jù)用中文模糊查詢,此時 用get傳參會出現(xiàn)中文亂碼
解決辦法:
前端對需要傳的中文參數(shù)進行一次編碼 URLEncoder.encode(傳參,“utf-8”);
后臺需要再次解碼:URLDecoder.decode(接收的參數(shù),“utf-8”);
@RequestMapping(value = "outPutExcel", method = RequestMethod.GET) @ResponseBody public void outPutExcel( HttpServletResponse response,String officeid, String sonid,String nameorphone,String beginTime, String endTime,String option) { String nString = ""; try { if (nameorphone != null && nameorphone != "") { //對前端傳的參數(shù)解碼 nString = URLDecoder.decode(nameorphone,"UTF-8"); } } catch (UnsupportedEncodingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } response.reset(); //設置瀏覽器下載的格式,并以當前時間的毫秒數(shù)命名 response.setHeader("Content-Disposition", "attachment;Filename=" + System.currentTimeMillis() + ".xls"); response.setContentType("application/msexcel"); Listlist = purchaseService.selectPCSum(officeid, sonid, nString, beginTime, endTime, option); if (list == null && list.isEmpty()) { throw new NullPointerException("導出數(shù)據(jù)源為空"); } HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("sheet0"); HSSFRow rows; HSSFCell cells; //設置表格第一行的列名 // 獲得表格第一行 rows = sheet.createRow(0); // 根據(jù)需要給第一行每一列設置標題 cells = rows.createCell(0); cells.setCellValue("客戶姓名"); cells = rows.createCell(1); cells.setCellValue("客戶電話"); cells = rows.createCell(2); cells.setCellValue("下單日期"); cells = rows.createCell(3); cells.setCellValue("訂單號"); cells = rows.createCell(4); cells.setCellValue("所屬分公司"); cells = rows.createCell(5); cells.setCellValue("簽單人"); cells = rows.createCell(6); cells.setCellValue("品名"); cells = rows.createCell(7); cells.setCellValue("型號"); cells = rows.createCell(8); cells.setCellValue("顏色"); cells = rows.createCell(9); cells.setCellValue("尺寸"); cells = rows.createCell(10); cells.setCellValue("材質"); cells = rows.createCell(11); cells.setCellValue("已采購數(shù)量(件)"); cells = rows.createCell(12); cells.setCellValue("采購單價"); cells = rows.createCell(13); cells.setCellValue("采購總價"); cells = rows.createCell(14); cells.setCellValue("已出庫(件)"); //循環(huán)數(shù)據(jù)庫查出來的數(shù)據(jù)集,對應每一列賦值 //此處list.size()本不應該-1,因為同事在list集合里追加了另一條數(shù)據(jù),導致報錯故將其去除 for (int i = 0; i < list.size()-1; i++) { rows = sheet.createRow(i + 1); cells = rows.createCell(0); cells.setCellValue(list.get(i).getCustomerName()); cells = rows.createCell(1); cells.setCellValue(list.get(i).getPhone()); //對日期格式進行轉換 cells = rows.createCell(2); String dateString = list.get(i).getPlaceOrderTime().toString(); Date date = null; try { date = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK).parse(dateString); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cells.setCellValue(sdf.format(date)); cells = rows.createCell(3); cells.setCellValue(list.get(i).getOrderNumber()); cells = rows.createCell(4); cells.setCellValue(list.get(i).getOfficeName()); cells = rows.createCell(5); cells.setCellValue(list.get(i).getUsername()); cells = rows.createCell(6); cells.setCellValue(list.get(i).getProductName()); cells = rows.createCell(7); cells.setCellValue(list.get(i).getType()); cells = rows.createCell(8); cells.setCellValue(list.get(i).getColor()); cells = rows.createCell(9); cells.setCellValue(list.get(i).getSize()); cells = rows.createCell(10); cells.setCellValue(list.get(i).getTexture()); cells = rows.createCell(11); cells.setCellValue(list.get(i).getPurchasedNumber()); cells = rows.createCell(12); cells.setCellValue(list.get(i).getPurchaseprice()); cells = rows.createCell(13); cells.setCellValue(list.get(i).getPurchasePriceSun()); cells = rows.createCell(14); cells.setCellValue(list.get(i).getOutlibraryNumber()); } try { OutputStream oStream = response.getOutputStream(); wb.write(oStream); oStream.flush(); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
關于Java實現(xiàn)數(shù)據(jù)庫中查詢出數(shù)據(jù)轉存成excel表的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。