C#在word中插入表格,如何使表格在整页

假如我们需要在一个word文档的某个位置插入一张表格,而且要对这一张表格中的单元格做拆分合并,之类的。
先看下效果,先不管表格是否合理,总之就是要这样的统计文档,但是人数,班级数都是不确定的,也就是表格是根 据数据动态生成的,
这样我们就很难用之前的替换方式来实现,也就是要动态来创建表格。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 班级成绩统计单
1、用单元格拆分的方式实现,也就是根据有多少班级,则增加多少行,针对于其中的学生信息再对单元格进行拆分。
/// &summary&
/// 数据实体类
/// &/summary&
public class Student
public string N//姓名
public int S//成绩
public string StuC//班级
public string L//班主任
/// &summary&
/// 动态创建table到word
/// &/summary&
protected void CreateTableToExcel()
Word.Application app = null;
Word.Document doc = null;
//构造数据
List&Student& datas = new List&Student&();
datas.Add(new Student{ Leader="小李", Name="张三", Score=498, StuClass="一班"});
datas.Add(new Student{ Leader="陈飞", Name="李四", Score=354, StuClass="二班"});
datas.Add(new Student{ Leader="陈飞", Name="小红", Score=502, StuClass="二班"});
datas.Add(new Student{ Leader="王林", Name="丁爽", Score=566, StuClass="三班"});
var cate = datas.GroupBy(s=&s.StuClass);
int rows = cate.Count()+1;//表格行数加1是为了标题栏
int cols = 5;//表格列数
object oMissing = System.Reflection.Missing.V
app = new Word.Application();//创建word应用程序
doc = app.Documents.Add();//添加一个word文档
//输出大标题加粗加大字号水平居中
app.Selection.Font.Bold = 700;
app.Selection.Font.Size = 16;
app.Selection.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphC
app.Selection.Text = "班级成绩统计单";
//换行添加表格
object line = Word.WdUnits.wdL
app.Selection.MoveDown(ref line, oMissing, oMissing);
app.Selection.TypeParagraph();//换行
Word.Range range = app.Selection.R
Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);
//设置表格的字体大小粗细
table.Range.Font.Size = 10;
table.Range.Font.Bold=0;
//设置表格标题
int rowIndex = 1;
table.Cell(rowIndex, 1).Range.Text = "班级";
table.Cell(rowIndex, 2).Range.Text = "姓名";
table.Cell(rowIndex, 3).Range.Text = "成绩";
table.Cell(rowIndex, 4).Range.Text = "人数";
table.Cell(rowIndex, 5).Range.Text = "班主任";
//循环数据创建数据行
rowIndex++;
foreach (var i in cate)
table.Cell(rowIndex, 1).Range.Text = i.K//班级
table.Cell(rowIndex, 4).Range.Text = i.Count().ToString();//人数
table.Cell(rowIndex, 5).Range.Text = i.First().L//班主任
table.Cell(rowIndex,2).Split(i.Count(), 1);//分割名字单元格
table.Cell(rowIndex,3).Split(i.Count(), 1);//分割成绩单元格
//对表格中的班级、姓名,成绩单元格设置上下居中
table.Cell(rowIndex, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalC
table.Cell(rowIndex, 4).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalC
table.Cell(rowIndex, 5).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalC
//构建姓名,成绩数据
foreach (var x in i)
table.Cell(rowIndex, 2).Range.Text = x.N
table.Cell(rowIndex, 3).Range.Text = x.Score.ToString();
rowIndex++;
//导出到文件
string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
string physicNewFile = Server.MapPath(newFile);
doc.SaveAs(physicNewFile,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
catch(Exception ex)
if (doc != null)
doc.Close();//关闭文档
if (app != null)
app.Quit();//退出应用程序
&2、单元格合并的方式,即循环数据输出,然后对班级,人数,班主任列进行合并,但是合并的时候需要注意,假如某班级只有一个同学,也就是其实不需要合并的,
则必须排除在外,否则会出现&此命令无效&的异常,所以下面代码在合并的时候做了一个判断。
protected void CreateTableToExcel2()
Word.Application app = null;
Word.Document doc = null;
//构造数据
List&Student& datas = new List&Student&();
datas.Add(new Student { Leader = "小李", Name = "张三", Score = 498, StuClass = "一班" });
datas.Add(new Student { Leader = "陈飞", Name = "李四", Score = 354, StuClass = "二班" });
datas.Add(new Student { Leader = "陈飞", Name = "小红", Score = 502, StuClass = "二班" });
datas.Add(new Student { Leader = "王林", Name = "丁爽", Score = 566, StuClass = "三班" });
var cate = datas.GroupBy(s =& s.StuClass);
int rows = datas.Count + 1;
int cols = 5;//表格列数
object oMissing = System.Reflection.Missing.V
app = new Word.Application();//创建word应用程序
doc = app.Documents.Add();//添加一个word文档
//输出大标题加粗加大字号水平居中
app.Selection.Font.Bold = 700;
app.Selection.Font.Size = 16;
app.Selection.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphC
app.Selection.Text = "班级成绩统计单";
//换行添加表格
object line = Word.WdUnits.wdL
app.Selection.MoveDown(ref line, oMissing, oMissing);
app.Selection.TypeParagraph();//换行
Word.Range range = app.Selection.R
Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);
//设置表格的字体大小粗细
table.Range.Font.Size = 10;
table.Range.Font.Bold = 0;
//设置表格标题
int rowIndex = 1;
table.Cell(rowIndex, 1).Range.Text = "班级";
table.Cell(rowIndex, 2).Range.Text = "姓名";
table.Cell(rowIndex, 3).Range.Text = "成绩";
table.Cell(rowIndex, 4).Range.Text = "人数";
table.Cell(rowIndex, 5).Range.Text = "班主任";
//循环数据创建数据行
rowIndex++;
foreach (var i in cate)
int moveCount = i.Count() - 1;//纵向合并行数
if (moveCount.ToString() != "0")
table.Cell(rowIndex, 1).Merge(table.Cell(rowIndex + moveCount, 1));//合并班级
table.Cell(rowIndex, 4).Merge(table.Cell(rowIndex + moveCount, 4));//合并人数
table.Cell(rowIndex, 5).Merge(table.Cell(rowIndex + moveCount, 5));//合并班主任
//写入合并的数据并垂直居中
table.Cell(rowIndex, 1).Range.Text = i.K
table.Cell(rowIndex, 4).Range.Text = i.Count().ToString();
table.Cell(rowIndex, 5).Range.Text = i.First().L
table.Cell(rowIndex, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalC
table.Cell(rowIndex, 4).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalC
table.Cell(rowIndex, 5).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalC
//构建姓名,成绩数据
foreach (var x in i)
table.Cell(rowIndex, 2).Range.Text = x.N
table.Cell(rowIndex, 3).Range.Text = x.Score.ToString();
rowIndex++;
//导出到文件
string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
string physicNewFile = Server.MapPath(newFile);
doc.SaveAs(physicNewFile,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
catch (Exception ex)
if (doc != null)
doc.Close();//关闭文档
if (app != null)
app.Quit();//退出应用程序
阅读(...) 评论()C#操作Word的超详细总结
时间: 12:43:16
&&&& 阅读:4198
&&&& 评论:
&&&& 收藏:0
标签:本文中用C#来操作Word,包括:
创建Word;
插入文字,选择文字,编辑文字的字号、粗细、颜色、下划线等;
设置段落的首行缩进、行距;
设置页面页边距和纸张大小;
设置页眉、页码;
插入图片,设置图片宽高以及给图片添加标题;
插入表格,格式化表格,往表格中插入数据;
保存Word,打印Word;
重新打开Word等。
Visual studio版本:Visual Studio 应该也可以)
准备工作:
/* 1. 添加引用COM里面的 Microsoft Word 12.0 Object. Library 引用(12.0表示Word 2007版本)
2. 导命名空间
using MSWord =Microsoft.Office.Interop.W
using System.IO;
using System.R
3. 把引用中的Microsoft.Office.Interop.Word的&属性&中的嵌入互操作设为False */
以下是全部代码:
2 using System.Collections.G
3 using System.L
4 using System.Runtime.InteropS
5 using System.T
6 using MSWord = Microsoft.Office.Interop.W
7 using System.IO;
8 using System.R
10 namespace Console_WordSkill_All
class Program
static void Main(string[] args)
//文件路径变量
string strC
//文本内容变量
MSWord.Application wordA
//Word应用程序变量
MSWord.Document wordD
//Word文档变量
path = Environment.CurrentDirectory + "\\MyWord_Print.doc";
wordApp = new MSWord.ApplicationClass(); //初始化
wordApp.Visible = true;//使文档可见
//如果已存在,则删除
if (File.Exists((string)path))
File.Delete((string)path);
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object Nothing = Missing.V
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
#region 页面设置、页眉图片和文字设置,最后跳出页眉设置
//页面设置
wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸
wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientP//排列方式为垂直方向
wordDoc.PageSetup.TopMargin = 57.0f;
wordDoc.PageSetup.BottomMargin = 57.0f;
wordDoc.PageSetup.LeftMargin = 57.0f;
wordDoc.PageSetup.RightMargin = 57.0f;
wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置
//设置页眉
wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdNormalV//普通视图(即页面视图)样式
wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekPrimaryH//进入页眉设置,其中页眉边距在页面设置中已完成
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphR//页眉中的文字右对齐
//插入页眉图片(测试结果图片未插入成功)
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphC
string headerfile = @"C:\Users\xiahui\Desktop\OficeProgram\3.jpg";
MSWord.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing);
shape1.Height = 5;//强行设置貌似无效,图片没有按设置的缩放&&图片的比例并没有改变。
shape1.Width = 20;
wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("
文档页眉");//在页眉的图片后面追加几个字
//去掉页眉的横线
wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleN
wordApp.ActiveWindow.ActivePane.Selection.Borders[MSWord.WdBorderType.wdBorderBottom].Visible = false;
wordApp.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainD//退出页眉设置
#endregion
#region 页码设置并添加页码
//为当前页添加页码
MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageN//获取当前页的号码
pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInD//设置页码的风格,是Dash形还是圆形的
pns.HeadingLevelForChapter = 0;
pns.IncludeChapterNumber = false;
pns.RestartNumberingAtSection = false;
pns.StartingNumber = 0; //开始页页码?
object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberC//将号码设置在中间
object first = true;
wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);
#endregion
#region 行间距与缩进、文本字体、字号、加粗、斜体、颜色、下划线、下划线颜色设置
wordApp.Selection.ParagraphFormat.LineSpacing = 16f;//设置文档的行间距
wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
//写入普通文本
strContent = "我是普通文本\n";
wordDoc.Paragraphs.Last.Range.Text = strC
wordDoc.Paragraphs.Last.Range.Text = "我再加一行试试,这里不加‘\\n‘";
//直接添加段,不是覆盖( += )
wordDoc.Paragraphs.Last.Range.Text += "不会覆盖的,";
//添加在此段的文字后面,不是新段落
wordDoc.Paragraphs.Last.Range.InsertAfter("这是后面的内容\n");
//将文档的前4个字替换成"哥是替换文字",并将其颜色设为红色
object start = 0;
object end = 4;
MSWord.Range rang = wordDoc.Range(ref start, ref end);
rang.Font.Color = MSWord.WdColor.wdColorR
rang.Text = "哥是替换文字";
wordDoc.Range(ref start, ref end);
//写入黑体文本
object unite = MSWord.WdUnits.wdS
wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
strContent = "这是黑体文本\n";
wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
wordDoc.Paragraphs.Last.Range.Text = strC
//写入加粗文本
strContent = "这是粗体文本\n"; //
wordApp.Selection.EndKey(ref unite, ref Nothing);//这一句不加,有时候好像也不出问题,不过还是加了安全
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Text = strC
//写入15号字体文本
strContent = "我这个文本的字号是15号,而且是宋体\n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
wordDoc.Paragraphs.Last.Range.Text = strC
//写入斜体文本
strContent = "我是斜体字文本\n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
wordDoc.Paragraphs.Last.Range.Text = strC
//写入蓝色文本
strContent = "我是蓝色的文本\n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorB
wordDoc.Paragraphs.Last.Range.Text = strC
//写入下划线文本
strContent = "我是下划线文本\n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineT
wordDoc.Paragraphs.Last.Range.Text = strC
//写入红色下画线文本
strContent = "我是点线下划线,并且下划线是红色的\n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineDottedH
wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = MSWord.WdColor.wdColorR
wordDoc.Paragraphs.Last.Range.Text = strC
//取消下划线,并且将字号调整为12号
strContent = "我他妈不要下划线了,并且设置字号为12号,黑色不要斜体\n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Size = 12;
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineN
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorB
wordDoc.Paragraphs.Last.Range.Font.Italic = 0;
wordDoc.Paragraphs.Last.Range.Text = strC
#endregion
#region 插入图片、居中显示,设置图片的绝对尺寸和缩放尺寸,并给图片添加标题
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
//图片文件的路径
string filename = Environment.CurrentDirectory + "\\6.jpg";
//要向Word文档中插入图片的位置
Object range = wordDoc.Paragraphs.Last.R
//定义该插入的图片是否为外部链接
Object linkToFile = false;
//默认,这里貌似设置为bool类型更清晰一些
//定义要插入的图片是否随Word文档一起保存
Object saveWithDocument = true;
//使用InlineShapes.AddPicture方法(【即&嵌入型&】)插入图片
wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphC//居中显示图片
//设置图片宽高的绝对大小
//wordDoc.InlineShapes[1].Width = 200;
//wordDoc.InlineShapes[1].Height = 150;
//按比例缩放大小
wordDoc.InlineShapes[1].ScaleWidth = 20;//缩小到20% ?
wordDoc.InlineShapes[1].ScaleHeight = 20;
//在图下方居中添加图片标题
wordDoc.Content.InsertAfter("\n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphC
wordApp.Selection.Font.Size = 10;//字体大小
wordApp.Selection.TypeText("图1 测试图片\n");
#endregion
#region 添加表格、填充数据、设置表格行列宽高、合并单元格、添加表头斜线、给单元格添加图片
wordDoc.Content.InsertAfter("\n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphL
//object WdLine2 = MSWord.WdUnits.wdL//换一行;
//wordApp.Selection.MoveDown(ref WdLine2, 6, ref Nothing);//向下跨15行输入表格,这样表格就在文字下方了,不过这是非主流的方法
//设置表格的行数和列数
int tableRow = 6;
int tableColumn = 6;
//定义一个Word中的表格对象
MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = 1;//这个值可以设置得很大,例如5、13等等
//表格的索引是从1开始的。
wordDoc.Tables[1].Cell(1, 1).Range.Text = "列\n行";
for (int i = 1; i & tableR i++)
for (int j = 1; j & tableC j++)
if (i == 1)
table.Cell(i, j + 1).Range.Text = "Column " +//填充每列的标题
if (j == 1)
table.Cell(i + 1, j).Range.Text = "Row " + //填充每行的标题
table.Cell(i + 1, j + 1).Range.Text = i + "行 " + j + "列";
//填充表格的各个小格子
table.Rows.Add(ref Nothing);
table.Rows[tableRow + 1].Height = 35;//设置新增加的这行表格的高度
//向新添加的行的单元格中添加图片
string FileName = Environment.CurrentDirectory + "\\6.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = table.Cell(tableRow + 1, tableColumn).R//选中要添加图片的单元格
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
//由于是本文档的第2张图,所以这里是InlineShapes[2]
wordDoc.Application.ActiveDocument.InlineShapes[2].Width = 50;//图片宽度
wordDoc.Application.ActiveDocument.InlineShapes[2].Height = 35;//图片高度
// 将图片设置为四周环绕型
MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[2].ConvertToShape();
s.WrapFormat.Type = MSWord.WdWrapType.wdWrapS
//设置table样式
table.Rows.HeightRule = MSWord.WdRowHeightRule.wdRowHeightAtL//高度规则是:行高有最低值下限?
table.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));//
table.Range.Font.Size = 10.5F;
table.Range.Font.Bold = 0;
table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphC//表格文本居中
table.Range.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalB//文本垂直贴到底部
//设置table边框样式
table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleD//表格外框是双线
table.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleS//表格内框是单线
table.Rows[1].Range.Font.Bold = 1;//加粗
table.Rows[1].Range.Font.Size = 12F;
table.Cell(1, 1).Range.Font.Size = 10.5F;
wordApp.Selection.Cells.Height = 30;//所有单元格的高度
//除第一行外,其他行的行高都设置为20
for (int i = 2; i &= tableR i++)
table.Rows[i].Height = 20;
//将表格左上角的单元格里的文字(&行& 和 &列&)居右
table.Cell(1, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphR
//将表格左上角的单元格里面下面的&列&字移到左边,相比上一行就是将ParagraphFormat改成了Paragraphs[2].Format
table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphL
table.Columns[1].Width = 50;//将第 1列宽度设置为50
//将其他列的宽度都设置为75
for (int i = 2; i &= tableC i++)
table.Columns[i].Width = 75;
//添加表头斜线,并设置表头的样式
table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].Visible = true;
table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].Color = MSWord.WdColor.wdColorR
table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].LineWidth = MSWord.WdLineWidth.wdLineWidth150
//合并单元格
table.Cell(4, 4).Merge(table.Cell(4, 5));//横向合并
table.Cell(2, 3).Merge(table.Cell(4, 3));//纵向合并,合并(2,3),(3,3),(4,3)
#endregion
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
wordDoc.Content.InsertAfter("\n");
wordDoc.Content.InsertAfter("就写这么多,算了吧!");
//WdSaveFormat为Word 2003文档的保存格式
object format = MSWord.WdSaveFormat.wdFormatD// office 2007就是wdFormatDocumentDefault
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档对象
//看是不是要打印
//wordDoc.PrintOut();
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Console.WriteLine(path + " 创建完毕!");
Console.ReadKey();
//我还要打开这个文档玩玩
MSWord.Application app = new MSWord.Application();
MSWord.Document doc = null;
object unknow = Type.M
app.Visible = true;
string str = Environment.CurrentDirectory + "\\MyWord_Print.doc";
object file =
doc = app.Documents.Open(ref file,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
string temp = doc.Paragraphs[1].Range.Text.Trim();
Console.WriteLine("你他妈输出temp干嘛?");
catch (Exception ex)
Console.WriteLine(ex.Message);
wordDoc.Paragraphs.Last.Range.Text += "我真的不打算再写了,就写这么多吧";
Console.ReadKey();
生成编辑的Word内容如下图所示:
参考资料:
http://blog.csdn.net/ruby97/article/details/7406806
http://blog.csdn.net/yhrun/article/details/7674540
http://www.cnblogs.com/eye-like/p/4121219.html
http://www.cnblogs.com/knowledgesea/archive//3095376.html
http://www.cnblogs.com/shi2172843/p/5848116.html
https://msdn.microsoft.com/en-us/library/bb257531(v=office.12).aspx
http://wenku.baidu.com/view/80ec0a6c1eb91a37f1115cab.html?from=search标签:原文地址:http://www.cnblogs.com/xh6300/p/5915717.html
&&国之画&&&& &&&&chrome插件
版权所有 京ICP备号-2
迷上了代码!}

我要回帖

更多关于 怎样使word表格调均匀 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信