java后台动态生成excel

 excel导出实际效果图

序号会计期间类别编号类别名称公司领导董事会/总经理办公室企业发展与考核部运营与投资管理部安全质量技术管理部人力资源部财务部法律事务部纪检监察审计部党委工作部工会信息化部科技与国际合作部南方区域事业部北方区域事业部西南区域事业部华东区域事业部北京区域事业部工程技术研究院安保离退事务部存量土地开发项目办公室合计
12018080000100002投资性房地产   30316.15                 30316.15
2201808000010000300001房屋建筑物   611720.34                 611720.34
3201808000010000300002运输设备 16051.28             16235.97     32287.25
4201808000010000300003仪器及试验设备   20388.89                 20388.89
5201808000010000300004办公设备及家具 4844.46263.524676.59291.29511.33517.5376.976.9173.7576.914101.79314.06252.12438.151549.2742.722280.98469.321350.91 52308.47
6  合计 20895.74263.5687101.97291.29511.33517.5376.976.9173.7576.914101.79314.06252.12438.1517785.2442.722280.98469.321350.91 747021.1
public ActionForward monthDeprReportExport(ActionMapping mapping, ActionForm form, 
 HttpServletRequest request, HttpServletResponse response) {

		String organId = request.getParameter("organId");// 单位ID
		String classId = request.getParameter("classId"); // 资产分类ID
		String yearValue = request.getParameter("yearValue"); // 折旧年度
		String month = request.getParameter("month"); // 折旧月份
		String organName = "";
		if (Integer.parseInt(month) < 10) {
			month = "0" + month;
		}
		if (StringUtils.isBlank(organId)) {
			organId = request.getSession().getAttribute(LoginConstant.ORGANID).toString();
			OrganList organlist = (OrganList) this.depreciReportService.getObjById(OrganList.class, Integer.valueOf(organId));
			organName = organlist.getOrganName();
		} else {
			organName = request.getSession().getAttribute(LoginConstant.ORGANNAME).toString();
		}
		String yearMonth = yearValue + month;// 折旧年月
		List<Map<String, Object>> list = this.depreciReportService.getMonthDeprtReoprt(organId, classId, yearMonth);
		List<UnitList> unitArray = new ArrayList<UnitList>();
		list.remove(list.size() - 1);
		// 查询需要统计折旧的部门
		unitArray = this.depreciReportService.findBySql(UnitList.class, "select 'unit'+cast(unit_id as varchar) unit_no,unit_name from unit_list where is_enabled='1' and " + "(unit_id != unit_pid" + "	OR ((SELECT COUNT (*) FROM unit_list u WHERE u.unit_pid = unit_list.unit_pid) = 1))"
				+ " and organ_id=" + organId + " order by unit_id");
		// 计算合计行数据
		Map<String, Object> summaryMap = new HashMap<String, Object>();
		summaryMap.put("className", "合计");
		for (UnitList u : unitArray) {
			double d1 = 0d;
			for (Map<String, Object> temp : list) {
				if (temp.get(u.getUnitNo()) != null) {
					d1 += ((BigDecimal) temp.get(u.getUnitNo())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				}

			}
			BigDecimal b = new BigDecimal(d1);
			double df = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			summaryMap.put(u.getUnitNo(), df == 0 ? null : df);
		}
		list.add(summaryMap);
		// 手动创建excel
		try {
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename=" + yearMonth + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型
			// 创建工作薄
			WritableWorkbook workbook = Workbook.createWorkbook(os);
			// 创建新的一页
			WritableSheet sheet = workbook.createSheet("Sheet1", 0);
			// 定义标题格式 字体 下划线 斜体 粗体 颜色
			WritableCellFormat titleformat = new WritableCellFormat(new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK)); // 单元格定义
			titleformat.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
			// 定义label格式
			WritableCellFormat labelformat = new WritableCellFormat(new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK)); // 单元格定义
			labelformat.setAlignment(jxl.format.Alignment.RIGHT); // 设置对齐方式
			// 定义field格式
			WritableCellFormat fieldformat = new WritableCellFormat(new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK)); // 单元格定义
			fieldformat.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式

			sheet.mergeCells(0, 0, unitArray.size() + 4 , 1);// 合并第一二行
			sheet.addCell(new Label(0, 0, "分类部门汇总", titleformat));
			// 单位名称
			sheet.mergeCells(0, 2, 1, 3);
			sheet.addCell(new Label(0, 2, "单位名称:", labelformat));
			sheet.mergeCells(2, 2, 4, 3);
			sheet.addCell(new Label(2, 2, organName, fieldformat));
			// 时间
			sheet.mergeCells(5, 2, 6, 3);
			sheet.addCell(new Label(5, 2, "时间:", labelformat));
			sheet.mergeCells(7, 2, 9, 3);
			sheet.addCell(new Label(7, 2, yearMonth, fieldformat));
			// 汇总项目
			sheet.mergeCells(10, 2, 11, 3);
			sheet.addCell(new Label(10, 2, "汇总项目:", labelformat));
			sheet.mergeCells(12, 2, unitArray.size()+4, 3);
			sheet.addCell(new Label(12, 2, "本月折旧", fieldformat));
			int minColWidth = 10;
			// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
			sheet.addCell(new Label(0, 4, "序号"));
			sheet.addCell(new Label(1, 4, "会计期间"));
			sheet.addCell(new Label(2, 4, "类别编号"));
			sheet.addCell(new Label(3, 4, "类别名称"));
			sheet.setColumnView(0, minColWidth);
			sheet.setColumnView(1, minColWidth);
			sheet.setColumnView(2, 18);
			sheet.setColumnView(3, 18);
			Map<Integer, String> indexKeyRefMap = new HashMap<Integer, String>();
			for (int i = 0; i < unitArray.size(); i++) {
				UnitList unitlist = unitArray.get(i);
				indexKeyRefMap.put(i + 4, unitlist.getUnitNo());
				sheet.addCell(new Label(i + 4, 4, unitlist.getUnitName()));
				int colWidth = 2 * unitlist.getUnitName().length();
				if (colWidth < minColWidth) {
					colWidth = minColWidth;// 实际宽度如果小于最小宽度则使用最小宽度
				}
				sheet.setColumnView(i + 4, colWidth);
			}
			sheet.addCell(new Label(unitArray.size() + 4, 4, "合计"));
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				sheet.addCell(new Label(0, 5 + i, (i + 1) + ""));
				sheet.addCell(new Label(1, 5 + i, map.get("kjqj") != null ? map.get("kjqj").toString() : ""));
				sheet.addCell(new Label(2, 5 + i, map.get("classCode") != null ? map.get("classCode").toString() : ""));
				sheet.addCell(new Label(3, 5 + i, map.get("className") != null ? map.get("className").toString() : ""));
				double sumCol = 0d;
				for (int j = 0; j < unitArray.size(); j++) {
					Object obj = map.get(indexKeyRefMap.get(j + 4));
					String val_ = "";
					if (obj != null) {
						BigDecimal b = new BigDecimal(obj.toString());
						val_ = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
						sumCol +=  b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
					} else {
						val_ = "";
					}

					Label temp = new Label(j + 4, 5 + i, val_);
					sheet.addCell(temp);
				}
				//合计列
				BigDecimal bf = new BigDecimal(sumCol);
				sheet.addCell(new Label(unitArray.size() + 4, 5 + i, bf.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + ""));
			}
			// 把创建的内容写入到输出流中,并关闭输出流
			workbook.write();
			workbook.close();
			os.close();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 手动创建excel结束
		return null;
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值