抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

导出数据库表信息生成Word

1.导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- ================== 将数据库表信息生成word文档信息所需 ====================== -->
<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lowagie/itext-rtf -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>

2.实体映射

TableFileds.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import lombok.Data;

/**
* <p> 数据库表字段信息 </p>
*/
@Data
public class TableFileds {

/**
* 字段名
*/
private String field;
/**
* 类型
*/
private String type;
/**
* 是否为空
*/
private String Null;
/**
* 主键
*/
private String key;
/**
* 字段说明
*/
private String comment;
/**
* 默认值
*/
private String Default;

}

Tables.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import lombok.Data;

/**
* <p> 数据库表信息 </p>
*/
@Data
public class Tables {

/**
* 表名
*/
private String name;
/**
* 表注释
*/
private String comment;

}

3.查询表数据信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Mapper
public interface TableMapper {

/**
* 获取指定数据库下所有表名和注释
*
* @param dbName:数据库名
* @return: java.util.List<com.zhengqing.demo.modules.system.entity.Tables>
*/
@Select("select table_name as name,table_comment as comment from information_schema.tables where table_schema =#{dbName} order by table_name")
List<Tables> getAllTables(@Param("dbName") String dbName);

/**
* 获取指定表信息
*
* @param tableName:表
* @return: java.util.List<com.zhengqing.demo.modules.system.entity.TableFileds>
*/
@Select("SHOW FULL FIELDS FROM ${tableName}")
List<TableFileds> getTable(@Param("tableName") String tableName);

}

4.生成word文档工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
@Service
public class TableToWordUtil {

@Autowired
TableMapper tableMapper;

/**
* 生成word文档
*
* @param tables:该数据库下所有表信息
* @param fileName:生成文件地址
* @param title:文件内容标题
* @return: void
*/
public void toWord(List<Tables> tables, String fileName, String title) {
Document document = new Document(PageSize.A4);
try {
// 创建文件夹
File dir = new File(Constants.FILE_PATH);
dir.mkdirs();

// 创建文件
File file = new File(fileName);
if (file.exists() && file.isFile()) {
file.delete();
}
file.createNewFile();

// 写入文件信息
RtfWriter2.getInstance(document, new FileOutputStream(fileName));
document.open();
Paragraph ph = new Paragraph();
Font f = new Font();
Paragraph p = new Paragraph(title, new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
p.setAlignment(1);
document.add(p);
ph.setFont(f);
for (int i = 0; i < tables.size(); i++) {
String table_name = tables.get(i).getName();
String table_comment = tables.get(i).getComment();
List<TableFileds> fileds = tableMapper.getTable(tables.get(i).getName());
String all = "" + (i + 1) + " 表名称:" + table_name + "(" + table_comment + ")";
Table table = new Table(6);

document.add(new Paragraph(""));

table.setBorderWidth(1);
table.setPadding(0);
table.setSpacing(0);

//添加表头的元素,并设置表头背景的颜色
Color chade = new Color(176, 196, 222);

Cell cell = new Cell("编号");
addCell(table, cell, chade);
cell = new Cell("字段名");
addCell(table, cell, chade);
cell = new Cell("类型");
addCell(table, cell, chade);
cell = new Cell("是否非空");
addCell(table, cell, chade);
cell = new Cell("是否主键");
addCell(table, cell, chade);
cell = new Cell("注释");
addCell(table, cell, chade);

table.endHeaders();

// 表格的主体
for (int k = 0; k < fileds.size(); k++) {
addContent(table, cell, (k + 1) + "");
addContent(table, cell, fileds.get(k).getField());
addContent(table, cell, fileds.get(k).getType());
addContent(table, cell, fileds.get(k).getNull().equals("YES") ? "否" : "是");
addContent(table, cell, fileds.get(k).getKey() != "" ? "是" : "否");
addContent(table, cell, fileds.get(k).getComment());
}
Paragraph pheae = new Paragraph(all);
//写入表说明
document.add(pheae);
//生成表格
document.add(table);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 添加表头到表格
*
* @param table
* @param cell
* @param chade
*/
private void addCell(Table table, Cell cell, Color chade) {
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(chade);
table.addCell(cell);
}

/**
* 添加内容到表格
*
* @param table
* @param content
*/
private void addContent(Table table, Cell cell, String content) {
cell = new Cell(content);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
}

}

常量参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Constants {

/**
* 需要生成word文档的数据库
*/
public static final String DATABASE = "demo";
/**
* 生成文件名前缀
*/
public static final String FILE_NAME = "测试数据库";

/**
* 生成文件地址
*/
public static String FILE_PATH = "D:\\www";

}

5.生成word文档实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Service
public class TableService {

@Autowired
private TableMapper tableMapper;
@Autowired
private TableToWordUtil tableToWordUtil;

public String getTableInfo() {
// 1、获取数据库所有表信息
List<Tables> tables = tableMapper.getAllTables(Constants.DATABASE);

// 2、生成文件名信息 - 年月日时分秒
String date = null;
try {
date = DateTimeUtils.dateFormat(new Date(), DateTimeUtils.PARSE_PATTERNS[12]);
} catch (ParseException e) {
e.printStackTrace();
}
String docFileName = Constants.FILE_PATH + "\\" + Constants.FILE_NAME + "-" + date + ".doc";

// 3、调用工具类生成文件
tableToWordUtil.toWord(tables, docFileName, Constants.FILE_NAME);

// 4、返回文件地址
String filePath = docFileName.replaceAll("\\\\", "/");
return filePath;
}
}

6.控制层实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import com.zhengqing.demo.modules.common.api.BaseController;
import com.zhengqing.demo.modules.common.dto.output.ApiResult;
import com.zhengqing.demo.modules.system.service.ITableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "系统管理-数据库表管理")
public class TableController extends BaseController {

@Autowired
ITableService tableService;

@GetMapping(value = "/tableToWord", produces = "application/json;charset=utf-8", name = "导出数据库表信息生成Word")
@ApiOperation(value = "导出数据库表信息生成Word", httpMethod = "GET", response = ApiResult.class, notes = "导出数据库表信息生成Word")
public ApiResult tableToWord() {
try {
return ApiResult.ok("导出数据库表信息生成Word成功", tableService.getTableInfo());
} catch (Exception e) {
log.error("导出数据库表信息生成Word失败", e);
return ApiResult.fail(e.getMessage());
}
}
}

评论