java根据模板导出报表

15 篇文章 0 订阅
10 篇文章 0 订阅

struts2代码

/**
	 * 写报表数据
	 * @return
	 */
	public String exportManagerAddressList() {
		QueryObj queryObj = QueryObj.parseQueryObj(this);
		try {
			fileName = managerAddressListBiz.findManagerAddressList(queryObj,ids);
			downloadFileName = managerAddressListBiz.resultIDepartment(queryObj);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 保存报表
	 * @return
	 */
	public String downloadManagerAddressList() {
		try {
			File file = new File(Config.getInstance().getTempPath() + File.separator + fileName);
			FileInputStream fis = new FileInputStream(file);
			byte[] data = new byte[fis.available()];
			fis.read(data);
			stream = new ByteArrayInputStream(data);
			downloadFileName = "团队通讯录";
			//这里得转换字符,否则页面有下载异常
			fileName = new String(downloadFileName.getBytes("GBK"), "ISO8859-1");
			fis.close();
			file.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

biz代码

public String findManagerAddressList(QueryObj queryObj,String ids) {
		FmsDepartment dept = fmsDepartmentDao.load(Long.parseLong(queryObj
				.getQueryValue("deptId")));

		String exportFileName = getExportFileName(dept.getId().toString());
		try {
			InputStream template = this
					.getClass()
					.getResourceAsStream(
							"/com/sf/module/fmsbackstage/META-INF/pages/template/managerAddressList.xls");
			HSSFWorkbook workbook = new HSSFWorkbook(template);

			String date = CommonUtil.getYYYY_MM_DD_HH24Fmt().format(new Date());
			Collection<ManagerAddressList> list = new ArrayList<ManagerAddressList>();
			
			if (ids != null && !"".equals(ids)) {
				list = this.managerAddressListDao.findCheckBy(ids);
			} else {
				list = this.managerAddressListDao.findBy(queryObj, super
						.getCurrentUser().getId());
			}
			HSSFSheet sheet = workbook.getSheetAt(0);
			workbook.setSheetName(workbook.getSheetIndex(sheet), dept
					.getDeptName());
			writeHeadToSheet(sheet, dept.getDeptName(), date);

			fillReportData(sheet, list);

			String fileName = Config.getInstance().getTempPath()
					+ File.separator + exportFileName;
			FileOutputStream fos = new FileOutputStream(fileName);
			workbook.write(fos);
			fos.close();
			template.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return exportFileName;
	}

	public String resultIDepartment(QueryObj queryObj) {
		// 用户选择的网点
		FmsDepartment dept = fmsDepartmentDao.load(Long.parseLong(queryObj
				.getQueryValue("deptId")));
		if (dept != null) {
			return dept.getDeptName();
		} else {
			return "";
		}
	}

	/**
	 * 写头数据
	 * 
	 * @param sheet
	 * @param deptName
	 * @param date
	 */
	private void writeHeadToSheet(HSSFSheet sheet, String deptName, String date) {
		sheet.getRow(1).getCell((int) 4).setCellValue(
				new HSSFRichTextString(date));

		sheet.getRow(1).getCell((int) 1).setCellValue(
				new HSSFRichTextString(deptName));
	}

	/**
	 * 生成报表数据
	 * 
	 * @param sheet
	 * @param data
	 */
	private void fillReportData(HSSFSheet sheet,
			Collection<ManagerAddressList> data) {
		int rownum = 2;
		List<HSSFCellStyle> style = new ArrayList<HSSFCellStyle>(0);
		HSSFRow rowModel = sheet.getRow(3);
		for (int i = 0; i < 11; i++) {
			style.add(rowModel.getCell(i+1).getCellStyle());
		}
		sheet.removeRow(rowModel);
		for (ManagerAddressList bean : data) {
			HSSFRow row = sheet.getRow(++rownum);
			if (row == null) {
				row = sheet.createRow(rownum);
			}
			int column = 0;
//			// 序号
//			HSSFCell cell0 = (row.getCell(column) == null ? row
//					.createCell(column) : row.getCell(column));
//			column++;
//			cell0.setCellStyle(style.get(0));
//			cell0.setCellValue(new HSSFRichTextString());
			// 所属网点
			HSSFCell cell1 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell1.setCellStyle(style.get(column));
			if (bean.getDepartment().getDeptName() != null) {
				cell1.setCellValue(new HSSFRichTextString(bean.getDepartment()
						.getDeptName()));
			}
			column++;
			// 工号
			HSSFCell cell2 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell2.setCellStyle(style.get(column));
			if (bean.getEmpCode() != null) {
				cell2.setCellValue(new HSSFRichTextString(bean.getEmpCode()));
			}
			column++;
			// 姓名
			HSSFCell cell3 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell3.setCellStyle(style.get(column));
			if (bean.getEmpName() != null) {
				cell3.setCellValue(new HSSFRichTextString(bean.getEmpName()));
			}
			column++;
			// 性别
			HSSFCell cell4 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell4.setCellStyle(style.get(column));
			if (bean.getSex() != null) {
				String[] sex = new String[] { "男", "女" };
				cell4.setCellValue(new HSSFRichTextString(sex[bean.getSex()
						.intValue()]));
			}
			column++;
			// 办公电话
			HSSFCell cell5 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell5.setCellStyle(style.get(column));
			if (bean.getMobilephone() != null) {
				
				String str = bean.getMobilephone();
				if(bean.getAreaCode() != null){
					str = bean.getAreaCode() + "-" + str;
				}
				cell5.setCellValue(new HSSFRichTextString(str));
			}
			column++;
			// 手机号码
			HSSFCell cell6 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell6.setCellStyle(style.get(column));
			if (bean.getMobilephone() != null) {
				cell6
						.setCellValue(new HSSFRichTextString(bean
								.getMobilephone()));
			}
			column++;
			// 专职或兼职
			HSSFCell cell7 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell7.setCellStyle(style.get(column));
			if (bean.getPosition() != null) {
				String[] position = new String[] { "专职", "兼职" };
				cell7.setCellValue(new HSSFRichTextString(position[bean
						.getPosition().intValue()]));
			}
			column++;
			// 现岗位
			HSSFCell cell8 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell8.setCellStyle(style.get(column));
			if (bean.getNowJob() != null) {
				cell8.setCellValue(new HSSFRichTextString(bean.getNowJob()));
			}
			column++;
			// 任职日期
			HSSFCell cell8_ = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell8_.setCellStyle(style.get(column));
			if (bean.getInDate() != null) {
				cell8_.setCellValue(new HSSFRichTextString(CommonUtil.getYYYY_MM_DD_HH24Fmt().format(bean.getInDate())));
			}
			column++;
			// E-mail
			HSSFCell cell9 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell9.setCellStyle(style.get(column));
			if (bean.getEmail() != null) {
				cell9.setCellValue(new HSSFRichTextString(bean.getEmail()));
			}
			column++;
			// 备注
			HSSFCell cell10 = (row.getCell(column) == null ? row
					.createCell(column) : row.getCell(column));
			cell10.setCellStyle(style.get(column));
			if (bean.getRemark() != null) {
				cell10.setCellValue(new HSSFRichTextString(bean.getRemark()));
			}
			column++;
		}
	}

	/**
	 * 生成文件名称
	 * 
	 * @param oilCard
	 * @return
	 */
	private String getExportFileName(String deptId) {
		if (deptId == null) {
			deptId = "0";
		}
		return "managerAddressList" + deptId + "_" + System.currentTimeMillis()
				+ ".xls";
	}

jsp--导出

//导出数据准备
		function onExport() {
			if (queryView.getForm().isValid() == false) {
				Ext.MessageBox.alert('${app:i18n('prompt')}', '${app:i18n('validateError')}');
			} else {		
				
				var records = gridView.getSelectionModel().getSelection();
				var ids = '';
				for(var i = 0; i < records.length; i++) {
				  ids += records[i].data.id + ',';
				}
				
				queryView.getForm().doAction('submit',{
					url:"exportManagerAddressList.action",
					waitMsg:"${app:i18n('exportingFile')}",
					params: {'ids' : ids},
					success:exportshowResult,
					failure:function(form, action){
						Ext.MessageBox.alert('ERROR',action.result.error);
					}
				});				
			}
		}
		//保存导出报表
		function exportshowResult(form, action) {
			var fileName = action.result.fileName;
			var downloadFileName = action.result.downloadFileName;
			var url = "downloadManagerAddressList.action?fileName=" + fileName + "&downloadFileName="+downloadFileName;
			window.location = url;
		}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值