XSSF导出.xlsx文件 格式设置及存储

	public JResponseObject ZRZL_HZDC(JConnection conn, JParamObject PO, JResponseObject RO)
			throws InvalidFormatException {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
		String time = dateFormat.format(date);

		String wldw = PO.GetValueByParamName("wldw", "");
		String ny = PO.GetValueByParamName("ny", "");
		Row row = null;
		Statement stmt1 = null, stmt2 = null, stmt3 = null;
		ResultSet rs1 = null, rs2 = null, rs3 = null;
		StringBuffer vsSql = new StringBuffer();
		StringBuffer whereSql = new StringBuffer();
		String dwmc = "", jhnd = "";//
		int czzs = 0, zclb = 0;
		Double fwjzmj = 0.00, fpjebhs = 0.00, ygnzj = 0.00, zcjz = 0.00, zcyz = 0.00;
		System.out.println(PO.GetValueByParamName("ZCLB", ""));
		String JHLX = PO.GetValueByParamName("JHLX", "");// 计划类型
		String XMLX = PO.GetValueByParamName("XMLX", "");// 项目类型
		String ZCLB = PO.GetValueByParamName("ZCLB", "");// 资产类别
		String ZLLX = PO.GetValueByParamName("ZLLX", "");// 租赁类型
		String SPJD = PO.GetValueByParamName("SPJD", "");// 审批进度
		String JHND = PO.GetValueByParamName("JHND", "");// 审批进度

		try {
			jhnd = PO.GetValueByParamName("JHND", "");
			// 读取Excel模板
			InputStream inputStream = new FileInputStream(new File(EAI.LocalUserHome + "附件地址/muban/资金集中票据发票信息表.xlsx"));
			Workbook workbook = WorkbookFactory.create(inputStream);
			// 获取工作表
			Sheet sheet = workbook.getSheetAt(0);
			sheet.setColumnWidth(0, 15000);
			sheet.setColumnWidth(1, 3000);
			sheet.setColumnWidth(2, 6500);
			sheet.setColumnWidth(3, 3000);
			sheet.setColumnWidth(4, 4000);
			sheet.setColumnWidth(5, 4000);
			sheet.setColumnWidth(6, 4000);
			Font font = workbook.createFont();
			font.setFontHeightInPoints((short) 10);
			font.setFontName("宋体");
			XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
			XSSFCellStyle btstyle = (XSSFCellStyle) workbook.createCellStyle();
			style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			style.setFont(font);
			style.setBorderBottom(BorderStyle.THIN);
			style.setBorderLeft(BorderStyle.THIN);
			style.setBorderRight(BorderStyle.THIN);
			style.setBorderTop(BorderStyle.THIN);
			btstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			btstyle.setFont(font);
			btstyle.setBorderBottom(BorderStyle.THIN);
			btstyle.setBorderLeft(BorderStyle.THIN);
			btstyle.setBorderRight(BorderStyle.THIN);
			btstyle.setBorderTop(BorderStyle.THIN);
			btstyle.setAlignment(HorizontalAlignment.CENTER);
			if (!"null".equals(JHLX) && !"".equals(JHLX) && !"[]".equals(JHLX)) {// 计划类型
				JHLX = JHLX.replaceAll("\\[", "");
				JHLX = JHLX.replaceAll("]", "");
				JHLX = JHLX.replaceAll(" ", "");
				JHLX = JHLX.replaceAll(",", "','");
				whereSql.append("   and T.F_JHLX in( '" + JHLX + "' ) ");
			}
			if (!"null".equals(JHND) && !"".equals(JHND)) {// 计划年度
				whereSql.append("   and T.F_JHND = '" + JHND + "'  ");
			}
			if (!"null".equals(XMLX) && !"".equals(XMLX) && !"[]".equals(XMLX)) {// 项目类型
				XMLX = XMLX.replaceAll("\\[", "");
				XMLX = XMLX.replaceAll("]", "");
				XMLX = XMLX.replaceAll(" ", "");
				XMLX = XMLX.replaceAll(",", "','");
				whereSql.append("   and T.F_XMLX IN ( '" + XMLX + "' ) ");
			}

			if (!"null".equals(ZCLB) && !"".equals(ZCLB) && !"[]".equals(ZCLB)) {// 资产类别
				ZCLB = ZCLB.replaceAll("\\[", "");
				ZCLB = ZCLB.replaceAll("]", "");
				ZCLB = ZCLB.replaceAll(" ", "");
				ZCLB = ZCLB.replaceAll(",", "','");
				whereSql.append("   and MX.F_ZCLB in( '" + ZCLB + "' )  ");
			}

			if (!"null".equals(ZLLX) && !"".equals(ZLLX) && !"[]".equals(ZLLX)) {// 租赁类型
				ZLLX = ZLLX.replaceAll("\\[", "");
				ZLLX = ZLLX.replaceAll("]", "");
				ZLLX = ZLLX.replaceAll(" ", "");
				ZLLX = ZLLX.replaceAll(",", "','");
				whereSql.append("   and MX.F_ZLLX in( '" + ZLLX + "' )  ");
			}
			if (!"null".equals(SPJD) && !"".equals(SPJD) && !"[]".equals(SPJD)) {// 资产状态
				SPJD = SPJD.replaceAll("\\[", "");
				SPJD = SPJD.replaceAll("]", "");
				SPJD = SPJD.replaceAll(" ", "");
				whereSql.append("   and T.F_DJZT IN ('" + SPJD.replaceAll(",", "','") + "' ) ");
			}
			XSSFCellStyle titleStyle = (XSSFCellStyle) workbook.createCellStyle(); // 标题样式
			XSSFCellStyle zsStyle = (XSSFCellStyle) workbook.createCellStyle(); // 注释样式
			titleStyle.setAlignment(HorizontalAlignment.CENTER);
			titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			Font ztFont = workbook.createFont();
			Font ztFont1 = workbook.createFont();
			ztFont.setItalic(false); // 设置字体为斜体字
			// ztFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色”
			ztFont.setFontHeightInPoints((short) 20); // 将字体大小设置为18px
			ztFont.setFontName("宋体"); // 将“宋体”字体应用到当前单元格上
			ztFont.setBold(true); // 加粗
			// ztFont.setUnderline(Font.U_DOUBLE);//
			// 添加(Font.U_SINGLE单条下划线/Font.U_DOUBLE双条下划线)
			// ztFont.setStrikeout(true);// 是否添加删除线
			titleStyle.setFont(ztFont);
			zsStyle.setAlignment(HorizontalAlignment.RIGHT);
			ztFont1.setItalic(false); // 设置字体为斜体字
			// ztFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色”
			ztFont1.setFontHeightInPoints((short) 10); // 将字体大小设置为18px
			zsStyle.setFont(ztFont1);
			String titleName = jhnd + "年拟租入资产汇总表";

			int rownum = 0;// 计数
			vsSql.setLength(0);
			vsSql.append(
					" SELECT SO.ORG_NAME F_DWMC,SUM(1) F_CZZS,NVL(SUM(MX.F_FWJZMJ),0) F_FWJZMJ,COUNT(DISTINCT MX.F_ZCLB) F_ZCLB,NVL(SUM(MX.F_ZCYZ),0) F_ZCYZ,NVL(SUM(MX.F_ZCJZ),0) F_ZCJZ,NVL(SUM(MX.F_YGNZJ),0)  F_YGNZJ FROM ZCYW_ZLYWMX MX   LEFT JOIN SYS_ORG SO ON MX.F_LRZXBH = SO.ORG_ID LEFT JOIN  ZCYW_ZLYWTT T ON  T.F_DJBH  = MX.F_DJBH  WHERE MX.F_DJBH LIKE 'ZR%'  ");
			vsSql.append(whereSql);
			vsSql.append("  GROUP BY F_LRZXBH ,SO.ORG_NAME ");

			System.out.println(vsSql.toString());

			stmt2 = conn.createStatement();
			rs2 = stmt2.executeQuery(vsSql.toString());
			stmt3 = conn.createStatement();
			row = sheet.createRow(0);
			// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
			Cell cell = row.createCell(0);
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
			cell.setCellValue(titleName);
			cell.setCellStyle(titleStyle);
			// 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
			rownum++;
			row = sheet.createRow(rownum);
			cell = row.createCell(0);
			sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
			cell.setCellValue("金额单位:元");
			cell.setCellStyle(zsStyle);
			rownum++;
			row = sheet.createRow(rownum);
			cell = row.createCell(0);
			cell.setCellValue("单位名称");
			cell.setCellStyle(btstyle);
			cell = row.createCell(1);
			cell.setCellValue("资产数量");
			cell.setCellStyle(btstyle);
			cell = row.createCell(2);
			cell.setCellValue("租赁房屋建筑面积(平方米)");
			cell.setCellStyle(btstyle);
			cell = row.createCell(3);
			cell.setCellValue("资产类别数");
			cell.setCellStyle(btstyle);
			cell = row.createCell(4);
			cell.setCellValue("资产原值");
			cell.setCellStyle(btstyle);
			cell = row.createCell(5);
			cell.setCellValue("资产净值");
			cell.setCellStyle(btstyle);
			cell = row.createCell(6);
			cell.setCellValue("预估年租金");
			cell.setCellStyle(btstyle);
			rownum++;
			while (rs2.next()) {

				row = sheet.createRow(rownum);
				row.setHeightInPoints(12);
				dwmc = rs2.getString("F_DWMC");
				czzs = rs2.getInt("F_CZZS");
				fwjzmj = rs2.getDouble("F_FWJZMJ");
				zclb = rs2.getInt("F_ZCLB");
				zcyz = rs2.getDouble("F_ZCYZ");
				zcjz = rs2.getDouble("F_ZCJZ");
				ygnzj = rs2.getDouble("F_YGNZJ");

				cell = row.createCell(0);
				cell.setCellValue(dwmc);
				cell.setCellStyle(style);

				cell = row.createCell(1);
				cell.setCellValue(czzs);
				cell.setCellStyle(style);

				cell = row.createCell(2);
				cell.setCellValue(fwjzmj);
				cell.setCellStyle(style);

				cell = row.createCell(3);
				cell.setCellValue(zclb);
				cell.setCellStyle(style);

				cell = row.createCell(4);
				cell.setCellValue(zcyz);
				cell.setCellStyle(style);

				cell = row.createCell(5);
				cell.setCellValue(zcjz);
				cell.setCellStyle(style);

				cell = row.createCell(6);
				cell.setCellValue(ygnzj);
				cell.setCellStyle(style);

				rownum++;
				sheet.shiftRows(rownum, rownum + 10, 1);
			}
			rownum++;
			//合计行
			row = sheet.createRow(rownum);
			int totalCell = sheet.getRow(3).getLastCellNum();// 获取第四行列的数量
			String colString;//长度转成ABC后的列
			String sumString;//求和公式
			cell = row.createCell(0);
			cell.setCellValue("合计:");
			cell.setCellStyle(style);
			for (int i = 1; i < totalCell; i++) {// 我从第二列开始每列都执行求和操作 因此i=2
			    cell = row.createCell(i);// 创建单元格
			    cell.setCellStyle(style);// 设置单元格样式
			    colString = CellReference.convertNumToColString(i);  //长度转成ABC列
			    //求和公式 求i9至i12单元格的总和
			    sumString = "SUM(" + colString + "4:" + colString + rownum + ")";
			    cell.setCellFormula(sumString);// 把公式塞入合计列
			}
			
			sheet.shiftRows(rownum, rownum + 10, -1);
			// 导出Excel文件
			String outputLJ =  jhnd + "拟租入资产汇总表(" + time + ")";
			FileOutputStream outputStream = new FileOutputStream(EAI.LocalUserHome + "WEB-INF/classes/static/FJSC/TempFile/" + outputLJ + ".xlsx");
			workbook.setForceFormulaRecalculation(true);// 执行公式
			workbook.write(outputStream);
			workbook.close();
			RO.setErrorString(outputLJ);
			RO.setErrorCode(1);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			RO.setErrorCode(0);
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EncryptedDocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (null != rs1) {
					rs1.close();
				}
				if (null != stmt1) {
					stmt1.close();
				}
				if (null != rs2) {
					rs2.close();
				}
				if (null != stmt2) {
					stmt2.close();
				}
				if (null != rs3) {
					rs3.close();
				}
				if (null != stmt3) {
					stmt3.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return RO;
	}

效果如下:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值