一键生成Oracle、SqlServer、MySql数据库表结构说明文档

一键生成Oracle、SqlServer、MySql数据库表结构说明文档

作为一名后台开发人员,平时工作中经常需要编写数据库表结构说明文档,手动编写显然不符合程序员的风格,在网上找了一些工具大多只支持单一类型的数据库,于是决定自己动手写一个。有兴趣的朋友可下载源码在我的基础上继续拓展。

工具下载链接

工具链接
源码链接

工具使用说明

运行工具需提前安装“.net framework 4.0”,目前支持Oracle、Mysql、Sqlserver这三种常见数据库,文档输出类型有word、html、PDF这三种。

工具使用界面效果图

一键生成Oracle、SqlServer、MySql数据库表结构说明文档_第1张图片

生成文档效果图

一键生成Oracle、SqlServer、MySql数据库表结构说明文档_第2张图片

核心代码

使用到的第三方DLL
Oracle.ManagedDataAccess.dll 该dll的好处在于不用安装oracle客户端可直连oracle数据库
MySql.Data.dll
Microsoft.Office.Interop.Word (word Api)

/// 
        /// 生成word文档
        /// 
        /// 进度条
        /// 字典表数据
        /// 输出路径
        /// 数据库名称
        /// 保存文件名
        /// 是否生成html
        public  void WriteWord(FrmShowProgress frm, List data, string path, string dbName, string saveFile, bool toHTML)
        {
            _Application application = null;
            _Document document = null;
            object oEndOfDoc = "\\endofdoc";
            try
            {
                int num6;
                int num12;
                application = new ApplicationClass
                {
                    Visible = false
                };
                document = application.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                Paragraph paragraph = document.Content.Paragraphs.Add(ref oMissing);
                paragraph.Range.Font.Name = "微软雅黑";
                paragraph.Range.Text = dbName + "数据库设计文档";
                paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
               // paragraph.Range.Font.Shadow = 1;
                paragraph.Range.Font.Size = 14f;
                paragraph.Format.SpaceAfter = 10f;
                paragraph.Range.InsertParagraphAfter();
                int numRows = data.Count + 1;
                int numColumns = 3;
                Range range = document.Bookmarks.get_Item(ref oEndOfDoc).Range; 
                Table table = document.Tables.Add(range, numRows, numColumns, ref oMissing, ref oMissing);
                table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                table.Range.ParagraphFormat.SpaceAfter = 1f;
                table.Range.Font.Size = 10f;
                table.Rows[1].Range.Font.Shadow = 1;
                table.Rows[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(1, 1).Range.Text = "序号";
                table.Cell(1, 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                table.Cell(1, 2).Range.Text = "表名";
                table.Cell(1, 2).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                table.Cell(1, 3).Range.Text = "说明";
                table.Cell(1, 3).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                table.Rows.First.Range.Font.Color = WdColor.wdColorBlack;
                int progress = 0;
                string message = string.Empty;
                int num4 = 10;
                int num5 = 90;
                for (num6 = 2; num6 <= numRows; num6++)
                {
                    progress = (int)((((num6 - 1) * 1.0) / ((double)(numRows * num4))) * 100.0);
                    try
                    {
                        frm.ShowProgress(string.Format("正在写入表名 {0} ", data[num6 - 2].TbName), progress, false);
                    }
                    catch
                    {
                    }
                    table.Rows[num6].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    table.Rows[num6].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    table.Rows[num6].Range.Font.Color = WdColor.wdColorBlack;
                    num12 = num6 - 1;
                    table.Cell(num6, 1).Range.Text = num12.ToString();
                    table.Cell(num6, 2).Range.Text = data[num6 - 2].TbName;
                    table.Cell(num6, 3).Range.Text = "";
                }
                for (num6 = 0; num6 < data.Count; num6++)
                {
                    message = string.Format("正在处理表 {0} ", data[num6].TbName);
                    progress = ((int)((((num6 + 1) * 1.0) * num5) / ((double)data.Count))) + num4;
                    if (progress > 100)
                    {
                        progress = 100;
                    }
                    try
                    {
                        frm.ShowProgress(message, progress, false);
                    }
                    catch
                    {
                    }
                    Paragraph paragraph2 = document.Content.Paragraphs.Add(ref oMissing);
                    paragraph2.Range.Font.Color = WdColor.wdColorBlack;
                    num12 = num6 + 1;
                    paragraph2.Range.Text = string.Format("{0}.{1}", num12.ToString(), data[num6].TbName);
                    paragraph2.Range.Font.Size = 10f;
                    paragraph2.Range.Font.Name = "宋体";
                    paragraph2.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    paragraph2.Range.InsertParagraphAfter();
                    int num7 = data[num6].TbCols.Count + 1;
                    int num8 = 6;
                    Range range2 = document.Bookmarks.get_Item(ref oEndOfDoc).Range; 
                    Table table2 = document.Tables.Add(range2, num7, num8, ref oMissing, ref oMissing);
                    table2.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                    table2.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                    table2.Range.ParagraphFormat.SpaceAfter = 1f;
                    table2.Cell(1, 1).Range.Text = "序号";
                    table2.Cell(1, 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                    table2.Cell(1, 2).Range.Text = "名称";
                    table2.Cell(1, 2).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                    table2.Cell(1, 3).Range.Text = "数据类型";
                    table2.Cell(1, 3).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                    table2.Cell(1, 4).Range.Text = "允许空值";
                    table2.Cell(1, 4).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                    table2.Cell(1, 5).Range.Text = "是否主键";
                    table2.Cell(1, 5).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                    table2.Cell(1, 6).Range.Text = "说明";
                    table2.Cell(1, 6).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                    table2.Rows[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    int row = 2;
                    while (row <= num7)
                    {
                        table2.Rows[row].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                        table2.Rows[row].Range.Font.Size = 10f;
                        table2.Rows[row].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                        table2.Cell(row, 1).Range.Text = (row - 1).ToString();
                        table2.Cell(row, 2).Range.Text = data[num6].TbCols[row - 2].Name;
                        table2.Cell(row, 3).Range.Text = data[num6].TbCols[row - 2].DataTypeEx;
                        table2.Cell(row, 4).Range.Text = data[num6].TbCols[row - 2].Nullable ? "" : "N";
                        table2.Cell(row, 5).Range.Text = data[num6].TbCols[row - 2].IsPK ? "PK" : "";
                        table2.Cell(row, 6).Range.Text = data[num6].TbCols[row - 2].Describle;
                        row++;
                    }
                    if (data[num6].TbIndexs.Count > 0)
                    {
                        Paragraph paragraph3 = document.Content.Paragraphs.Add(ref oMissing);
                        paragraph3.Range.Font.Color = WdColor.wdColorBlack;
                        paragraph3.Range.Text = "";
                        paragraph3.Range.Font.Size = 10f;
                        paragraph3.Range.Font.Name = "宋体";
                        paragraph3.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                        paragraph3.Range.InsertParagraphAfter();
                        int num10 = data[num6].TbIndexs.Count + 1;
                        int num11 = 2;
                        Range range3 = document.Bookmarks.get_Item(ref oEndOfDoc).Range; 
                        Table table3 = document.Tables.Add(range3, num10, num11, ref oMissing, ref oMissing);
                        table3.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                        table3.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                        table3.Range.ParagraphFormat.SpaceAfter = 1f;
                        table3.Cell(1, 1).Range.Text = "索引名称";
                        table3.Cell(1, 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                        table3.Cell(1, 2).Range.Text = "列名";
                        table3.Cell(1, 2).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
                        table3.Rows[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                        for (row = 2; row <= num10; row++)
                        {
                            table3.Rows[row].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                            table3.Rows[row].Range.Font.Size = 10f;
                            table3.Rows[row].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                            table3.Cell(row, 1).Range.Text = data[num6].TbIndexs[row - 2].IndexName;
                            table3.Cell(row, 2).Range.Text = data[num6].TbIndexs[row - 2].ColName;
                        }
                    }
                }
                TimeSpan span = DateTime.Now.Subtract(this.timeStart);
                if (span.Hours > 0)
                {
                    message = string.Format("word写入完毕! 用时{0}时{1}分{2}秒", span.Hours, span.Minutes, span.Seconds);
                }
                else if (span.Minutes > 0)
                {
                    message = string.Format("word写入完毕! 用时{0}分{1}秒", span.Minutes, span.Seconds);
                }
                else
                {
                    message = string.Format("word写入完毕! 用时{0}秒", span.Seconds);
                }
                progress = 100;
                try
                {
                    frm.ShowProgress(message, progress, true);
                }
                catch
                {
                }
                object fileName = path + @"\" + saveFile + ".doc";
                if (File.Exists(fileName.ToString()))
                {
                    File.Delete(fileName.ToString());
                }
                document.SaveAs(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                document.Close(ref oMissing, ref oMissing, ref oMissing);
                application.Quit(ref oMissing, ref oMissing, ref oMissing);
                document = null;
                application = null;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (toHTML)
                {
                    this.DocFomartHTML(frm, path, saveFile);
                }
               this.KillAllWinWord();
            }
        }
 /// 
       /// 将word转换成html
       /// 
       /// 进度条
       /// 文件存储路径
       /// 保存的文件名
        public void DocFomartHTML(FrmShowProgress frm, string path, string saveFile)
        {
            try
            {
                _Application application = null;
                _Document target = null;
                application = new ApplicationClass
                {
                    Visible = false
                };
                object fileName = path + @"\" + saveFile + ".doc";
                target = application.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                string message = "正在进行HTML格式转换中";
                try
                {
                    frm.ShowMsg(message, 0, true);
                }
                catch
                {
                }
                Type type = application.Documents.GetType();
                Type type2 = target.GetType();
                object obj3 = path + @"\" + saveFile + ".html";
                type2.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, target, new object[] { obj3, WdSaveFormat.wdFormatHTML });
                target.Save();
                try
                {
                    frm.ShowMsg("HTML格式转换完成", 0, true);
                }
                catch
                {
                }
                target.Close(ref oMissing, ref oMissing, ref oMissing);
                application.Documents.Close(ref oMissing, ref oMissing, ref oMissing);
                application.Quit(ref oMissing, ref oMissing, ref oMissing);
                target = null;
                application = null;
            }
            catch (Exception)
            {
            }
        }
  private void KillAllWinWord()
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (process.Id != 0)
                {
                    string str = "WINWORD.EXE" + process.ProcessName + "  ID:" + process.Id.ToString();
                    try
                    {
                        try
                        {
                            if ((process.Modules != null) && (process.Modules.Count > 0))
                            {
                                ProcessModule module = process.Modules[0];
                                str = ((str + "\n Modules[0].FileName:" + module.FileName) + "\n Modules[0].ModuleName:" + module.ModuleName) + "\n Modules[0].FileVersionInfo:\n" + module.FileVersionInfo.ToString();
                                if (module.ModuleName.ToLower() == "winword.exe")
                                {
                                    process.Kill();
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    finally
                    {
                    }
                }
            }
        }

你可能感兴趣的:(文档生成工具)