Skip to content

Commit

Permalink
issue #1求个生成doc文件的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
enilu committed Jan 12, 2019
1 parent 3fd18c2 commit fb251c4
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 8 deletions.
14 changes: 6 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.3.1</version>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>


</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.enilu.tool.database.doc.generator.bean.ColumnVo;
import cn.enilu.tool.database.doc.generator.bean.TableVo;
import cn.enilu.tool.database.doc.generator.doc.WordGenerator;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
Expand Down Expand Up @@ -49,6 +50,9 @@ public void generateDoc() {
}
List<TableVo> list = getTableData();
save2File(list);
//保存word
WordGenerator.createDoc(dbName,list);

}

public void save2File(List<TableVo> tables) {
Expand Down
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();
}
}
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);

}

}
61 changes: 61 additions & 0 deletions src/main/resources/database.html
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>

0 comments on commit fb251c4

Please sign in to comment.