Exemplo n.º 1
0
        private void btn_SaveHTML_Click(object sender, EventArgs e)
        {
            var model = new ThreadModel()
            {
                dbName     = cmb_DBList.Text,
                savePath   = txt_SaveHtmlPath.Text,
                dataSource = txt_dataSource.Text,
                port       = txt_PORT.Text,
                uid        = txt_uid.Text,
                pwd        = txt_pwd.Text,
            };

            //创建Thread对象
            Thread thread = new Thread(SaveHtml);

            thread.IsBackground = true;
            //启动线程
            thread.Start(model);
        }
Exemplo n.º 2
0
        void SaveHtml(object obj)
        {
            ThreadModel threadModel = obj as ThreadModel;

            string dbName = threadModel.dbName;

            #region 验证是否为空

            if (string.IsNullOrEmpty(dbName))
            {
                MessageBox.Show("请选择数据库");
                return;
            }

            if (string.IsNullOrEmpty(threadModel.savePath))
            {
                MessageBox.Show("HTML导出路径不能为空");
                return;
            }

            #endregion

            #region 获取数据

            StringBuilder str = new StringBuilder();
            str.Append("<div align=\"center\">");
            str.Append("<div>");
            str.Append($"<h1>{dbName} 数据库文档</h1>");
            str.Append("</div>");

            string conn = $"data source={threadModel.dataSource};database={dbName};PORT={threadModel.port};uid={threadModel.uid};pwd={threadModel.pwd};";

            var tables = (tablesList != null && tablesList.Count > 0) ? tablesList : GetDBTables(conn, dbName);

            var tableInfos = GetTableInfoList(conn, dbName);

            int tableNum = 1;
            foreach (var table in tables)
            {
                str.Append("<div>");
                str.Append("<table width=\"80%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
                str.Append("<tr>");
                str.Append($"<td colspan=\"6\" align=\"center\" style=\"color: red; font - size:30px;\"><b> 表名({ table.table_name})</b></td>");
                str.Append("</tr>");
                str.Append("<tr align=\"center\" style=\"color:red;\">");
                str.Append("<td><b>序号</b></td>");
                str.Append("<td><b>字段名称</b></td>");
                str.Append("<td><b>类型</b></td>");
                str.Append("<td><b>是否为null</b></td>");
                str.Append("<td><b>是否主键</b></td>");
                str.Append("<td><b>字段描述</b></td>");
                str.Append("</tr>");

                #region 循环写入列名

                var infoList = tableInfos.Where(x => x.TABLE_NAME.Equals(table.table_name)).ToList();

                int i = 1;

                foreach (var info in infoList)
                {
                    str.Append("<tr align=\"center\">");
                    str.Append($"<td>{i}</td>");                    //序号
                    str.Append($"<td>{info.column_name}</td>");     //字段名称
                    str.Append($"<td>{info.column_type}</td>");     //字段类型
                    str.Append($"<td>{info.IS_NULLABLE}</td>");     //是否为null
                    str.Append($"<td>{info.COLUMN_KEY}</td>");      //是否为主键
                    str.Append($"<td>{info.COLUMN_COMMENT}</td>");  //字段描述
                    str.Append("</tr>");

                    i++;

                    if (!lbl_info.InvokeRequired)
                    {
                        lbl_info.Text = $"正在导出HTML文档请稍等,一共有({tables.Count})张表,已处理({tableNum})张表,已处理完({table.table_name})表";
                    }
                    else
                    {
                        lbl_info.Invoke(new Action(() =>
                        {
                            lbl_info.Text = $"正在导出HTML文档请稍等,一共有({tables.Count})张表,已处理({tableNum})张表,已处理完({table.table_name})表";
                        }));
                    }
                    Thread.Sleep(5);
                }

                str.Append("</table>");

                #endregion

                str.Append("<br/><br/>");

                tableNum++;
            }

            #endregion

            str.Append("</div>");

            #region 保存 HTML 文件

            string saveName = $"{dbName}_数据库文档_{DateTime.Now.ToString("yyyy-MM-dd")}.html";

            string path = Path.Combine(threadModel.savePath, saveName);

            using (FileStream fileStream = File.Create(path))
            {
                byte[] mybyte = Encoding.UTF8.GetBytes(str.ToString());
                fileStream.Write(mybyte, 0, mybyte.Length);
            }

            #endregion

            if (MessageBox.Show("保存成功,是否打开文件", "提示消息", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                var result1 = Process.Start("explorer.exe", path);
            }
        }
Exemplo n.º 3
0
        void SaveWord(object obj)
        {
            ThreadModel threadModel = obj as ThreadModel;

            if (string.IsNullOrEmpty(threadModel.dbName))
            {
                MessageBox.Show("请选择数据库");
                return;
            }

            if (string.IsNullOrEmpty(threadModel.savePath))
            {
                MessageBox.Show("word保存路径不能为空");
                return;
            }

            //创建document对象
            var doc = new XWPFDocument();

            //创建段落对象1
            var p1 = doc.CreateParagraph();

            p1.Alignment = ParagraphAlignment.CENTER; //字体居中

            var runTitle = p1.CreateRun();

            runTitle.IsBold = true;
            runTitle.SetText($"{threadModel.dbName} 数据库文档");
            runTitle.FontSize = 16;
            runTitle.SetFontFamily("宋体", FontCharRange.None); //设置雅黑字体

            doc.CreateParagraph();
            doc.CreateParagraph();

            string conn = $"data source={threadModel.dataSource};database={threadModel.dbName};PORT={threadModel.port};uid={threadModel.uid};pwd={threadModel.pwd};";

            try
            {
                var tables = (tablesList != null && tablesList.Count > 0) ? tablesList : GetDBTables(conn, threadModel.dbName);

                var tableInfolist = GetTableInfoList(conn, threadModel.dbName);

                int tableNum = 1;

                foreach (var table in tables)
                {
                    var infoList = tableInfolist.Where(x => x.TABLE_NAME.Equals(table.table_name)).ToList();

                    #region 创建表格,循环写入列名

                    #region 创建表格、设置宽度、合并

                    var tableContent = doc.CreateTable(infoList.Count + 2, 6);
                    //tableContent.Width = 1000 * 5;

                    tableContent.SetColumnWidth(0, 600);
                    tableContent.SetColumnWidth(1, 1500);
                    tableContent.SetColumnWidth(2, 1000);
                    tableContent.SetColumnWidth(3, 1500);
                    tableContent.SetColumnWidth(4, 1000);
                    tableContent.SetColumnWidth(5, 2500);

                    tableContent.GetRow(0).MergeCells(0, 5); //合并

                    #endregion

                    #region 设置背景颜色

                    tableContent.GetRow(0).GetCell(0).SetColor("#fbd4b4");
                    tableContent.GetRow(1).GetCell(0).SetColor("#fbd4b4");
                    tableContent.GetRow(1).GetCell(1).SetColor("#fbd4b4");
                    tableContent.GetRow(1).GetCell(2).SetColor("#fbd4b4");
                    tableContent.GetRow(1).GetCell(3).SetColor("#fbd4b4");
                    tableContent.GetRow(1).GetCell(4).SetColor("#fbd4b4");
                    tableContent.GetRow(1).GetCell(5).SetColor("#fbd4b4");

                    #endregion

                    #region 创建表头

                    tableContent.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, tableContent, $"表名({table.table_name})"));
                    tableContent.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc, tableContent, "序号"));
                    tableContent.GetRow(1).GetCell(1).SetParagraph(SetCellText(doc, tableContent, "字段名称"));
                    tableContent.GetRow(1).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "类型"));
                    tableContent.GetRow(1).GetCell(3).SetParagraph(SetCellText(doc, tableContent, "是否为null"));
                    tableContent.GetRow(1).GetCell(4).SetParagraph(SetCellText(doc, tableContent, "是否主键"));
                    tableContent.GetRow(1).GetCell(5).SetParagraph(SetCellText(doc, tableContent, "字段描述"));

                    #endregion

                    int i = 2;//从表格第三行开始写入列

                    #region 循环写入列

                    foreach (var item in infoList)
                    {
                        tableContent.GetRow(i).GetCell(0).SetParagraph(SetCellText(doc, tableContent, (i - 1).ToString(), ParagraphAlignment.CENTER, 50));  //序号

                        tableContent.GetRow(i).GetCell(1).SetParagraph(SetCellText(doc, tableContent, item.column_name, ParagraphAlignment.CENTER, 50));    //字段名称

                        tableContent.GetRow(i).GetCell(2).SetParagraph(SetCellText(doc, tableContent, item.column_type, ParagraphAlignment.CENTER, 50));    //字段类型

                        tableContent.GetRow(i).GetCell(3).SetParagraph(SetCellText(doc, tableContent, item.IS_NULLABLE, ParagraphAlignment.CENTER, 50));    //是否为null

                        tableContent.GetRow(i).GetCell(4).SetParagraph(SetCellText(doc, tableContent, item.COLUMN_KEY, ParagraphAlignment.CENTER, 50));     //是否为主键

                        tableContent.GetRow(i).GetCell(5).SetParagraph(SetCellText(doc, tableContent, item.COLUMN_COMMENT, ParagraphAlignment.CENTER, 50)); //字段描述

                        i++;

                        if (!lbl_info.InvokeRequired)
                        {
                            lbl_info.Text = $"正在导出Word文档请稍等,一共有({tables.Count})张表,已处理({tableNum})张表,已处理完({table.table_name})表";
                        }
                        else
                        {
                            lbl_info.Invoke(new Action(() =>
                            {
                                lbl_info.Text = $"正在导出Word文档请稍等,一共有({tables.Count})张表,已处理({tableNum})张表,已处理完({table.table_name})表";
                            }));
                        }
                        Thread.Sleep(5);
                    }
                    #endregion

                    tableNum++;

                    #endregion

                    doc.CreateParagraph();//新增段
                }

                #region 保存word文件
                string saveName = $"{threadModel.dbName}_数据库文档_{DateTime.Now.ToString("yyyy-MM-dd")}.doc";

                string path = Path.Combine(txt_SaveWordPath.Text, saveName);

                using (FileStream sw = File.Create(path))
                {
                    doc.Write(sw);
                    //sw.Close();
                }

                if (MessageBox.Show("保存成功,是否打开文件", "提示消息", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    Process.Start(path);
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }