利用EXCEL生成模型

/**
     * 
     * @param fileName 文件名
     * @param outputPath 生成路径
     * @param modelEnIndex 模型名字
     * @param modelZhIndex 模型中文注释
     * @param fieldCommentIndex 字段补充注释
     * @param fieldTypeIndex 字段类型
     * @param fieldIndex 字段名字
     * @param fieldZhIndex 字段中文注释
     */
    public static void generateFile(String fileName,
                                    String outputPath,
                                    int modelEnIndex,
                                    int modelZhIndex,
                                    int fieldCommentIndex,
                                    int fieldTypeIndex,
                                    int fieldIndex,
                                    int fieldZhIndex)
    {
        List<List<String>> retLst = ExcelUtils.readExcelData(fileName, 0, 1, 0);
        Map<String, List<String>> fieldMap = new HashMap<String, List<String>>();
        List<String> temp = null;
        for (List<String> row : retLst)
        {
            String modelZh = row.get(modelZhIndex);
            String modelEn = row.get(modelEnIndex);
            String key = modelEn + "_" + modelZh;
            String fieldComment = String.format("/** %s %s */%s",
                                                row.get(fieldZhIndex),
                                                row.get(fieldCommentIndex),
                                                System.getProperty("line.separator"));
            String fieldCreation = String.format("private %s %s; %s",
                                                 row.get(fieldTypeIndex),
                                                 row.get(fieldIndex),
                                                 System.getProperty("line.separator"));
            if (!(modelZh == null || modelZh.equals("") || modelEn == null || modelEn.equals("")))
            {
                temp = new ArrayList<String>();
                fieldMap.put(key, temp);
            }
            temp.add(fieldComment);
            temp.add(fieldCreation);
        }
        // 生成代码
        File outputFile = new File(outputPath);
        if (!outputFile.exists())
        {
            outputFile.mkdir();
        }
        Writer writer = null;
        try
        {
            for (Entry<String, List<String>> entry : fieldMap.entrySet())
            {
                String key = entry.getKey();
                List<String> value = entry.getValue();
                String modelName = key.split("_")[0];
                String modelComment = key.split("_")[1];
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile
                                                                                        + File.separator
                                                                                        + modelName
                                                                                        + ".java")));
                // 通过outputPath截取包名
                writer.write(String.format("/** %s */%s public class %s{%s",
                                           modelComment,
                                           System.getProperty("line.separator"),
                                           modelName,
                                           System.getProperty("line.separator")));
                for (int i = 0; i < value.size(); i++)
                {
                    writer.write(value.get(i) + System.getProperty("line.separator"));
                    if (i == value.size() - 1)
                    {
                        writer.write("}" + System.getProperty("line.separator"));
                    }
                }
                writer.flush();
            }
            System.out.println("done");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (writer != null)
            {
                try
                {
                    writer.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值