private void btnGenerateDoc_Click(object sender, EventArgs e) { string fileName = string.Format("数据库设计文档.doc"); string saveDocFile = FileDialogHelper.SaveWord(fileName, "C:\\"); if (!string.IsNullOrEmpty(saveDocFile)) { try { #region 准备工作 Aspose.Words.Document doc = new Aspose.Words.Document(); doc.BuiltInDocumentProperties.Title = "数据库设计说明书"; doc.BuiltInDocumentProperties.Keywords = "数据库设计说明书"; doc.BuiltInDocumentProperties.CreatedTime = DateTimeHelper.GetServerDateTime2(); doc.BuiltInDocumentProperties.LastSavedTime = DateTimeHelper.GetServerDateTime2(); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); builder.PageSetup.PaperSize = PaperSize.A4; builder.PageSetup.Orientation = Aspose.Words.Orientation.Portrait; builder.InsertParagraph(); Dictionary <string, string> tableList = new Dictionary <string, string>(); tableList.Add("TB_DictType", "数据字典类型基础表"); tableList.Add("TB_DictData", "数据字典项目基础表"); tableList.Add("TB_LoginLog", "用户登陆日志表"); int i = 1; foreach (string key in tableList.Keys) { string description = string.Format("{0}({1})", key, tableList[key]); GenerateTableData(doc, builder, i++, description); } #endregion doc.Save(saveDocFile, SaveFormat.Doc); if (MessageDxUtil.ShowYesNoAndTips("保存成功,是否打开文件?") == System.Windows.Forms.DialogResult.Yes) { System.Diagnostics.Process.Start(saveDocFile); } } catch (Exception ex) { LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, ex, typeof(FrmAsposeWords)); MessageDxUtil.ShowError(ex.Message); return; } } }
private void GenerateTableData(Aspose.Words.Document doc, Aspose.Words.DocumentBuilder builder, int index, string description) { builder.Font.Size = 10; builder.Font.Bold = true; builder.Write(string.Format("{0}){1}", index, description)); Table table = builder.StartTable(); builder.RowFormat.HeadingFormat = true; builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; double totalWidth = 660; builder.InsertCell();// 添加一个单元格 builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.CellFormat.Width = totalWidth; builder.Font.Size = 9; builder.Font.Bold = true; builder.CellFormat.Shading.BackgroundPatternColor = Color.LightGray; builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; //水平居左对齐 builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直居中对齐 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.Write("字段列表"); builder.EndRow(); List <string> columFields = new List <string>() { "编号", "字段列名", "字段描述", "数据类型", "可空", "默认值", "约束类型" }; for (int i = 0; i < columFields.Count; i++) { builder.InsertCell();// 添加一个单元格 if (i == 0) { builder.CellFormat.Width = 40; } else if (i == 1 || i == 2) { builder.CellFormat.Width = 150; } else { builder.CellFormat.Width = 80; } builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//修改为水平居中对齐 builder.Write(columFields[i]); } builder.EndRow(); for (int i = 0; i < 20; i++) { for (int j = 0; j < 7; j++) { builder.InsertCell(); // 添加一个单元格 builder.Font.Bold = false; builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //修改内容为白色背景 builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; //修改为水平居左对齐 if (j == 0) { builder.CellFormat.Width = 40; } else if (j == 1 || j == 2) { builder.CellFormat.Width = 150; } else { builder.CellFormat.Width = 80; } if (j == 0) { builder.Write((i + 1).ToString()); } else { builder.Write("测试" + j.ToString()); } } builder.EndRow(); } builder.EndTable(); builder.InsertParagraph(); }
/// <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)); } }