创建表格,实现合并行、和并列、表居中、表格水平和垂直居中、设置单元格边框颜色和样式。
string templateFile = Server.MapPath("table_templ.doc"); string saveDocFile = Server.MapPath("table.doc"); Aspose.Words.Document doc = new Aspose.Words.Document(templateFile); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); #region 添加第一行 //第一行第一列 builder.InsertCell(); //垂直居中 builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //水平居中 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //合并行 builder.CellFormat.VerticalMerge = CellMerge.First; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write("合并行"); //第一行第二列 builder.InsertCell(); //合并列 builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write("合并列"); //第一行第三列 builder.InsertCell(); //合并列 builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //设置边框颜色 builder.CellFormat.Borders.Top.Color = System.Drawing.Color.Red; //设置边框样式 builder.CellFormat.Borders.Top.LineStyle = LineStyle.Double; //builder.CellFormat.Borders.Top.LineStyle = LineStyle.Single; //第一行第四列 builder.InsertCell(); //合并列 builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write("第一行第四列"); builder.EndRow(); #endregion #region 添加第二行 //第二行第一列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.Previous; builder.CellFormat.HorizontalMerge = CellMerge.None; //第二行第二列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write("第二行第二列"); //第二行第二列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write("第二行第三列"); //第二行第二列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write("第二行第四列"); builder.EndRow(); #endregion //表格水平居中 var table = (Aspose.Words.Tables.Table)doc.GetChild(NodeType.Table, 0, true); table.Alignment = TableAlignment.Center; doc.Save(saveDocFile);