java實現匯出EXCEL檔案
阿新 • • 發佈:2018-12-09
/** * 方法:下載差錯明細檔案 作者:王元晨 */ public void DownDlDtlMskFile() { String tpId = DtaInfo.getInstance().getTpId(); CompSDO inputSdo = EPOper.getCompSDO(tpId, "iQ_DlDtlMskFile_REQ"); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); String now = format.format(new Date()); int seq = StarringSeq.getCustomSeq("3"); String seqString = String.format("%04d", seq); String asscOEcd = (String) inputSdo.getValue("asscOEcd");// 合作機構編碼 String oEcd = (String) inputSdo.getValue("oEcd");// 機構編碼 String begDt = (String) inputSdo.getValue("begDt");// 開始日期 String finDt = (String) inputSdo.getValue("finDt");// 結束日期 if (StringUtils.isEmpty(asscOEcd)) { setRspsInfo(tpId, "PC12101", "合作機構編碼為必輸項!"); return; } if (StringUtils.isEmpty(oEcd)) { setRspsInfo(tpId, "PC12102", "機構編碼為必輸項!"); return; } if (StringUtils.isEmpty(begDt)) { setRspsInfo(tpId, "PC12103", "開始日期為必輸項!"); return; } if (StringUtils.isEmpty(finDt)) { setRspsInfo(tpId, "PC12104", "結束日期為必輸項!"); return; } StringBuilder sql = new StringBuilder(); sql.append(" SELECT " + "a.MERT_ID , " + "a.MERT_NM , " + "a.ORD_T_DT , " + "a.ORD_S_N , " + "a.ORD_AMT " + "FROM T_ORD_CHKA_ERR a " + "WHERE a.ASSC_O_ECD ='" + asscOEcd + "' " + "AND a.O_ECD ='" + oEcd + "' " + "AND a.ORD_T_DT BETWEEN '" + begDt + "' AND '" + finDt + "' "); sql.append("ORDER BY ASSC_O_ECD,O_ECD,MERT_ID "); IDataSourceRoute dsRoute = (IDataSourceRoute) SpringUtil.getBean("dsRoute"); IBaseDos baseDos = dsRoute.getBaseDos(""); String[] args = new String[] { asscOEcd, oEcd, begDt, finDt }; boolean flag = PubBean.isSqlPlatValid(args); if (flag == false) { throw new BaseException("PCA1010", "引數不合法,發現引起資料注入危險的內容!"); } int num = baseDos.executeSql(Sql.SELLIST, sql.toString(), "T_ORD_CHKA_ERR");// 總資料量 String fileNm = "對賬差錯明細"; TrcLog.log(LOG_FILE, "[檔名字]"+fileNm+now+seqString); TrcLog.log(LOG_FILE, "匯出excel開始!!!"); File excelFile = createExcelFile(fileNm, 1,now+seqString); if (null != excelFile) { excel(num, tpId, excelFile.getAbsolutePath()); TrcLog.log(LOG_FILE, "匯出excel結束!!!"); MongoDbOpBean mongoDbOpBean = new MongoDbOpBean(); String fileId = mongoDbOpBean.upload(excelFile.getAbsolutePath()); excelFile.delete();//刪除檔案 TrcLog.log(LOG_FILE, "fileId:" + fileId); TrcLog.log(LOG_FILE, "fileNm:" + fileNm+now+seqString); EPOper.put(tpId, "iQ_DlDtlMskFile_RSP[0].fileId[0]", fileId);// 檔案ID EPOper.put(tpId, "iQ_DlDtlMskFile_RSP[0].fileNm[0]", fileNm+now+seqString+".xls");// 檔名 PubBean.setRspsInfo(tpId, "0000000", "成功"); } else { throw new BaseException("PS12F01", "建立檔案查詢結果明細檔案失敗"); } } /** * * @param FileName * @param FileType * 1-xls/2-xlsx * @return */ public File createExcelFile(String FileName, int FileType,String now) { StringBuilder fullPath = new StringBuilder(); fullPath.append( System.getProperty("user.home") + FILE_PATH + FileName + now ); if (FileType == 1) { fullPath.append(".xls"); } else if (FileType == 2) { fullPath.append(".xlsx"); } File excelFile = new File(fullPath.toString()); if (excelFile.exists()) { excelFile.delete(); try { excelFile.createNewFile(); } catch (IOException e) { throw new BaseException("PS12F01", "建立檔案[" + fullPath.toString() + "]失敗"); } } else { try { excelFile.createNewFile(); } catch (IOException e) { throw new BaseException("PS12F01", "建立檔案[" + fullPath.toString() + "]失敗"); } } return excelFile; } @SuppressWarnings("resource") public static void excel(int num, String tpId, String url) { // 匯出路徑 try { // 建立HSSFWorkbook物件 HSSFWorkbook wb = new HSSFWorkbook(); // 建立HSSFSheet物件 HSSFSheet sheet = wb.createSheet("農信通自助金融服務系統追償性清算明細表"); // 建立HSSFRow物件 HSSFRow row = sheet.createRow(0); // 設定表頭 row.createCell(0).setCellValue("序號"); TrcLog.log(LOG_FILE, "迴圈地表頭【序號】"); row.createCell(1).setCellValue("商戶編碼"); TrcLog.log(LOG_FILE, "迴圈地表頭【商戶編碼】"); row.createCell(2).setCellValue("商戶名稱"); TrcLog.log(LOG_FILE, "迴圈地表頭【商戶名稱】"); row.createCell(3).setCellValue("交易日期"); TrcLog.log(LOG_FILE, "迴圈地表頭【交易日期】"); row.createCell(4).setCellValue("交易流水號"); TrcLog.log(LOG_FILE, "迴圈地表頭【交易流水號】"); row.createCell(5).setCellValue("交易金額"); TrcLog.log(LOG_FILE, "迴圈地表頭【交易金額】"); TrcLog.log(LOG_FILE, "=================================資料日誌開始=================================="); // 迴圈遍歷集合,生成表格 String log = ""; for (int i = 0; i < num; i++) { row = sheet.createRow(row.getRowNum() + 1); row.createCell(0).setCellValue(i + 1); row.createCell(1).setCellValue( (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].MERT_ID") ); log = log + (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].MERT_ID") + " ,"; row.createCell(2).setCellValue( (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].MERT_NM") ); log = log + (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].MERT_NM") + " ,"; row.createCell(3).setCellValue( (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].ORD_T_DT") ); log = log + (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].ORD_T_DT") + " ,"; row.createCell(4).setCellValue( (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].ORD_S_N") ); log = log + (String)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].ORD_S_N") + " ,"; row.createCell(5).setCellValue( (Double)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].ORD_AMT") ); log = log + (Double)EPOper.get(tpId, "T_ORD_CHKA_ERR[" + i + "].ORD_AMT") + " ,"; } TrcLog.log(LOG_FILE, "【"+log+"】"); log = ""; TrcLog.log(LOG_FILE, "=================================資料日誌結束=================================="); // 輸出Excel檔案 FileOutputStream output = new FileOutputStream(url); output.flush(); wb.write(output); output.close(); } catch (Exception e) { TrcLog.log("download.log", e.toString()); e.printStackTrace(); } }