예제 #1
0
        private void ExportHtml()
        {
            string tableDataDirName      = "表数据示例";
            string tableStructureDirName = "表结构说明";

            Directory.CreateDirectory(".//tmp");

            Directory.CreateDirectory(".//tmp//resources");


            //定义目录DataTable 结构
            Synyi.DBChmCreater.Entity.DataTableCollection dataTableStructures = new Synyi.DBChmCreater.Entity.DataTableCollection("<b>数据库表目录</b>");

            //将选中项的总数 设置为进度条最大值 +1项是表目录文件
            Dispatcher.Invoke(new Action(() =>
            {
                tpbExport.Value   = 0;
                tpbExport.Maximum = ckbData.SelectedItems.Count + ckbTables.SelectedItems.Count + 1;// ckbData.CheckedItems.Count + ckbTables.CheckedItems.Count + 1;
            }));

            //获取需要导出的表结构 选中项
            List <string> selectTabStructList = new List <string>();

            foreach (var item in ckbTables.SelectedItems) // CheckedItems)
            {
                selectTabStructList.Add(item.ToString());
            }

            #region 导出表结构 数据字典部分 导出为html
            if (selectTabStructList.Count > 0)
            {
                lblMessage.Content = "准备表结构文件...";
                //得到选中的表结构的字段信息
                var lstDt      = dal.GetTableStruct(selectTabStructList);
                var pathTables = $"./tmp/{tableStructureDirName}";
                Directory.CreateDirectory(pathTables);
                var tables     = dal.GetTables();
                var allSchemas = tables.Select(p => p.Domain).Distinct();
                foreach (var item in allSchemas)
                {
                    Directory.CreateDirectory(System.IO.Path.Combine(pathTables, item));
                }
                var tableIndex = 1;
                foreach (var dt in lstDt)
                {
                    //得到表描述
                    var array   = dt.TableName.Split('.');
                    var domain  = array[0];
                    var tabname = array[1];
                    var drs     = tables.Where(p => p.TableName == tabname).Where(p => p.Domain == domain).FirstOrDefault();// Select("表名='" + tabname + "' and 域='" + domain + "'");
                    var desp    = string.Empty;
                    if (drs != null)
                    {
                        desp = drs.TableDescription;
                    }
                    //创建表字段信息的html
                    HtmlHelp.CreateHtml(dt, true, System.IO.Path.Combine(pathTables, dt[0].tableschema, $"{dt.TableName}.html"), true, desp, true);
                    //构建表目录
                    dataTableStructures.Add(new DataTableItem {
                        TableNo = tableIndex++, Domain = drs.Domain, TableName = $"<a href=\"{tableStructureDirName}\\{drs.Domain}\\{ dt.TableName}.html\">{ dt.TableName.Split('.').GetValue(1)}</a>", TableDescription = desp
                    });
                    //改变进度
                    Dispatcher.Invoke(new Action(() => { tpbExport.Value++; }));
                }
                //导出表目录
                HtmlHelp.CreateHtml(dataTableStructures, false, "./tmp/" + defaultHtml, false, string.Empty, false); //默认页面
                Dispatcher.Invoke(new Action(() => { tpbExport.Value++; }));
            }
            #endregion

            #region 导出表数据  表中的数据示例 导出为html
            //传递需要导出数据的table选中项  得到数据内容
            selectTabStructList.Clear();
            foreach (var item in ckbData.SelectedItems)  //CheckedItems)
            {
                selectTabStructList.Add(item.ToString());
            }
            if (selectTabStructList.Count > 0)
            {
                Dispatcher.Invoke(new Action(() => { lblMessage.Content = "正在生成表数据数据..."; }));

                //读取ini
                int result = ini.GetInt32("Set", "maxrow", -1);

                var lstDt = dal.GetTableData(selectTabStructList, result);
                //创建常用数据的html
                var pathTables = $"./tmp/{tableDataDirName}";
                Directory.CreateDirectory(pathTables);
                var selecttabdataschemas = selectTabStructList.Select(p => p.Split('.')[0]).Distinct();
                foreach (var item in selecttabdataschemas)
                {
                    Directory.CreateDirectory(System.IO.Path.Combine(pathTables, item));
                }

                foreach (var dt in lstDt)
                {
                    HtmlHelp.CreateHtml2(dt, true, System.IO.Path.Combine(System.IO.Path.Combine(pathTables, dt.TableName.Split('.')[0]), dt.TableName + ".html"), true);
                    Dispatcher.Invoke(new Action(() => { tpbExport.Value++; }));
                }
            }

            Dispatcher.Invoke(new Action(() => { lblMessage.Content = "成功生成HTML..."; }));
            #endregion
        }
