升鲜宝生鲜配送供应链管理系统Mysql表结构数据字典的生成小工具V0.01

news/2025/2/12 11:47:21/文章来源:https://www.cnblogs.com/yousee/p/18579978

最近最近要交付升鲜宝生鲜配送供应链管理系统源代码给上海的客户,需要将蓝湖UI设计图及数据字典交接给别人。在网上找了半天没有找到合适的根据Mysql生成Word数据字典,自己就写了几行代码,记录一下.后面可能会继续改造。主要的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Body = DocumentFormat.OpenXml.Wordprocessing.Body;
using Text = DocumentFormat.OpenXml.Wordprocessing.Text;
using DocumentFormat.OpenXml;namespace DotNet.WinForm
{public partial class FrmCreateDBHtml : Form{public FrmCreateDBHtml(){InitializeComponent();}private void btnSave_Click(object sender, EventArgs e){string connectionString = $"server={txtServer.Text};user={txtUser.Text};password={txtPassword.Text};database={txtDatabase.Text};";string wordFilePath = "升鲜宝数据字典.docx";try{GenerateDataDictionary(connectionString, wordFilePath);MessageBox.Show("Data dictionary has been generated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);}}private void BtnSave_Click(object sender, EventArgs e){string connectionString = $"server={txtServer.Text};user={txtUser.Text};password={txtPassword.Text};database={txtDatabase.Text};";string wordFilePath = "DataDictionary.docx";try{GenerateDataDictionary(connectionString, wordFilePath);MessageBox.Show("Data dictionary has been generated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);}}static void GenerateDataDictionary(string connectionString, string wordFilePath){using (MySqlConnection connection = new MySqlConnection(connectionString)){connection.Open();DataTable tables = connection.GetSchema("Tables");using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(wordFilePath, DocumentFormat.OpenXml.WordprocessingDocumentType.Document)){MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();mainPart.Document = new Document();Body body = new Body();foreach (DataRow row in tables.Rows){string tableName = row["TABLE_NAME"].ToString();string tableComment = "";// Get table commentusing (MySqlConnection commentConnection = new MySqlConnection(connectionString)){commentConnection.Open();string commentQuery = $"SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{connection.Database}' AND TABLE_NAME = '{tableName}';";MySqlCommand commentCommand = new MySqlCommand(commentQuery, commentConnection);tableComment = commentCommand.ExecuteScalar()?.ToString() ?? "";}Paragraph tableTitle = new Paragraph(new Run(new Text($"Table: {tableName} - {tableComment}")));body.Append(tableTitle);// Add a gap between tablesbody.Append(new Paragraph(new Run(new Text(" "))));string query = $"DESCRIBE {tableName}";MySqlCommand command = new MySqlCommand(query, connection);using (MySqlDataReader reader = command.ExecuteReader()){Table wordTable = new Table();// Set table properties for A4 page size and fixed widthTableProperties tblProperties = new TableProperties(new TableWidth { Type = TableWidthUnitValues.Pct, Width = "5000" },new TableBorders(new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },new LeftBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },new RightBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },new InsideVerticalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 }));wordTable.AppendChild(tblProperties);// Add table headers with bold textTableRow headerRow = new TableRow();headerRow.Append(CreateTableCell("列名", "1500", true)); // 150 pxheaderRow.Append(CreateTableCell("字段类型", "1200", true)); // 120 pxheaderRow.Append(CreateTableCell("是否为空", "800", true));  // 80 pxheaderRow.Append(CreateTableCell("主键", "500", true));    // 50 pxheaderRow.Append(CreateTableCell("说明", "3000", true));   // Remaining widthwordTable.Append(headerRow);// Add table rowswhile (reader.Read()){TableRow dataRow = new TableRow();dataRow.Append(CreateTableCell(reader["Field"].ToString(), "1500"));dataRow.Append(CreateTableCell(reader["Type"].ToString(), "1200"));dataRow.Append(CreateTableCell(reader["Null"].ToString(), "800"));dataRow.Append(CreateTableCell(reader["Key"].ToString(), "500"));// Get column commentstring comment = "";using (MySqlConnection commentConnection = new MySqlConnection(connectionString)){commentConnection.Open();string commentQuery = $"SELECT COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{connection.Database}' AND TABLE_NAME = '{tableName}' AND COLUMN_NAME = '{reader["Field"].ToString()}';";MySqlCommand commentCommand = new MySqlCommand(commentQuery, commentConnection);comment = commentCommand.ExecuteScalar()?.ToString() ?? "";}dataRow.Append(CreateTableCell(comment, "3000"));wordTable.Append(dataRow);}body.Append(wordTable);}}mainPart.Document.Append(body);}}}static TableCell CreateTableCell(string text, string width, bool isBold = false){TableCell tableCell = new TableCell();TableCellProperties tableCellProperties = new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Pct, Width = width });tableCell.Append(tableCellProperties);Run run = new Run(new Text(text));if (isBold){run.RunProperties = new RunProperties(new Bold());}Paragraph paragraph = new Paragraph(run);tableCell.Append(paragraph);return tableCell;}}
}

  生成的数据字典效果如下:

  

遇到的问题如下:

      1.生成的时候,需要注意列宽,使之适应竖版A4的显示

       2.表名后面,需要加上中文的显示。

       3.每个表格之间,需要间隙,解决显示拥挤。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/844622.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

非递归线段树实现

ZKW 非递归线段树 参考文章:线段树详解(非递归版)_非递归线段树-CSDN博客 建树: 原数组[1,n] 存在线段树的 [2,n+1] (为了方便区间查询,要空出第一个节点和最后一个节点) 区间查询 若查询[L,R] 区间, 选取L-1和 R+1 两个节点, 向上寻找.每次到达父节点 L-1 查看自己的父亲的右…

hot100-一刷-01哈希(共3道题)

1.两数之和 题目链接 题目描述代码实现 分析:暴力的话就是两个for循环依次寻找相加为target的两个数。 用一个map记录已经遍历过的数,其中key就用这个数的字面值,value就存它的下标。 判断是否相加为taget的时候,只需要看map中是否有target-nums[i]就可以,说明当前的nums[…

Activiti 学习笔记

工作流概述 每一项业务的开始和结束,都可以理解为一个工作流,例如,公司的费用报销的基本流程如下:员工先提出费用报销申请,提交给部门领导,部门领导审批后,提交给财务部门审批,审批完成后,通知提出申请的员工可以报销,报销流程结束。整个流程按照步骤完成,这就是一个…

高级程序语言设计课第九次作业

14.17.314.17.414.17.514.17.1014.17.1114.18.314.18.4 a: b: 14.18.5

WEB AK赛-web1_观字

对这段代码进行解析: substr($url, 0, 7) 截取url变量前7位判断是否是http协议 preg_match() 正则表达式检查 URL 中是否包含一些潜在的危险字符: 过滤了:., ;, |, <, >, *, %, ^, () 这题只需要绕过"."即可。 尝试使用url编码不可以 在curl中可以使用"…

Fail pg walkthrough Intermediate

nmap ┌──(root㉿kali)-[/home/ftpuserr] └─# nmap -p- -A 192.168.159.126 Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-12-01 02:54 UTC Nmap scan report for 192.168.159.126 Host is up (0.071s latency). Not shown: 65533 closed tcp ports (reset) PORT …

通过实现 FactoryBean 接口注入 Bean

原文:如何使用 Spring 的 FactoryBean 接口在 Spring 容器中有两类的 Bean,一类是普通的 Bean,一类是工厂 Bean。这两种 Bean 都是被 Spring 的容器进行管理的。而 Spring 也提供了一个接口用于扩展工厂 Bean,我们只要实现org.springframework.beans.factory.FactoryBean即…

探索古诺尔斯语:揭开维京时代语言的神秘面纱

在历史的长河中,有一些语言以其独特的文化和历史价值,成为语言学家、历史学家乃至文学爱好者研究的宝贵资源。古诺尔斯语,作为现代北欧语言的祖先,正是这样一个充满魅力的语言。它不仅是冰岛语、瑞典语、挪威语、丹麦语、法罗语和艾尔夫达利语的源头,更是维京时代斯堪的纳…

探索古英语的奥秘:一部语言的时光机

在历史的长河中,语言如同一条蜿蜒的河流,承载着文化的变迁和人类的智慧。古英语,作为英语的早期形式,不仅见证了中世纪英格兰的辉煌,更是现代英语的根基。它如同一部时光机,带我们穿梭回那个英雄与传说并存的时代,让我们得以窥见语言的原始面貌和文化的魅力。古英语:语…

【Altium Designer 25.0.2下载与安装教程】

1、安装包 「AltiumDesigner v25.0.2.28.rar」 链接:https://pan.quark.cn/s/babcbc39d4b1 提取码:EPis 2、安装教程(建议关闭杀毒软件和系统防护) 1) 下载并解压下载的安装包,右击Installer.exe安装,弹窗安装对话框2) 点击Next3) 选择语言Chinese,I…

字符串比较内容、模拟用户登录案例

1.equals、equalsIgnoreCase 在之前我们使用“==”比较的是字符串的地址,但是地址对于我们来说没有用,我们要比较的是字符串的内容。 而equals和equalsIgnoreCase就是用于比较字符串的内容的两种方法1.equals 比较两个对象的内容是否一致,如果一致则为true,否则为false 调用…

JVM学习-03-垃圾收集器与内存分配策略

第三章、垃圾收集器与内存分配策略3.1 概述 垃圾收集需要完成的三件事情:哪些内存需要回收? 什么时候回收? 如何回收?3.2 对象已死? GC相关博客:JVM GC?我比《深入理解Java虚拟机》再深入一点点_技术交流_牛客网 (nowcoder.com) 3.2.1 引用计数算法 在Java 领域,至少主…