首页 > 代码库 > Java 根据固定格式的Excel生成实体类

Java 根据固定格式的Excel生成实体类

/**
 * Excel文档转对象
 * 
 * @author dxm
 *
 */
public class ExcelToObject {

	/**
	 * 转换
	 * 
	 * @param file
	 */
	public static void convert(File file, String savePath, String pack) {

		// Excel是否存在
		if (null == file || !file.exists()) {
			return;
		}

		// 创建存放路径目录
		File save = new File(savePath);
		if (!save.exists()) {
			save.mkdirs();
		}

		try {

			// Excel文件名
			String fileName = file.getName();

			// 建立输入流
			InputStream input = new FileInputStream(file);
			Workbook wb = null;

			// 根据文件格式(2003或者2007)来初始化
			if (fileName.endsWith("xlsx")) {
				wb = new XSSFWorkbook(input);
			} else {
				wb = (Workbook) new HSSFWorkbook(input);
			}

			// 获得第一个表单
			Sheet sheet = wb.getSheetAt(0);

			// 文件输出流
			FileOutputStream fos = null;

			// 获得第一个表单的迭代器
			Iterator<Row> rows = sheet.rowIterator();

			// 插入语句
			List<String> typeList = new ArrayList<String>();
			List<String> nameList = new ArrayList<String>();
			StringBuffer sb = new StringBuffer();

			// 包名
			pack = "package " + pack + ";\n\n";

			// 获得第一个表单的迭代器
			while (rows.hasNext()) {

				// 获得行数据
				Row row = rows.next();

				// 列0,1,2表示属性名,类型,说明
				Cell cell0 = row.getCell(0);
				Cell cell1 = row.getCell(1);
				Cell cell2 = row.getCell(2);
				String name = cell0.getStringCellValue().trim();
				String type = cell1.getStringCellValue().trim();
				String mark = cell2.getStringCellValue().trim();

				// 过滤空数据
				if (name.isEmpty()) {
					continue;
				}

				// 过滤说明
				if (name.equals("字段")) {
					continue;
				}

				// 新表开始
				int index = type.indexOf("t_");
				if (-1 != index) {

					// 写入上个类文件
					if (null != fos) {
						getSet(typeList, nameList, sb);
						fos.write(sb.toString().getBytes("UTF-8"));
						fos.close();
						typeList.clear();
						nameList.clear();
						sb = new StringBuffer();
					}

					// 类首字母大写
					type = type.substring(index + 2);
					String c = String.valueOf(type.charAt(0));
					c = c.toUpperCase();
					type = c + type.substring(1);

					// 删除旧的类文件
					File f = new File(savePath + "/" + type + ".java");
					if (f.exists()) {
						f.delete();
					}

					// 创建文件输出流
					fos = new FileOutputStream(f);

					// 拼接输出语句
					sb.append(pack);
					sb.append("/**\n * ");
					sb.append(name);
					sb.append("\n * \n * @author juling\n *\n */\n");
					sb.append("public class ");
					sb.append(type);

					// 拼接继承类
					if (!mark.isEmpty()) {
						sb.append(" extends ");
						sb.append(mark);
						sb.append(" {\n");
						sb.append("\tprivate static final long serialVersionUID = -1L;\n");
						if (mark.equals("VData")) {
							sb.insert(pack.length(), "import com.mohe.common.db.VData;\n\n");
						}
					} else {
						sb.append(" {\n");
					}

					continue;
				}

				// 拼接输出语句
				sb.append("\n");
				sb.append("\t// ");
				sb.append(mark);
				sb.append("\n");
				sb.append("\tpublic ");
				sb.append(type);
				sb.append(" ");
				sb.append(name);
				sb.append(";\n");

				// 如果为时间类型插入导入java.util.Date包
				if (type.equals("Date")) {
					sb.insert(pack.length(), "import java.util.Date;\n\n");
				}

				typeList.add(type);
				nameList.add(name);
			}

			// 写入最后一个类文件
			if (null != fos) {
				getSet(typeList, nameList, sb);
				fos.write(sb.toString().getBytes("UTF-8"));
				fos.close();
				typeList.clear();
				nameList.clear();
				sb = new StringBuffer();
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 填充Get,Set方法
	 */
	private static void getSet(List<String> typeList, List<String> nameList, StringBuffer sb) {

		for (int i = 0; i < typeList.size(); i++) {

			// 属性类型和名称
			String type = typeList.get(i);
			String name = nameList.get(i);

			// GET,SET方法首字母大写
			String c = String.valueOf(name.charAt(0));
			c = c.toUpperCase();
			String n = c + name.substring(1);

			// 拼接输出语句
			sb.append("\n\tpublic ");
			sb.append(type);
			sb.append(" get");
			sb.append(n);
			sb.append("() {");
			sb.append("\n\t\treturn ");
			sb.append(name);
			sb.append(";\n\t}\n");
			sb.append("\n\tpublic void set");
			sb.append(n);
			sb.append("(");
			sb.append(type);
			sb.append(" ");
			sb.append(name);
			sb.append(") {");
			sb.append("\n\t\tthis.");
			sb.append(name);
			sb.append(" = ");
			sb.append(name);
			sb.append(";\n\t}\n");
		}

		sb.append("\n}");
	}

	public static void main(String[] args) throws InterruptedException {

		String app = System.getProperty("user.dir");
		String modelPath = app + "*";
		ExcelToObject.convert(new File(“*”), modelPath, "*");
		ObjectToMapping.convert(new File(modelPath), app + "*", "timeKey", true);
	}
}

Java 根据固定格式的Excel生成实体类