java实现导出EXCEL文件

/**
	 * 方法:下载差错明细文件 作者:王元晨
	 */
	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();
		}
	}

猜你喜欢

转载自blog.csdn.net/A15712399740/article/details/84759719