首页 > Java开发 > Java根据固定格式的Excel生成实体类

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

  1. /** 
  2.  * Excel文档转对象 
  3.  *  
  4.  * @author dxm 
  5.  * 
  6.  */  
  7. public class ExcelToObject {  
  8.   
  9.     /** 
  10.      * 转换 
  11.      *  
  12.      * @param file 
  13.      */  
  14.     public static void convert(File file, String savePath, String pack) {  
  15.   
  16.         // Excel是否存在  
  17.         if (null == file || !file.exists()) {  
  18.             return;  
  19.         }  
  20.   
  21.         // 创建存放路径目录  
  22.         File save = new File(savePath);  
  23.         if (!save.exists()) {  
  24.             save.mkdirs();  
  25.         }  
  26.   
  27.         try {  
  28.   
  29.             // Excel文件名  
  30.             String fileName = file.getName();  
  31.   
  32.             // 建立输入流  
  33.             InputStream input = new FileInputStream(file);  
  34.             Workbook wb = null;  
  35.   
  36.             // 根据文件格式(2003或者2007)来初始化  
  37.             if (fileName.endsWith("xlsx")) {  
  38.                 wb = new XSSFWorkbook(input);  
  39.             } else {  
  40.                 wb = (Workbook) new HSSFWorkbook(input);  
  41.             }  
  42.   
  43.             // 获得第一个表单  
  44.             Sheet sheet = wb.getSheetAt(0);  
  45.   
  46.             // 文件输出流  
  47.             FileOutputStream fos = null;  
  48.   
  49.             // 获得第一个表单的迭代器  
  50.             Iterator<Row> rows = sheet.rowIterator();  
  51.   
  52.             // 插入语句  
  53.             List<String> typeList = new ArrayList<String>();  
  54.             List<String> nameList = new ArrayList<String>();  
  55.             StringBuffer sb = new StringBuffer();  
  56.   
  57.             // 包名  
  58.             pack = "package " + pack + ";\n\n";  
  59.   
  60.             // 获得第一个表单的迭代器  
  61.             while (rows.hasNext()) {  
  62.   
  63.                 // 获得行数据  
  64.                 Row row = rows.next();  
  65.   
  66.                 // 列0,1,2表示属性名,类型,说明  
  67.                 Cell cell0 = row.getCell(0);  
  68.                 Cell cell1 = row.getCell(1);  
  69.                 Cell cell2 = row.getCell(2);  
  70.                 String name = cell0.getStringCellValue().trim();  
  71.                 String type = cell1.getStringCellValue().trim();  
  72.                 String mark = cell2.getStringCellValue().trim();  
  73.   
  74.                 // 过滤空数据  
  75.                 if (name.isEmpty()) {  
  76.                     continue;  
  77.                 }  
  78.   
  79.                 // 过滤说明  
  80.                 if (name.equals("字段")) {  
  81.                     continue;  
  82.                 }  
  83.   
  84.                 // 新表开始  
  85.                 int index = type.indexOf("t_");  
  86.                 if (-1 != index) {  
  87.   
  88.                     // 写入上个类文件  
  89.                     if (null != fos) {  
  90.                         getSet(typeList, nameList, sb);  
  91.                         fos.write(sb.toString().getBytes("UTF-8"));  
  92.                         fos.close();  
  93.                         typeList.clear();  
  94.                         nameList.clear();  
  95.                         sb = new StringBuffer();  
  96.                     }  
  97.   
  98.                     // 类首字母大写  
  99.                     type = type.substring(index + 2);  
  100.                     String c = String.valueOf(type.charAt(0));  
  101.                     c = c.toUpperCase();  
  102.                     type = c + type.substring(1);  
  103.   
  104.                     // 删除旧的类文件  
  105.                     File f = new File(savePath + "/" + type + ".java");  
  106.                     if (f.exists()) {  
  107.                         f.delete();  
  108.                     }  
  109.   
  110.                     // 创建文件输出流  
  111.                     fos = new FileOutputStream(f);  
  112.   
  113.                     // 拼接输出语句  
  114.                     sb.append(pack);  
  115.                     sb.append("/**\n * ");  
  116.                     sb.append(name);  
  117.                     sb.append("\n * \n * @author juling\n *\n */\n");  
  118.                     sb.append("public class ");  
  119.                     sb.append(type);  
  120.   
  121.                     // 拼接继承类  
  122.                     if (!mark.isEmpty()) {  
  123.                         sb.append(" extends ");  
  124.                         sb.append(mark);  
  125.                         sb.append(" {\n");  
  126.                         sb.append("\tprivate static final long serialVersionUID = -1L;\n");  
  127.                         if (mark.equals("VData")) {  
  128.                             sb.insert(pack.length(), "import com.mohe.common.db.VData;\n\n");  
  129.                         }  
  130.                     } else {  
  131.                         sb.append(" {\n");  
  132.                     }  
  133.   
  134.                     continue;  
  135.                 }  
  136.   
  137.                 // 拼接输出语句  
  138.                 sb.append("\n");  
  139.                 sb.append("\t// ");  
  140.                 sb.append(mark);  
  141.                 sb.append("\n");  
  142.                 sb.append("\tpublic ");  
  143.                 sb.append(type);  
  144.                 sb.append(" ");  
  145.                 sb.append(name);  
  146.                 sb.append(";\n");  
  147.   
  148.                 // 如果为时间类型插入导入java.util.Date包  
  149.                 if (type.equals("Date")) {  
  150.                     sb.insert(pack.length(), "import java.util.Date;\n\n");  
  151.                 }  
  152.   
  153.                 typeList.add(type);  
  154.                 nameList.add(name);  
  155.             }  
  156.   
  157.             // 写入最后一个类文件  
  158.             if (null != fos) {  
  159.                 getSet(typeList, nameList, sb);  
  160.                 fos.write(sb.toString().getBytes("UTF-8"));  
  161.                 fos.close();  
  162.                 typeList.clear();  
  163.                 nameList.clear();  
  164.                 sb = new StringBuffer();  
  165.             }  
  166.   
  167.         } catch (IOException ex) {  
  168.             ex.printStackTrace();  
  169.         }  
  170.     }  
  171.   
  172.     /** 
  173.      * 填充Get,Set方法 
  174.      */  
  175.     private static void getSet(List<String> typeList, List<String> nameList, StringBuffer sb) {  
  176.   
  177.         for (int i = 0; i < typeList.size(); i++) {  
  178.   
  179.             // 属性类型和名称  
  180.             String type = typeList.get(i);  
  181.             String name = nameList.get(i);  
  182.   
  183.             // GET,SET方法首字母大写  
  184.             String c = String.valueOf(name.charAt(0));  
  185.             c = c.toUpperCase();  
  186.             String n = c + name.substring(1);  
  187.   
  188.             // 拼接输出语句  
  189.             sb.append("\n\tpublic ");  
  190.             sb.append(type);  
  191.             sb.append(" get");  
  192.             sb.append(n);  
  193.             sb.append("() {");  
  194.             sb.append("\n\t\treturn ");  
  195.             sb.append(name);  
  196.             sb.append(";\n\t}\n");  
  197.             sb.append("\n\tpublic void set");  
  198.             sb.append(n);  
  199.             sb.append("(");  
  200.             sb.append(type);  
  201.             sb.append(" ");  
  202.             sb.append(name);  
  203.             sb.append(") {");  
  204.             sb.append("\n\t\tthis.");  
  205.             sb.append(name);  
  206.             sb.append(" = ");  
  207.             sb.append(name);  
  208.             sb.append(";\n\t}\n");  
  209.         }  
  210.   
  211.         sb.append("\n}");  
  212.     }  
  213.   
  214.     public static void main(String[] args) throws InterruptedException {  
  215.   
  216.         String app = System.getProperty("user.dir");  
  217.         String modelPath = app + "*";  
  218.         ExcelToObject.convert(new File(“*”), modelPath, "*");  
  219.         ObjectToMapping.convert(new File(modelPath), app + "*""timeKey"true);  
  220.     }  
  221. }  

本文固定链接: http://www.devba.com/index.php/archives/6117.html | 开发吧

报歉!评论已关闭.