建设网站赚钱么,在线生成短链接,创建iis网站,设计网站推荐素材网站文章目录 1.场景说明2.解决办法3.生成文档3.1.实现思路3.2.引入Apache POI依赖3.3.获取表及表字段说明Mapper3.4.POI创建文档表格#xff0c;并填充数据3.5.完整的接口下载代码3.6.效果展示 1.场景说明
在项目中表已经建立好了#xff0c;但是现在想对外提供一个表的字段的描… 文章目录 1.场景说明2.解决办法3.生成文档3.1.实现思路3.2.引入Apache POI依赖3.3.获取表及表字段说明Mapper3.4.POI创建文档表格并填充数据3.5.完整的接口下载代码3.6.效果展示 1.场景说明
在项目中表已经建立好了但是现在想对外提供一个表的字段的描述说明该怎么办。例如开发前期赶进度也没个什么数据库的需求设计文档之类的项目开发后期现在要补充文档了表要是少那就挨个复制粘贴了多的话复制粘贴也麻烦。
例如这是某个表
希望最后在word文档中提供这样的表结构说明
2.解决办法
1.根据建表的语句挨个的复制呗还能怎么办适用于表量比较少然后技术比较菜的人如果数据库的表有个七八十上百张相信我真的有人挨个的将SQL客户端工具打开然后挨个的复制到文档里面去。然后时候说自己工作很多加班很晚才弄完。2.有点学习技术需求不至于太菜的虽然我没去找过有没有但是我相信这种网上肯定有一堆人已经实现将数据库表结构转换成word文档的代码或者示例所以我觉得只要不是太菜一下午也能解决了。3.直接让GPT转换了导出建表语句给GPT一个示例然后GPT来做例如如下导出一个建表语句然后再最上面给出一个示例然后接下来就交给GPT了
4.接下来才是本文的重头戏带着学习的成分我们去造轮子当然了不是造GPT咱没有那个本事咱们自己写一个组件来实现根据数据库逆向生成表结构的文档说明造轮子根据数据库表生成文档
3.生成文档
3.1.实现思路
1.获取到所有的表根据你使用的数据库查询表例如我用mysql因此用下面的语句查看表名
SHOW TABLES;2.获取表的字段描述说明
SHOW FULL FIELDS FROM 表名3.利用步骤1和步骤2的表名、表结构信息生成多个word表格 java中生成word的技术有很多 Apache POIApache POI是一个开源的Java库用于读取和写入Microsoft Office格式的文件包括Word文档.doc和.docx。它提供了丰富的API和类可以操作和处理Word文档的内容、格式、样式和元数据等。 Docx4jDocx4j是一个用于创建和操作.docx文件的Java库。它提供了许多API和类可以通过编程方式创建、修改和处理Word文档。Docx4j还支持生成PDF、HTML和其他格式的文档。 Apache POI XWPFApache POI XWPF是Apache POI库的扩展专门用于处理.docx格式的Word文档。它提供了更高级的API可以读取、写入和修改.docx文件并支持处理文本、样式、段落、表格、图像和其他文档元素。 JWordJWord是一个商业库用于创建和操作Word文档。它提供了丰富的API和功能可以生成复杂的Word文档包括文本、样式、表格、图像、图表和其他元素。 Aspose.Words for JavaAspose.Words是一个商业库用于在Java应用程序中处理Word文档。它提供了强大的API和功能可以创建、修改、转换和打印Word文档并支持许多高级特性如合并文档、插入水印、执行邮件合并等
本文中我们采用Apache POI来实现
3.2.引入Apache POI依赖
dependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion4.1.2/version
/dependency3.3.获取表及表字段说明Mapper
定义实体类TableStruct接收我们需要的字段例如这里我们需要field、type、comment上字段
Data
public class TableStruct {private String field;private String type;private String comment;
}编写我们的SQL这里我使用Mybatis当然其他的SQL框架也行
public interface TableMapper {Select(SHOW TABLES)ListString getAllTables();Select(SHOW FULL FIELDS FROM ${tableName})ListTableStruct getTableInfo(Param(tableName) String tableName);}3.4.POI创建文档表格并填充数据
//设置标题
private static void addCustomHeadingStyle(XWPFStyles styles, String styleId, String styleName, int headingLevel) {CTStyle ctStyle CTStyle.Factory.newInstance();ctStyle.setStyleId(styleId);CTString styleNameString CTString.Factory.newInstance();styleNameString.setVal(styleName);ctStyle.setName(styleNameString);CTDecimalNumber indentNumber CTDecimalNumber.Factory.newInstance();indentNumber.setVal(BigInteger.valueOf(headingLevel));CTPPr ppr CTPPr.Factory.newInstance();ppr.setOutlineLvl(indentNumber);ctStyle.setPPr(ppr);XWPFStyle style new XWPFStyle(ctStyle);styles.addStyle(style);
}// 创建文档
XWPFDocument document new XWPFDocument();//设置标题
XWPFStyles styles document.createStyles();
String heading1StyleId heading1;
addCustomHeadingStyle(styles, heading1StyleId, 标题 1, 1);tableMapper.getAllTables().forEach(temp -{//生成标题XWPFParagraph title1Paragraph document.createParagraph();title1Paragraph.setStyle(heading1StyleId);XWPFRun title1Run title1Paragraph.createRun();title1Run.setText(temp);//生成表头ListTableStruct tableInfo tableMapper.getTableInfo(temp);XWPFTable table document.createTable(tableInfo.size() 1, 4);table.setWidth(100%);table.getRow(0).getCell(0).setText(序号);table.getRow(0).getCell(1).setText(字段名称);table.getRow(0).getCell(2).setText(字段类型);table.getRow(0).getCell(3).setText(字段描述);//生成表内容 第0行已经设置为表头因此我们需要从第一行开始for (int row 0; row tableInfo.size(); row) {table.getRow(row 1 ).getCell(0).setText(Integer.toString(row));table.getRow(row 1 ).getCell(1).setText(tableInfo.get(row).getField());table.getRow(row 1 ).getCell(2).setText(tableInfo.get(row).getType());table.getRow(row 1 ).getCell(3).setText(tableInfo.get(row).getComment());}
});3.5.完整的接口下载代码 GetMapping(/info/download)public void downSeg(HttpServletResponse response){try {response.setContentType(application/vnd.openxmlformats-officedocument.spreadsheetml.sheet);response.setCharacterEncoding(utf-8);String fileName URLEncoder.encode(表结构注释说明LocalDate.now().toString(), UTF-8).replaceAll(\\, %20);response.setHeader(Content-disposition, attachment;filename*utf-8 fileName .docx);// 创建文档XWPFDocument document new XWPFDocument();//设置标题XWPFStyles styles document.createStyles();String heading1StyleId heading1;addCustomHeadingStyle(styles, heading1StyleId, 标题 1, 1);tableMapper.getAllTables().forEach(temp -{//生成标题XWPFParagraph title1Paragraph document.createParagraph();title1Paragraph.setStyle(heading1StyleId);XWPFRun title1Run title1Paragraph.createRun();title1Run.setText(temp);//生成表头ListTableStruct tableInfo tableMapper.getTableInfo(temp);XWPFTable table document.createTable(tableInfo.size() 1, 4);table.setWidth(100%);table.getRow(0).getCell(0).setText(序号);table.getRow(0).getCell(1).setText(字段名称);table.getRow(0).getCell(2).setText(字段类型);table.getRow(0).getCell(3).setText(字段描述);//生成表内容 第0行已经设置为表头因此我们需要从第一行开始for (int row 0; row tableInfo.size(); row) {table.getRow(row 1 ).getCell(0).setText(Integer.toString(row));table.getRow(row 1 ).getCell(1).setText(tableInfo.get(row).getField());table.getRow(row 1 ).getCell(2).setText(tableInfo.get(row).getType());table.getRow(row 1 ).getCell(3).setText(tableInfo.get(row).getComment());}});ByteArrayOutputStream byteArrayOutputStream new ByteArrayOutputStream();document.write(byteArrayOutputStream);ServletOutputStream outputStream response.getOutputStream();outputStream.write(byteArrayOutputStream.toByteArray());outputStream.flush();outputStream.close();document.close();} catch (Exception e) {e.printStackTrace();}}private static void addCustomHeadingStyle(XWPFStyles styles, String styleId, String styleName, int headingLevel) {CTStyle ctStyle CTStyle.Factory.newInstance();ctStyle.setStyleId(styleId);CTString styleNameString CTString.Factory.newInstance();styleNameString.setVal(styleName);ctStyle.setName(styleNameString);CTDecimalNumber indentNumber CTDecimalNumber.Factory.newInstance();indentNumber.setVal(BigInteger.valueOf(headingLevel));CTPPr ppr CTPPr.Factory.newInstance();ppr.setOutlineLvl(indentNumber);ctStyle.setPPr(ppr);XWPFStyle style new XWPFStyle(ctStyle);styles.addStyle(style);}3.6.效果展示
浏览器访问该接口即可下载 效果如下所示 OK收拾东西准备下班2023年11月28日16:57:24楼主单位4点50下班从产生这个想法到我这个想法实现以及到我生成这个文档以后将这50多张表的结构说明补充到详细设计文档里面总共花了大概2个小时。今天下午又是收货满满的一天。