java后台动态生成excel

 excel导出实际效果图

序号 会计期间 类别编号 类别名称 公司领导 董事会/总经理办公室 企业发展与考核部 运营与投资管理部 安全质量技术管理部 人力资源部 财务部 法律事务部 纪检监察审计部 党委工作部 工会 信息化部 科技与国际合作部 南方区域事业部 北方区域事业部 西南区域事业部 华东区域事业部 北京区域事业部 工程技术研究院 安保离退事务部 存量土地开发项目办公室 合计
1 201808 0000100002 投资性房地产       30316.15                                   30316.15
2 201808 000010000300001 房屋建筑物       611720.34                                   611720.34
3 201808 000010000300002 运输设备   16051.28                           16235.97           32287.25
4 201808 000010000300003 仪器及试验设备       20388.89                                   20388.89
5 201808 000010000300004 办公设备及家具   4844.46 263.5 24676.59 291.29 511.33 517.53 76.9 76.9 173.75 76.9 14101.79 314.06 252.12 438.15 1549.27 42.72 2280.98 469.32 1350.91   52308.47
6     合计   20895.74 263.5 687101.97 291.29 511.33 517.53 76.9 76.9 173.75 76.9 14101.79 314.06 252.12 438.15 17785.24 42.72 2280.98 469.32 1350.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;
	}

猜你喜欢

转载自blog.csdn.net/SUPERLEIJIAWEI/article/details/82149700
今日推荐