poi操作Excel_Word

lishihuan大约 3 分钟

poi操作Excel_Word

word 导出pdf 总共可以有三种方式(https://www.bilibili.com/video/BV1He4y1778E/open in new window)

  • 文件另存为,指定pdf格式
  • 文件打印,可以导出pdf
  • 文件导出 pdf

1.

使用记录

对日期处理

	



	/**
	 * 不考虑 年份(闰年)/月份的限定
	 *
	 * @param dateStr
	 */
public static Date validationDateFormat(String dateStr) {
		Date date = null;
		if(StringUtils.isEmpty(dateStr)){
			return date;
		}
		dateStr = dateStr.replace(".", "-"); // 先统一格式
		String format1 = "yyyy-MM-dd HH:mm:ss";
		String format2 = "yyyy-MM-dd HH:mm";
		String format3 = "yyyy-MM-dd HH";
		String format4 = "yyyy-MM-dd";
		String YYYY = "(\\d{4})"; // 年份  "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})"; // 年份  0001 - 9999
		String MM = "((0?[0-9])|([1][0-2]))"; // 月份  匹配01到12的月份,前面的0可以省略,如1或01。
		String dd = "(0?[1-9]|[12][0-9]|3[01])"; // 天  匹配01到31的日期,前面的0可以省略,如1或01。
		String HH = "((0?[0-9])|([1][0-9])|([2][0-4]))?";// 小时   匹配00到24的小时,前面的0可以省略,如1或01。
		String mm = "([0-5]?[0-9])?";// 分钟
		String ss = "([0-5]?[0-9])?";// 秒
		String timeRegex1 = "^(" + YYYY + "-" + MM + "-" + dd + ")\\s+(" + HH + "\\:" + mm +"\\:" + ss + ")$"; // 验证 yyyy-MM-dd HH:mm:ss
		String timeRegex2 = "^(" + YYYY + "-" + MM + "-" + dd + ")\\s+(" + HH + "\\:" + mm+ ")$"; // 验证 yyyy-MM-dd HH:mm
		String timeRegex3 = "^(" + YYYY + "-" + MM + "-" + dd + ")\\s+(" + HH + ")$"; // 验证 yyyy-MM-dd HH
		String timeRegex4 = "^(" + YYYY + "-" + MM + "-" + dd + ")$"; // 验证 yyyy-MM-dd
		boolean flag1 = Pattern.matches(timeRegex1, dateStr);
		boolean flag2 = Pattern.matches(timeRegex2, dateStr);
		boolean flag3 = Pattern.matches(timeRegex3, dateStr);
		boolean flag4 = Pattern.matches(timeRegex4, dateStr);
		if (flag1) {
			date = formatDate(dateStr, date, format1);
		}else if(flag2){
			date = formatDate(dateStr, date, format2);
		}else if(flag3){
			date = formatDate(dateStr, date, format3);
		}else if(flag4){
			date = formatDate(dateStr, date, format4);
		}
		return date;
	}

// 备份
	public static Date validationDateFormat_backup(String dateStr) {
		Date date = null;
		if(StringUtils.isEmpty(dateStr)){
			return date;
		}
		// 1. 匹配日期格式:yyyy-MM-dd HH:mm:ss
		String format1 = "yyyy-MM-dd HH:mm:ss";
		String format2 = "yyyy-MM-dd";
		String format3 = "yyyy.MM.dd";
		String YYYY = "(\\d{4})"; // 年份  "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})"; // 年份  0001 - 9999
		String mm = "((0?[0-9])|([1][0-2]))";
		String dd = "(0?[1-9]|[12][0-9]|3[01])";
		String HH_mm_ss = "((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))";
		String timeRegex1 = "^(" + YYYY + "-" + mm + "-" + dd + ")\\s+(" + HH_mm_ss + ")$"; // 验证 yyyy-MM-dd HH:mm:ss
		String timeRegex2 = "^(" + YYYY + "-" + mm + "-" + dd + ")$"; // 验证 yyyy-MM-dd
		String timeRegex3 = "^(" + YYYY + "." + mm + "." + dd + ")$"; // 验证 yyyy.MM.dd
		boolean flag1 = Pattern.matches(timeRegex1, dateStr);
		boolean flag2 = Pattern.matches(timeRegex2, dateStr);
		boolean flag3 = Pattern.matches(timeRegex3, dateStr);
		if (flag1) {
			date = formatDate(dateStr, date, format1);
		}else if(flag2){
			date = formatDate(dateStr, date, format2);
		}else if(flag3){
			date = formatDate(dateStr, date, format3);
		}
		return date;
	}

/**
	 * 对时间校验,并且实现赋值
	 *
	 * @param datestr:          日期字符串
	 * @param field:需要赋值的日期字段名称
	 * @param obj:              实体对象
	 * @param tips:提示
	 * @param sdf:格式化日期         SimpleDateFormat 对象
	 * @return : 返回 校验结果
	 */
	public static String validationDate(String datestr, String field, Object obj, String tips, SimpleDateFormat sdf) {
		if (StringUtils.isNotBlank(datestr)) {
			try {
				Date date = ExcelDateUtil.validationDateFormat(datestr);
				if(date==null){
					return tips + "格式不正确;";
				}
				Method method = obj.getClass().getMethod(field, Date.class);
				method.invoke(obj, date);
			} catch (Exception e) {
				e.printStackTrace();
				return tips + "格式不正确;";
			}
		}
		return "";
	}

// 调用
// dataDescribe += ExcelImportVerification.validationDate(item.getFhzCreateTimeStr(),"setFhzCreateTime",item,"防护桩修建时间" ,sdf);

Java 导出 Excel 列号数字与字母互相转换工具

https://www.cnblogs.com/yysbolg/p/10323124.htmlopen in new window

public static void main(String[] args) {
		String colstr = "AA";
		int colIndex = excelColStrToNum(colstr, colstr.length());
		System.out.println("'" + colstr + "' column index of " + colIndex);

		colIndex = 26;
		colstr = excelColIndexToStr(colIndex);
		System.out.println(colIndex + " column in excel of " + colstr);
	}

	/**
	 * Excel column index begin 1
	 * @param colStr
	 * @param length
	 * @return
	 */
	public static int excelColStrToNum(String colStr, int length) {
		int num = 0;
		int result = 0;
		for(int i = 0; i < length; i++) {
			char ch = colStr.charAt(length - i - 1);
			num = (int)(ch - 'A' + 1) ;
			num *= Math.pow(26, i);
			result += num;
		}
		return result;
	}

	/**
	 * 顺序号转英文字母;1→A,2→B,26→Z,27→AA,702→ZZ,703→AAA …
	 * Excel column index begin 1
	 * @param columnIndex
	 * @return
	 */
	public static String excelColIndexToStr(int columnIndex) {
		if (columnIndex <= 0) {
			return null;
		}
		String columnStr = "";
		columnIndex--;
		do {
			if (columnStr.length() > 0) {
				columnIndex--;
			}
			columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
			columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
		} while (columnIndex > 0);
		return columnStr;
	}

2. 导入,对数据处理的一个想法

	private void getVlaue(ZjjcMaterialInfo item) {
		String[] fields = {"tonnage", "kn", "mpa", "rated_oil", "calibrationCoef", "wybgg",
				"ylcgqgg", "sjcsfs", "gzdy", "ybkzfs", "ptsysbbz", "dqgg"};

		for (String field : fields) {
			String value = getProperty(item, field);
			// 业务处理
		}
	}



	private String getProperty(ZjjcMaterialInfo item, String fieldName) {
		try {
			Method method = item.getClass().getMethod("get" + StringUtils.capitalize(fieldName));
			return (String) method.invoke(item);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

3. 多sheel导入