数据库内容导出为excel并下载
生活随笔
收集整理的這篇文章主要介紹了
数据库内容导出为excel并下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/***Controller層*/@RequestMapping("/downUserInfo")public?void?downUserInfo(HttpServletRequest?request,HttpServletResponse?response)?{try?{djsService.downUserInfo(request,?response);}?catch?(Exception?e)?{e.printStackTrace();}}/***?service層*?下載所有報名學員信息*?@throws?Exception?*/public?void?downUserInfo(HttpServletRequest?request,HttpServletResponse?response)?throws?Exception?{List<User>?users?=?djsDao.findAllUser();?//查詢所有的報名學員File?file?=?UserInfoPoiUtil.write2003(users,?request);?//將學員信息寫入到excel中DownLoadUtil.downLoad(request,?response,?file);?//下載文件}/***?將學員信息寫入到excel中*/public?static?File?write2003(List<User>?users,HttpServletRequest?request)throws?Exception{File?src=new?File(request.getSession().getServletContext().getRealPath("/poi/userInfo.xls"));File?dest=new?File(request.getSession().getServletContext().getRealPath("/temp/userInfo.xls"));FileUtil?fileUtil=FileUtil.getInstance();fileUtil.copy(src,?dest);FileInputStream?in?=?new?FileInputStream(dest);HSSFWorkbook?wb?=?new?HSSFWorkbook(in);//獲得表單HSSFSheet?sheet?=?wb.getSheetAt(0);//?獲取字體HSSFFont?font?=?wb.createFont();font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//?字體增粗for(int?i=0;i<users.size();i++){User?u=users.get(i);HSSFRow?row?=?sheet.getRow(3+i);if(row==null){row=sheet.createRow(3+i);}for?(int?j?=?0;?j?<?8;?j++)?{HSSFCell?cell=row.getCell(j);if(cell==null){cell=row.createCell(j);}switch?(j)?{case?0:cell.setCellValue(u.getEmail());break;case?1:cell.setCellValue(u.getTelephone());break;case?2:Integer?discipline?=?u.getDiscipline();if(discipline?==?1){cell.setCellValue("APP開發與設計");}else{cell.setCellValue("網絡營銷技能");}break;case?3:cell.setCellValue(u.getQq());break;case?4:cell.setCellValue(new?SimpleDateFormat("yyyy-mm-dd").format(u.getAdminssionDate()));break;case?5:cell.setCellValue(GetCityValue.getCityValue(Integer.toString(u.getProvince())));break;case?6:String?school?=?GetCityValue.getSchoolValue(u);cell.setCellValue(school);break;case?7:String?major?=?GetCityValue.getMajorValue(u);cell.setCellValue(major);default:break;}}}//?寫入ExcelFileOutputStream?out?=?new?FileOutputStream(dest);wb.write(out);out.close();return?dest;}/***?文件下載*/public?static?void?downLoad(HttpServletRequest?request,HttpServletResponse?response,File?file)throws?Exception{//下載文件boolean?isOnLine?=?false;response.reset();?//?非常重要if?(isOnLine)?{?//?在線打開方式URL?u?=?new?URL("file:///"?+?file.getPath());response.setContentType(u.openConnection().getContentType());response.setHeader("Content-Disposition",?"inline;?filename="+file.getName());}?else?{?//?純下載方式response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition",?"p_w_upload;?filename="+file.getName());}byte[]?buf?=?new?byte[1024];int?len?=?0;BufferedInputStream?br?=?null;OutputStream?fileOut?=?null;try?{br?=?new?BufferedInputStream(new?FileInputStream(file));fileOut?=?response.getOutputStream();while?((len?=?br.read(buf))?>?0)?{fileOut.write(buf,?0,?len);}fileOut.flush();}?catch?(Exception?e)?{}?finally?{if?(br?!=?null)?{br.close();br?=?null;}if?(fileOut?!=?null)?{fileOut.close();fileOut?=?null;}}if(file.exists()){file.delete();}}
需要注意的問題:
????1、excel2003和excel2007是不同的。詳細區別自己查
????2、excel表格的類型,一般設置為text文本類型即可。
????3、excel表格中數據對應。
????4、記得寫操作以及關閉流的操作。
????5、excel文件命名一般不要出現中文名,使用英文名,再不濟漢語拼音也可以。(如果是中文名的話,需要做相應的修改。例如將tomcat的編碼格式配置為utf8)
轉載于:https://blog.51cto.com/fengcl/1704191
總結
以上是生活随笔為你收集整理的数据库内容导出为excel并下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 排版----描述
- 下一篇: 扩展GridView控件——为内容项添加