Esempio n. 1
0
 public void WriteBookmark(string bookmarkName, string bookmarkContent)
 {
     wDocBuilder.StartBookmark(bookmarkName);
     wDocBuilder.Write(bookmarkContent);
     wDocBuilder.EndBookmark(bookmarkName);
     AW.Bookmark bm = wDoc.Range.Bookmarks[bookmarkName];
 }
Esempio n. 2
0
        /// <summary>
        ///导出数据库设计报告文档
        /// </summary>
        public ActionResult CreateDBDoc(string dataBaseLinkId)
        {
            try
            {
                var watch = CommonHelper.TimerStart();
                //获取所有用户表信息
                DataTable dtTableLIst = dataBaseTableBLL.GetTableList(dataBaseLinkId, "");
                dtTableLIst.TableName = "T";

                //加载导出模板
                Aspose.Words.Document        doc = new Aspose.Words.Document(Server.MapPath("~/Resource/Temp/db.doc"));
                Aspose.Words.DocumentBuilder db  = new Aspose.Words.DocumentBuilder(doc);

                //填充2.1表汇总信息
                doc.MailMerge.ExecuteWithRegions(dtTableLIst);
                //系统中文名称
                string zhName = Config.GetValue("SystemName");
                //软件英文名称
                string enName = Config.GetValue("SoftName");
                //程序版本
                string version = Config.GetValue("Version");
                string appName = zhName + " " + version + "(" + enName + ")";
                //数据库连接信息
                DataBaseLinkEntity dbLink = databaseLinkBLL.GetEntity(dataBaseLinkId);
                //获取数据库类型及版本
                string dbName = dbLink.DbType + dbLink.Version;
                //填充基本信息
                doc.MailMerge.Execute(new string[] { "appName", "dbName" }, new string[] { appName, dbName });
                //表详细设计起始编号
                string num = "2.2.";
                //开始绘制表格信息
                db.MoveToDocumentEnd();
                int j = 1;
                foreach (DataRow dr in dtTableLIst.Rows)
                {
                    //表显示名称
                    string title = dr["name"].ToString();
                    if (dr["tdescription"] != DBNull.Value && dr["tdescription"] != null)
                    {
                        title = num + j + " " + dr["tdescription"].ToString() + "(" + title + ")";
                    }
                    else
                    {
                        title = num + j + " " + dr["name"].ToString();
                    }
                    //定义书签名称
                    string bookName = "mark" + j;
                    //开始创建书签并填充信息
                    db.StartBookmark(bookName);
                    db.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
                    db.ParagraphFormat.Alignment       = Aspose.Words.ParagraphAlignment.Left;
                    db.Bold = true;
                    db.Write(title);
                    db.EndBookmark(bookName);

                    //表名
                    string tableName = dr["name"].ToString();
                    //获取表结构
                    var tableInfo = dataBaseTableBLL.GetTableFiledList(dataBaseLinkId, tableName).ToList();
                    db.MoveToDocumentEnd();
                    //开始绘制表结构表格
                    db.StartTable();
                    //表头设置
                    db.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
                    db.ParagraphFormat.Alignment       = Aspose.Words.ParagraphAlignment.Center;
                    db.RowFormat.Alignment             = Aspose.Words.Tables.RowAlignment.Center;
                    db.Bold = true;
                    db.CellFormat.Borders.Color     = Color.Black;
                    db.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
                    db.InsertCell();
                    db.Write("列名");
                    db.InsertCell();
                    db.Write("数据类型");
                    db.InsertCell();
                    db.Write("可为空");
                    db.InsertCell();
                    db.Write("字段描述");
                    db.InsertCell();
                    db.Write("备注");
                    db.EndRow();

                    //开始绘制列信息
                    foreach (DataBaseTableFieldEntity field in tableInfo)
                    {
                        db.Bold = false;
                        db.ParagraphFormat.Alignment    = Aspose.Words.ParagraphAlignment.Center;
                        db.RowFormat.Alignment          = Aspose.Words.Tables.RowAlignment.Center;
                        db.CellFormat.Borders.Color     = Color.Black;
                        db.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
                        db.InsertCell();
                        db.Write(field.column_name); //列名称
                        db.InsertCell();
                        db.Write(field.datatype);    //列类型
                        db.InsertCell();
                        //是否非空
                        db.Write(field.isnullable == "1"?"否":"是");
                        //列描述
                        db.InsertCell();
                        if (string.IsNullOrEmpty(field.remark))
                        {
                            db.Write("");
                        }
                        else
                        {
                            db.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Left;
                            db.Write(field.remark);
                        }
                        //备注
                        db.InsertCell();
                        db.Write("");
                        db.EndRow();
                    }
                    db.EndTable();
                    db.ParagraphFormat.ClearFormatting();
                    db.InsertParagraph(); db.InsertParagraph();
                    j++;
                }
                //生成文档目录导航
                db.ParagraphFormat.ClearFormatting();
                //移动到目录书签
                db.MoveToBookmark("dir");
                //db.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

                Aspose.Words.Layout.LayoutCollector lc = new Aspose.Words.Layout.LayoutCollector(doc);
                j = 1;
                foreach (DataRow dr in dtTableLIst.Rows)
                {
                    string title = num + j + " " + dr["name"].ToString();
                    if (dr["tdescription"] != DBNull.Value && dr["tdescription"] != null)
                    {
                        title = num + j + " " + dr["tdescription"].ToString();
                    }
                    string bookName = "mark" + j;
                    db.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
                    //获取表书签所在的页码
                    string page = "";
                    if (doc.Range.Bookmarks[bookName] != null)
                    {
                        int pageIndex = lc.GetStartPageIndex(doc.Range.Bookmarks[bookName].BookmarkStart);
                        page = pageIndex.ToString();
                    }
                    //db.Bold = false;
                    //生成目录链接导航
                    //db.InsertHyperlink(title, bookName, true);
                    j++;
                }
                //设置文件名
                string fileName = zhName + "数据库设计报告_" + DateTime.Now.ToString("yyyyMMdd") + ".doc";
                doc.Save(Server.MapPath("~/Resource/Temp/" + fileName));

                return(Success("操作成功", fileName + "$" + CommonHelper.TimerEnd(watch)));
            }
            catch (Exception ex)
            {
                return(Error(ex.Message));
            }
        }