2012-03-10 12:41:17|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:C#, Office套件|来源:唯设编程网
在Win7环境C#使用Word模板生成报告(一)中,对C#开发Word的配置环境进行了详细的介绍,Win7环境C#使用Word模板生成报告(二)对其进行了完善和补充,详细介绍C#使用Word模板生成报告的具体方法,包括通过模板创建新的Word文档,保存文档,使用文本内容替换Word模板中的书签,在模板中插入图片、段落、表格等。本文再次对C#使用Word模板生成报告进行补充,将重点讨论生成Word报告时对表格进行的各种操作,包括合并单元格、设置Word表格内容的对齐方式、设置Word表格字体、设置Word表格边框、增加单元格、填充单元格等内容。
/// <summary> /// 合并单元格 /// </summary> /// <param name="table">表名</param> /// <param name="row1">开始行号</param> /// <param name="column1">开始列号</param> /// <param name="row2">结束行号</param> /// <param name="column2">结束列号</param> public void MergeCell(Table table, int row1, int column1, int row2, int column2) { table.Cell(row1, column1).Merge(table.Cell(row2, column2)); }
/// <summary> /// 设置表格内容的对齐方式 /// </summary> /// <param name="table">表名</param> /// <param name="Align">水平对齐方式:-1-左对齐,0-居中对其,1右对齐</param> /// <param name="Vertical">垂直对齐方式</param> public void SetParagraph_Table(Table table, int Align, int Vertical) { ParagraphFormat pFormat = table.Range.ParagraphFormat; switch (Align) { case -1: pFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break; case 0: pFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break; case 1: pFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break; } Cells cells = table.Range.Cells; switch (Vertical) { case -1: cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break; case 0: cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break; case 1: cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break; } }
/// <summary> /// 设置表格字体 /// </summary> /// <param name="table"></param> /// <param name="fontName"></param> /// <param name="size"></param> public void SetFont_Table(Table table, string fontName, double size) { if (size != 0) { table.Range.Font.Size = Convert.ToSingle(size); } if (fontName != "") { table.Range.Font.Name = fontName; } }
/// <summary> /// 是否使用边框 /// </summary> /// <param name="n">表格序号</param> /// <param name="use">是否使用</param> public void UseBorder(int n, bool use) { //允许有边框,默认没有边框(为0时报错, //1为实线边框,2、3为虚线边框,以后的数字没有试过) if (use) { wordDoc.Content.Tables[n].Borders.Enable = 1; } else { wordDoc.Content.Tables[n].Borders.Enable = 2; } }