예제 #2
0
        /// <summary>
        ///  导出表数据为html格式 居中表格样式
        /// </summary>
        /// <param name="dt">DataTable,需要给TableName赋值</param>
        /// <param name="KeepNull">保持Null为Null值,否则为空</param>
        /// <param name="Path">保存路径</param>
        /// <param name="hasReturn">携带返回目录链接</param>
        /// <param name="tableDesc">携带返回目录链接</param>
        /// <param name="alternateColor">是否隔行变色</param>
        public static void CreateHtml(Synyi.DBChmCreater.Entity.DataTableCollection dt, bool KeepNull, string Path, bool hasReturn = true, string tableDesc = "", bool alternateColor = false)
        {
            var code = new StringBuilder();

            code.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            code.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            code.AppendLine("<head>");
            code.AppendLine("    <META http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"> ");
            code.AppendLine($"    <title>{dt.Name}</title>");
            code.AppendLine("    <style type=\"text/css\">");
            code.AppendLine("        body");
            code.AppendLine("        {");
            code.AppendLine("            font-size: 9pt;");
            code.AppendLine("        }");
            code.AppendLine("        .styledb");
            code.AppendLine("        {");
            code.AppendLine("            font-size: 14px;");
            code.AppendLine("        }");
            code.AppendLine("        .styletab");
            code.AppendLine("        {");
            code.AppendLine("            font-size: 14px;");
            code.AppendLine("            padding-top: 15px;");
            code.AppendLine("        }");
            code.AppendLine("        a");
            code.AppendLine("        {");
            code.AppendLine("            color: #015FB6;");
            code.AppendLine("        }");
            code.AppendLine("        a:link, a:visited, a:active");
            code.AppendLine("        {");
            code.AppendLine("            color: #015FB6;");
            code.AppendLine("            text-decoration: none;");
            code.AppendLine("        }");
            code.AppendLine("        a:hover");
            code.AppendLine("        {");
            code.AppendLine("            color: #E33E06;");
            code.AppendLine("        }");
            if (alternateColor)
            {
                code.AppendLine("		tr:nth-child(even) {bgcolor: #CCC}");
                code.AppendLine("		tr:nth-child(odd) {bgcolor: #FFF}");
            }
            code.AppendLine("    </style>");
            code.AppendLine("</head>");
            code.AppendLine("<body>");
            code.AppendLine("    <div style=\"text-align: center\">");
            code.AppendLine("        <div>");
            code.AppendLine("            <table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"90%\" style=\"text-align: left\">");
            code.AppendLine("                <tr>");
            code.AppendLine("                    <td bgcolor=\"#FBFBFB\">");
            code.AppendLine("                        <table cellspacing=\"0\" cellpadding=\"5\" border=\"1\" width=\"100%\" bordercolorlight=\"#D7D7E5\" bordercolordark=\"#D3D8E0\">");
            code.AppendLine("                        <caption>");
            code.AppendLine($"        <div class=\"styletab\">{dt.Name}{(tableDesc.Length == 0 ? string.Empty : "  (" + tableDesc + ") ")}{(hasReturn ? "<a href='../../数据库表目录.html' style='float: right; margin-top: 6px;'>返回目录</a>" : string.Empty)}</div>");//.FormatString(
            code.AppendLine("                        </caption>");
            code.AppendLine("                        <tr bgcolor=\"#DEEBF7\">");
            //构建表头

            Type itemtype = typeof(DataTableItem);

            var dtStructProps = itemtype.GetProperties();

            foreach (PropertyInfo dc in dtStructProps)
            {
                var prop = dc.GetCustomAttribute <DescriptionAttribute>();
                code.AppendLine($"            <td>{prop.Description}</td>");//属性描述 作为列名
            }
            code.AppendLine("                         </tr>");
            //构建数据行

            var dtgroupByDomain = dt.GroupBy(p => p.Domain).OrderBy(p => p.Key);

            foreach (var domainitem in dtgroupByDomain)
            {
                //一个domain
                int count = domainitem.Count();
                int i     = 0;
                var ere   = domainitem.OrderBy(p => p.TableNo);
                foreach (DataTableItem dritem in ere)
                {
                    code.AppendLine("            <tr>");
                    if (i == 0)
                    {
                        string sfsd = $"rowspan = \"{count}\"";
                        //合并单元格
                        foreach (PropertyInfo dc in dtStructProps)
                        {
                            if (KeepNull && dc.GetValue(dritem) == DBNull.Value)
                            {
                                code.AppendLine("            <td>&nbsp;</td>");
                            }
                            else
                            {
                                if (dc.Name == "Domain")
                                {
                                    code.AppendLine($"            <td {sfsd}>{(dc.GetValue(dritem).ToString().Trim().Length > 0 ? dc.GetValue(dritem).ToString() : "&nbsp;")}</td>");
                                }
                                else
                                {
                                    code.AppendLine($"            <td>{(dc.GetValue(dritem).ToString().Trim().Length > 0 ? dc.GetValue(dritem).ToString() : "&nbsp;")}</td>");
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (PropertyInfo dc in dtStructProps)
                        {
                            if (dc.Name == "Domain")
                            {
                                continue;
                            }
                            if (KeepNull && dc.GetValue(dritem) == DBNull.Value)
                            {
                                code.AppendLine("            <td>&nbsp;</td>");
                            }
                            else
                            {
                                code.AppendLine($"            <td>{(!string.IsNullOrWhiteSpace(dc.GetValue(dritem)?.ToString()) ? dc.GetValue(dritem)?.ToString() : "&nbsp;")}</td>");
                            }
                        }
                    }
                    i++;
                    code.AppendLine("            </tr>");
                }
            }



            //foreach (DataTableStructure dr in dt)
            //{
            //    code.AppendLine("            <tr>");
            //    foreach (PropertyInfo dc in dtStructProps)
            //    {
            //        if (KeepNull &&  dc.GetValue(dr) == DBNull.Value)
            //        {
            //            code.AppendLine("            <td>&nbsp;</td>");
            //        }
            //        else
            //        {
            //            code.AppendLine($"            <td>{(dc.GetValue(dr).ToString().Trim().Length > 0 ? dc.GetValue(dr).ToString() : "&nbsp;")}</td>");
            //        }
            //    }
            //    code.AppendLine("            </tr>");
            //}
            code.AppendLine("                        </table>");
            code.AppendLine("                    </td>");
            code.AppendLine("                </tr>");
            code.AppendLine("            </table>");
            code.AppendLine("        </div>");
            code.AppendLine("    </div>");
            code.AppendLine("</body>");
            code.AppendLine("</html>");
            File.WriteAllText(Path, code.ToString(), Encoding.GetEncoding("gb2312"));
            //File.WriteAllText(Path, code.ToString(), Encoding.UTF8);
        }