web資料匯出到Excel的步驟方法及糾錯(一)
阿新 • • 發佈:2018-11-25
頁面HTML程式碼寫法——新增到處Excel按鈕
<input type="button" ng-click="exports0()" value="匯出Excel">
selectController.js ——新增內容
//資料匯出到excel $scope.exports0 = function () { userService.exports0().success( function (response) { if(response.success) alert("到處資料成功!!!"); else alert("系統出錯!!!"); } ); }
userService.js ——新增內容(該部分與javad程式碼相關聯)
this.exports0 = function(){
window.location.href = "/PostalBank/user/findexcle.do" ;
}
ScoreController.java 新增 (這是我新增的 你可以把他放到相應的controller中去)
@RequestMapping("findexcle.do") public Result exports0( HttpServletRequest request, HttpServletResponse response) { try { ServletOutputStream out = response.getOutputStream(); String fileName = "勞動紀律評分.xls"; String resultFileName = ImportExcelUtil.encodeChineseDownloadFileName(request, fileName); response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=" + resultFileName); List<Map> list = userService.getscroe(new TUser()); userService.export001(out,list); } catch (Exception e) { e.printStackTrace(); return new Result(false, "匯出失敗"); } return new Result(true, "匯出成功"); }
UserService.java ——新增內容
export001(ServletOutputStream out, List<Map> list);
List<Map> getscroe(TUser tuser)
UserServiceImpl.java-----新增內容
@Override public List<Map> getscroe(TUser tuser) { TUserExample example = new TUserExample(); example.setOrderByClause("id ASC"); TUserExample.Criteria criteria = example.createCriteria(); if(tuser != null) { //按工號進行匹配搜尋 if(tuser.getId()!= null) { criteria.andIdEqualTo(tuser.getId()); } } List<Map> list = tUserMapper.selectUserscroe(tuser); return list; } @Override public void export001(ServletOutputStream out, List<Map> list) { // 第一步,建立一個workbook,對應一個Excel檔案 HSSFWorkbook workbook = new HSSFWorkbook(); // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet HSSFSheet hssfSheet = workbook.createSheet("勞動紀律"); // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short HSSFRow hssfRow = hssfSheet.createRow(0); HSSFCell cell = null; String[] names = {"工號","姓名","公司","部門","崗位","評分"}; for (int i = 0; i < names.length; i++) { //為了每列設定單元格的顏色,得建立多個單元格格式物件 HSSFCellStyle hssfCellStyle = workbook.createCellStyle(); //居中樣式 hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell hssfCell = null; hssfCell = hssfRow.createCell(i);//列索引從0開始 hssfCell.setCellValue(names[i]);//列名1 //設定字型樣式 HSSFFont fontStyle=workbook.createFont(); fontStyle.setFontName("黑體"); //新增到cell樣式中 hssfCellStyle.setFont(fontStyle); hssfCell.setCellStyle(hssfCellStyle);//列居中顯示 } for(int j =0 ; j< list.size();j++) { Map map = list.get(j); HSSFRow row = hssfSheet.createRow(j+1); //可以使用遍歷表頭進行填充 但是獲取內容型別必須一致才可以 例如都是String 型別 cell = row.createCell(0); //這裡對空指標異常的處理 有的欄位空值 他在map中沒有key //那我咋知道他有沒有key了,下面這兩段的區別? // cell.setCellValue(map.get("jobnumber").toString() != null ? map.get("jobnumber").toString() : null); cell.setCellValue(!StringUtils.isEmpty(map.get("jobnumber")+"") ? map.get("jobnumber").toString() : null); cell = row.createCell(1); cell.setCellValue(!StringUtils.isEmpty(map.get("nickname")+"") ? map.get("nickname").toString() : null); cell = row.createCell(2); cell.setCellValue(!StringUtils.isEmpty(map.get("companyname")+"") ? map.get("companyname").toString() : null); cell = row.createCell(3); cell.setCellValue(!StringUtils.isEmpty(map.get("categoryname")+"") ? map.get("categoryname").toString() : null); cell = row.createCell(4); cell.setCellValue(!StringUtils.isEmpty(map.get("deptname")+"") ? map.get("deptname").toString() : null); cell = row.createCell(5); cell.setCellValue(map.containsKey("labourscore") && !StringUtils.isEmpty(map.get("labourscore")+"") ? map.get("labourscore").toString() : null); } // 第七步,將檔案輸出到客戶端瀏覽器 try { workbook.write(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
TuserMapper.xml
<select id="selectUserscroe" resultMap="BaseResultMapToMap" parameterType="com.shenshou.postalbank.dao.pojo.TUserExample" >
select
<include refid="Base_Column_List" />
from t_user
</select>
這些步驟寫完,頁面資料匯出EXCEL就能夠實現了。當然在這其中我們也會遇到一些各種各樣的問題。比如我在通過修改這部分程式碼時候放在其他頁面上面,會出現匯出的資料為亂碼。當這個問題出現,我的解決及錯誤將向大家展示如下。
web資料匯出到Excel的步驟方法及糾錯(二)https://blog.csdn.net/baidu_38878945/article/details/84447037