public void Export() { Directory.CreateDirectory(".//tmp"); //定义目录DataTable 结构 var dtMenus = new DataTable("<b>数据库表目录</b>"); dtMenus.Columns.Add("序号", typeof(int)); dtMenus.Columns.Add("表名", typeof(string)); dtMenus.Columns.Add("表说明", typeof(string)); //将选中项的总数 设置为进度条最大值 +1项是表目录文件 tpbExport.Value = 0; tpbExport.Maximum = ckbData.CheckedItems.Count + ckbTables.CheckedItems.Count + 1; //获取需要导出的表结构 选中项 List <string> lst = new List <string>(); foreach (var item in ckbTables.CheckedItems) { lst.Add(item.ToString()); } #region 导出表结构 if (lst.Count > 0) { lblMessage.Text = "准备表结构文件..."; //得到选中的表结构的字段信息 var lstDt = dal.GetTableStruct(lst); var pathTables = "./tmp/表结构"; Directory.CreateDirectory(pathTables); var tableIndex = 1; foreach (var dt in lstDt) { //得到表描述 var drs = dal.GetTables().Select("表名='" + dt.TableName + "'"); var desp = string.Empty; if (drs.Length > 0) { desp = drs[0]["表说明"].ToString(); } //创建表字段信息的html DbCommon.CreateHtml(dt, true, Path.Combine(pathTables, dt.TableName + " " + desp + ".html"), true, desp); //构建表目录 DataRow dr = dtMenus.NewRow(); dr["序号"] = tableIndex++; dr["表说明"] = desp; dr["表名"] = "<a href=\"表结构\\{0} {1}.html\">{0}</a>".FormatString(dt.TableName, desp); dtMenus.Rows.Add(dr); //改变进度 tpbExport.Value++; } //导出表目录 DbCommon.CreateHtml(dtMenus, false, "./tmp/" + defaultHtml, false); tpbExport.Value++; } #endregion #region 导出表数据 //传递需要导出数据的table选中项 得到数据内容 lst.Clear(); foreach (var item in ckbData.CheckedItems) { lst.Add(item.ToString()); } if (lst.Count > 0) { lblMessage.Text = "正在生成表数据数据..."; var lstDt = dal.GetTableData(lst); //创建常用数据的html var pathTables = "./tmp/常用数据"; Directory.CreateDirectory(pathTables); foreach (var dt in lstDt) { DbCommon.CreateHtml2(dt, true, Path.Combine(pathTables, dt.TableName + ".html")); tpbExport.Value++; } } #endregion try { lblMessage.Text = "正在编译CHM文件..."; //编译CHM文档 ChmHelp c3 = new ChmHelp(); c3.DefaultPage = defaultHtml; c3.Title = txtTitle.Text; c3.ChmFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), c3.Title + ".chm"); c3.RootPath = @"./tmp"; c3.Compile(); lblMessage.Text = "导出完毕 文件存储在:" + c3.ChmFileName; Directory.Delete("./tmp", true); } catch (Exception ex) { lblMessage.Text = "导出发生异常"; MessageBox.Show(ex.Message); } finally { this.EnableControl(true); } }
/// <summary> /// 导出 /// </summary> /// <param name="o"></param> public void Export(object o) { Directory.CreateDirectory(".//tmp"); //将日志文件复制到tmp文件夹下 if (txtLogPath.Text.Length > 0 && File.Exists(txtLogPath.Text)) { File.Copy(txtLogPath.Text, ".//tmp/更新日志.txt", true); } //定义目录DataTable 结构 var dtMenus = new DataTable("<b>数据库表目录</b>"); dtMenus.Columns.Add("序号", typeof(int)); dtMenus.Columns.Add("表名", typeof(string)); dtMenus.Columns.Add("表说明", typeof(string)); //将选中项的总数 设置为进度条最大值 +1项是表目录文件 tpbExport.Value = 0; tpbExport.Maximum = ckbData.CheckedItems.Count + ckbTables.CheckedItems.Count + 1; //获取需要导出的表结构 选中项 List <string> lst = new List <string>(); foreach (var item in ckbTables.CheckedItems) { lst.Add(item.ToString()); } #region 导出表结构 if (lst.Count > 0) { lblMessage.Text = "准备表结构文件..."; //得到选中的表结构的字段信息 var lstDt = dal.GetTableStruct(lst); var pathTables = "./tmp/表结构"; Directory.CreateDirectory(pathTables); var tableIndex = 1; foreach (var dt in lstDt) { //得到表描述 var drs = dal.GetTables().Select("表名='" + dt.TableName + "'"); var desp = string.Empty; if (drs.Length > 0) { desp = drs[0]["表说明"].ToString().RemoveValidFileChar(); } //创建表字段信息的html //得到文件存储路径 var tabPath = Path.Combine(pathTables, dt.TableName + ".html"); //指定table标题 将表的名称设置为超链接并返回 var title = "<a href='../" + defaultHtml + "'>" + dt.TableName + "</a>"; if (desp.Trim().Length > 0) { title += "(" + drs[0]["表说明"].ToString() + ")"; } DbCommon.CreateHtml(dt, true, tabPath, title); //构建表目录 DataRow dr = dtMenus.NewRow(); dr["序号"] = tableIndex++; dr["表说明"] = desp; dr["表名"] = "<a href=\"表结构\\{0}.html\">{0}</a>".FormatString(dt.TableName); dtMenus.Rows.Add(dr); //改变进度 tpbExport.Value++; } //导出表目录 DbCommon.CreateHtml(dtMenus, false, "./tmp/" + defaultHtml, "<b>数据库表目录</b>"); tpbExport.Value++; } #endregion #region 导出表数据 //传递需要导出数据的table选中项 得到数据内容 lst.Clear(); foreach (var item in ckbData.CheckedItems) { lst.Add(item.ToString()); } if (lst.Count > 0) { lblMessage.Text = "正在生成表数据数据..."; var lstDt = dal.GetTableData(lst); //创建常用数据的html var pathTables = "./tmp/常用数据"; Directory.CreateDirectory(pathTables); foreach (var dt in lstDt) { var tabPath = Path.Combine(pathTables, dt.TableName + ".html"); //指定table标题 将表的名称设置为超链接并返回 var title = "<a href='../" + defaultHtml + "'>" + dt.TableName + "</a>"; //得到表描述 var drs = dal.GetTables().Select("表名='" + dt.TableName + "'"); var desp = string.Empty; if (drs.Length > 0) { desp = drs[0]["表说明"].ToString(); } if (desp.Trim().Length > 0) { title += "(" + drs[0]["表说明"].ToString() + ")"; } DbCommon.CreateHtml(dt, true, tabPath, title); tpbExport.Value++; } } #endregion try { lblMessage.Text = "正在编译CHM文件..."; //编译CHM文档 ChmHelp c3 = new ChmHelp { DefaultPage = defaultHtml, Title = txtTitle.Text }; c3.ChmFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), c3.Title + ".chm"); c3.RootPath = @"./tmp"; c3.Compile(); lblMessage.Text = DateTime.Now.ToString() + " 导出成功"; Process.Start(c3.ChmFileName); Directory.Delete("./tmp", true); } catch (Exception ex) { lblMessage.Text = "导出发生异常"; MessageBox.Show(ex.Message); } finally { this.CloseLoading(); } }