Java简单处理Excel

Java实现Excel2007创建基本样式,为特定单元设置背景颜色样式,对日期格式进行处理,并对单元格进行简单判空处理

/**
	 * 创建基本样式
	 * @param wb
	 * @return
	 */
	private static CellStyle createBasicStyle(SXSSFWorkbook wb) {
		CellStyle cs = wb.createCellStyle();
		cs.setAlignment(CellStyle.ALIGN_CENTER);
		cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cs.setBorderBottom(CellStyle.BORDER_THIN);
		cs.setBorderLeft(CellStyle.BORDER_THIN);
		cs.setBorderTop(CellStyle.BORDER_THIN);
		cs.setBorderRight(CellStyle.BORDER_THIN);
		cs.setWrapText(true);

		Font font = wb.createFont();
		font.setFontHeightInPoints((short) 9);
		font.setFontName("宋体");
		cs.setFont(font);

		return cs;
	}

	/**
	 * 创建黄色背景样式
	 * @param wb
	 * @param baseStyle
	 * @return
	 */
	private static CellStyle createYellowCellStyle(SXSSFWorkbook wb, CellStyle baseStyle) {
		CellStyle yellowColorcs = wb.createCellStyle();
		yellowColorcs.cloneStyleFrom(baseStyle);
		yellowColorcs.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
		yellowColorcs.setFillPattern(CellStyle.SOLID_FOREGROUND);
		return yellowColorcs;
	}

	/**
	 * 创建红色背景样式
	 * @param wb
	 * @param baseStyle
	 * @return
	 */
	private static CellStyle createRedCellStyle(SXSSFWorkbook wb, CellStyle baseStyle) {
		CellStyle redColorcs = wb.createCellStyle();
		redColorcs.cloneStyleFrom(baseStyle);
		redColorcs.setFillForegroundColor(IndexedColors.RED.getIndex());
		redColorcs.setFillPattern(CellStyle.SOLID_FOREGROUND);
		return redColorcs;
	}

	/**
	 * 创建日期样式
	 * @param wb
	 * @param baseStyle
	 * @return
	 */
	private static CellStyle createDateCellStyle(SXSSFWorkbook wb, CellStyle baseStyle) {
		CellStyle datecs = wb.createCellStyle();
		datecs.cloneStyleFrom(baseStyle);
		XSSFDataFormat format = (XSSFDataFormat) wb.createDataFormat();
		datecs.setDataFormat(format.getFormat("yyyy/m/d"));
		return datecs;
	}
	/**
	 * 2007版本
	 * @param wb 被填充工作簿对象
	 * @param dataVector 
	 * @param dataList 
	 * @return wb
	 * PWJ对单元格数据进行判断,是否符合条件,符合条件处理背景颜色
	 */
	public static SXSSFWorkbook fillUpExlBackgroundColor2007(SXSSFWorkbook wb, String[] dataVector, List<Map<String, Object>> dataList) {
		Sheet sheet = wb.getSheetAt(0);
		int lastLine = sheet.getLastRowNum(); // 获取最后一行的行号

		// 创建基本样式
		CellStyle cs = createBasicStyle(wb);

		// 创建特殊单元格样式
		CellStyle yellowColorcs = createYellowCellStyle(wb, cs);
		CellStyle redColorcs = createRedCellStyle(wb, cs);
		CellStyle datecs = createDateCellStyle(wb, cs);

		for (int i = 0; i < dataList.size(); i++) {
			Row row = sheet.createRow(lastLine + i + 1);
			for (int j = 0; j < dataVector.length; j++) {
				String val = nullCheck(dataList.get(i).get(dataVector[j]));
				Cell cell = row.createCell(j);

				if (IsNumeric(val)) {
					cell.setCellValue(doubleNullCheck(val));
					cell.setCellStyle(cs);
				} else if (isDate(val)) {
					cell.setCellValue(DateUtil.stringToDate(val));
					cell.setCellStyle(datecs);
				} else {
					cell.setCellValue(val);
					cell.setCellStyle(cs);
				}

				// 根据特定条件设置单元格样式
				if (j == 3) { // 假设条件基于第四列(索引为3)
					if (val.equals("报废处理")) {
						cell.setCellStyle(redColorcs);
					} else if (val.equals("退回管理")) {
						cell.setCellStyle(yellowColorcs);
					}
				}
			}
		}
		return wb;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值