.net Aspose.Words word插入表格
Code:
/// <summary> /// 导出word /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet] [Route("api/Values/Todoc")] public HttpResponseMessage Todoc(int id) { string saveDocFile = System.Web.Hosting.HostingEnvironment.MapPath("~/" + "img/") + DateTime.Now.ToString("yyyyMMdd")+ DateTime.Now.ToString("HHmmss") + ".docx"; Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); builder.InsertHtml("<h1>2020年郑州市青少年机器人竞赛</h1>"); builder.InsertHtml("<h1>暨第二十届河南省青少年机器人竞赛</h1>"); builder.InsertHtml("<h1>郑州分赛区获取报名系统</h1>"); var team = db.Team.FirstOrDefault(x => x.ID == id); #region 添加第一行 builder.InsertCell(); //第一行第一列 builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.RowFormat.Height = 25;//设置此行高度 builder.RowFormat.HeightRule = HeightRule.Exactly;//设置HeightRule builder.CellFormat.Orientation = TextOrientation.Horizontal; //设置单元格文本的格式 builder.Write("队伍名称"); //第一行第二列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第一行第三列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write(team?.Name); //第一行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第一行第五列 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.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.EndRow(); #endregion #region 添加第一行 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.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write(team?.Teacher); //第一行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第一行第五列 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.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write(team?.Mobile); //第一行第八列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; builder.EndRow(); #endregion #region 添加第一行 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.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write(team?.Email); //第一行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第一行第五列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; //第一行第六列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第一行第七列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; //第一行第八列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; builder.EndRow(); #endregion var student = db.Student.Where(x => x.TeamID == id).ToList(); CreatRowTo2(builder, student); var teacher = db.Teacher.Where(x => x.TeamID == id).ToList(); CreateRowTo3(builder, teacher); #region 添加第四行 //第四行第一列 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.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; //第四行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; builder.Write("第四行第二列"); //第四行第五列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第四行第六列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第四行第七列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第四行第八列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; builder.EndRow(); #endregion builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//文字对齐方式 //表格水平居中 var table = (Aspose.Words.Tables.Table)doc.GetChild(NodeType.Table, 0, true); table.Alignment = TableAlignment.Center; try { var docStream = new MemoryStream(); doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Docx)); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(docStream.ToArray()) }; result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = "File.docx" }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return result; } catch { return new HttpResponseMessage(HttpStatusCode.NoContent); } } /// <summary> /// 第二块 /// </summary> /// <param name="builder"></param> /// <param name="list"></param> public void CreatRowTo2(Aspose.Words.DocumentBuilder builder,List<Student> list) { #region 添加第二行 //第二行第一列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.First; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.Width = 10.00; 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.CellFormat.Width = 25.00; 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.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.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write("就读学校省份"); builder.EndRow(); #endregion int count = 0; if (list!=null && list.Count()>0) { count = list.Take(9).ToList().Count(); foreach (var item in list.Take(9).ToList()) { #region 添加第二行 //第二行第一列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.Previous; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.Width = 10.00; //第二行第二列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(item.Name); //第二行第三列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(item.Gender == 1 ? "男" : "女"); //第二行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(item.IDNumber); //第二行第五列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(item.Class); //第二行第六列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write(item.School); //第二行第七列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第二行第八列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(item.Province); builder.EndRow(); #endregion } } for (int i = count; i < 10; i++) { #region 添加第二行 //第二行第一列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.Previous; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.Width = 10.00; //第二行第二列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; //第二行第三列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; //第二行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; //第二行第五列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; //第二行第六列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; //第二行第七列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第二行第八列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.EndRow(); #endregion } } /// <summary> /// 第三块 /// </summary> /// <param name="builder"></param> /// <param name="list"></param> public void CreateRowTo3(Aspose.Words.DocumentBuilder builder, List<Teacher> list) { #region 添加第三行 //第三行第一列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.First; 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.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; 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.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.EndRow(); #endregion int count = 0; if (list != null && list.Count() > 0) { count = list.Take(3).ToList().Count(); foreach (var item in list.Take(3).ToList()) { #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(item.Name); //第三行第二列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(item.Gender == 1 ? "男" : "女"); //第三行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(item.IDNumber); //第三行第五列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write(item.Mobile); //第三行第六列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第三行第七列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; builder.Write(item.Email); //第一行第八列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; builder.EndRow(); #endregion } } for (int i = count; i < 3; i++) { #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.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; //第三行第四列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; //第三行第五列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; //第三行第六列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; //第三行第七列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First; //第一行第八列 builder.InsertCell(); builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.Previous; builder.EndRow(); #endregion } }