网站首页 > java教程 正文
一、前言
最近看见朋友写了一个导出数据库生成word文档的业务,感觉很有意思,研究了一下,这里也拿出来与大家分享一波~
先来看看生成的word文档效果吧
下面我们也来一起简单的实现吧
二、Java 导出数据库表信息生成Word文档
温馨小提示:下面只是简单的展示一些主要代码,详情可参考文末给出的案例demo源码
基本环境
- spring-boot 2.1.8
- mybatis-plus 2.2.0
- mysql 数据库
1、新增依赖
<!-- ================== 将数据库表信息生成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、查询表数据信息
@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);
}
3、生成word文档实现类
@Service
public class TableService implements ITableService {
@Autowired
private TableMapper tableMapper;
@Autowired
private TableToWordUtil tableToWordUtil;
@Override
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;
}
}
4、其中生成word文档工具类
@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);
}
}
5、其中的一些常量参数
public class Constants {
/**
* 需要生成word文档的数据库
*/
public static final String DATABASE = "demo";
/**
* 生成文件名前缀
*/
public static final String FILE_NAME = "测试数据库";
/**
* 生成文件地址
*/
public static String FILE_PATH = "D:\\www";
}
三、测试生成效果
小编在demo中提供了一个get请求的接口http://localhost:8080/api/tableToWord
接下来我们就可以去返回的地址中查看生成文件了
案例demo源码
https://gitee.com/zhengqingya/java-workspace
猜你喜欢
- 2024-10-19 PDF转word工具分享!这5个转换工具,超级好用!
- 2024-10-19 电子书格式转换器|支持ePub、Azw3、Mobi、Doc、PDF、TXT文件
- 2024-10-19 扫呀 js 控制扫描仪,支持国产系统、ocr,pdf 导出的免费工具来了
- 2024-10-19 java如何生成动态Word文件(java动态生成图片)
- 2024-10-19 Java 将word文档转换为PNG、XPS、RTF等格式
- 2024-10-19 word转pdf软件有哪些?这几个方法教你轻松转换
- 2024-10-19 Python Web开发中Excel转PDF文件(pythonword转excel)
- 2024-10-19 Java 转换PDF为图片时设置图片的背景透明度
- 2024-10-19 Stimulsoft v2019.2.3发布,HTML文本导出为PDF格式不再是难题
- 2024-10-19 Spire.Doc for Java 10.6.0 增强了 Word 到 PDF 和 HTML 的转换
你 发表评论:
欢迎- 最近发表
-
- Java常量定义防暴指南:从"杀马特"到"高富帅"的华丽转身
- Java接口设计原则与实践:优雅编程的艺术
- java 包管理、访问修饰符、static/final关键字
- Java工程师的代码规范与最佳实践:优雅代码的艺术
- 编写一个java程序(编写一个Java程序计算并输出1到n的阶乘)
- Mycat的搭建以及配置与启动(mycat部署)
- Weblogic 安装 -“不是有效的 JDK Java 主目录”解决办法
- SpringBoot打包部署解析:jar包的生成和结构
- 《Servlet》第05节:创建第一个Servlet程序(HelloSevlet)
- 你认为最简单的单例模式,东西还挺多
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)