/**
*
* @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();
}
}
}
}