forked from enilu/database-doc-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
239 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/cn/enilu/tool/database/doc/generator/doc/Html2DocConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cn.enilu.tool.database.doc.generator.doc; | ||
|
||
import org.apache.poi.poifs.filesystem.DirectoryEntry; | ||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; | ||
|
||
import java.io.*; | ||
|
||
/** | ||
* Html2DocConverter | ||
* | ||
* @author zt | ||
* @version 2019/1/12 0012 | ||
*/ | ||
public class Html2DocConverter { | ||
|
||
private String inputPath; // 输入文件路径,以.html结尾 | ||
private String outputPath; // 输出文件路径,以.doc结尾 | ||
|
||
public Html2DocConverter(String inputPath, String outputPath) { | ||
super(); | ||
this.inputPath = inputPath; | ||
this.outputPath = outputPath; | ||
} | ||
|
||
/** | ||
* 读取html文件到word | ||
* | ||
* @param filepath | ||
* html文件的路径 | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public boolean writeWordFile() throws Exception { | ||
|
||
InputStream is = null; | ||
FileOutputStream fos = null; | ||
|
||
// 1 找不到源文件, 则返回false | ||
File inputFile = new File(this.inputPath); | ||
if (!inputFile.exists()) { | ||
return false; | ||
} | ||
|
||
File outputFile = new File(this.outputPath); | ||
// 2 如果目标路径不存在 则新建该路径 | ||
if (!outputFile.getParentFile().exists()) { | ||
outputFile.getParentFile().mkdirs(); | ||
} | ||
|
||
try { | ||
|
||
// 3 将html文件内容写入doc文件 | ||
is = new FileInputStream(inputFile); | ||
POIFSFileSystem poifs = new POIFSFileSystem(); | ||
DirectoryEntry directory = poifs.getRoot(); | ||
directory.createDocument( | ||
"WordDocument", is); | ||
|
||
fos = new FileOutputStream(this.outputPath); | ||
poifs.writeFilesystem(fos); | ||
|
||
System.out.println("转换word文件完成!"); | ||
|
||
return true; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (fos != null) { | ||
fos.close(); | ||
} | ||
if (is != null) { | ||
is.close(); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
public static void main(String[] args) throws Exception { | ||
|
||
new Html2DocConverter("G:/123.html" , "G:/temp5.doc").writeWordFile(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/cn/enilu/tool/database/doc/generator/doc/WordGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cn.enilu.tool.database.doc.generator.doc; | ||
|
||
import cn.enilu.tool.database.doc.generator.bean.ColumnVo; | ||
import cn.enilu.tool.database.doc.generator.bean.TableVo; | ||
import freemarker.template.Configuration; | ||
import freemarker.template.Template; | ||
|
||
import java.io.*; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* WordGenerator | ||
* | ||
* @author zt | ||
* @version 2019/1/12 0012 | ||
*/ | ||
public class WordGenerator { | ||
private static Configuration configuration = null; | ||
|
||
static { | ||
|
||
configuration = new Configuration(); | ||
configuration.setDefaultEncoding("utf-8"); | ||
try { | ||
configuration.setDirectoryForTemplateLoading(new File("./")); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private WordGenerator() { | ||
throw new AssertionError(); | ||
} | ||
|
||
public static void createDoc(String dbName, List<TableVo> list) { | ||
Map map = new HashMap(); | ||
map.put("dbName", dbName); | ||
map.put("tables", list); | ||
try { | ||
Template template = configuration.getTemplate("database.html"); | ||
String name = dbName + "-doc" + File.separator + dbName + ".html"; | ||
File f = new File(name); | ||
Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8"); | ||
template.process(map, w); | ||
w.close(); | ||
new Html2DocConverter(dbName + "-doc" + File.separator + dbName + ".html", dbName + "-doc" + File | ||
.separator + dbName + ".doc") | ||
.writeWordFile(); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
public static void main(String[] args) throws Exception { | ||
Map<String, Object> map = new HashMap<String, Object>(); | ||
List<TableVo> list = new ArrayList<>(); | ||
for (int i = 0; i < 5; i++) { | ||
TableVo tableVo = new TableVo(); | ||
tableVo.setTable("表" + i); | ||
tableVo.setComment("注释" + i); | ||
List<ColumnVo> columns = new ArrayList<>(); | ||
for (int j = 0; j < 5; j++) { | ||
ColumnVo columnVo = new ColumnVo(); | ||
columnVo.setName("name" + j); | ||
columnVo.setComment("注释" + j); | ||
columnVo.setKey("PRI"); | ||
columnVo.setIsNullable("是"); | ||
columnVo.setType("varchar(2"); | ||
columns.add(columnVo); | ||
|
||
} | ||
tableVo.setColumns(columns); | ||
list.add(tableVo); | ||
} | ||
|
||
createDoc("test", list); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<style> | ||
|
||
</style> | ||
</head> | ||
|
||
<body> | ||
<h1 style="text-align: center;">${dbName}数据库设计文档</h1> | ||
<h2>表汇总</h2> | ||
<table width="100%"> | ||
|
||
<thead style="background-color: #b9c9fe;font-weight: bold;"> | ||
<tr> | ||
<td> 名称</td> | ||
<td>备注</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<#list tables as item> | ||
<tr style="background-color: #e8edff;"> | ||
<td>${item.table}</td> | ||
<td>${item.comment}</td> | ||
</tr> | ||
</#list> | ||
</tbody> | ||
</table> | ||
<h2>表明细</h2> | ||
<#list tables as item> | ||
<br> | ||
<h3>${item.comment}(${item.table})</h3> | ||
<table width="100%"> | ||
<thead style="background-color: #b9c9fe;font-weight: bold;"> | ||
<tr> | ||
<td>列名</td> | ||
<td>类型</td> | ||
<td>KEY</td> | ||
<td>可否为空</td> | ||
<td>注释</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<#list item.columns as column> | ||
<tr style="background-color: #e8edff;"> | ||
<td>${column.name}</td> | ||
<td>${column.type}</td> | ||
<td>${column.key}</td> | ||
<td>${column.isNullable}</td> | ||
<td>${column.comment}</td> | ||
</tr> | ||
</#list> | ||
</tbody> | ||
</table> | ||
</#list> | ||
|
||
</body> | ||
</html> |