Microsoft.Office.Interop.Word 创建word
转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746.html
功能总结或者完善。
一、添加页眉
using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Linq; System.Text; using Word = Microsoft.Office.Interop.Word; System.IO; System.Reflection; Microsoft.Office.Interop.Word; namespace WordCreateDLL { public class AddHeader { static void AddSimpleHeader(Application WordApp,string HeaderText) { //添加页眉 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText); WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdalignParagraphLeft;设置左对齐 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; } HeaderText,WdParagraphAlignment wdalign) { WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText); WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;设置字体颜色 WordApp.Selection.ParagraphFormat.Alignment = wdalign;string HeaderText,WdParagraphAlignment wdalign,WdColor fontcolor,1)">float fontsize) { WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText); WordApp.Selection.Font.Color =fontcolor;设置字体颜色 WordApp.Selection.Font.Size = fontsize;设置字体大小 WordApp.Selection.ParagraphFormat.Alignment = wdalign;设置对齐方式 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; } } }
System; System.Collections.Generic; System.ComponentModel; System.Data; System.Linq; System.Text; using Word = Microsoft.Office.Interop.Word; System.IO; System.Reflection; WordCreateDLL { public class AddHeader { static void AddSimpleHeader(Application WordApp,string HeaderText) { 添加页眉 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText); WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdalignParagraphLeft;设置左对齐 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; } void AddSimpleHeader(Application WordApp,1)"> HeaderText,WdParagraphAlignment wdalign) { WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText); 设置字体颜色 WordApp.Selection.ParagraphFormat.Alignment = wdalign; fontsize) { 添加页眉 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText); WordApp.Selection.Font.Color =fontcolor;设置字体颜色 WordApp.Selection.Font.Size = fontsize;设置字体大小 WordApp.Selection.ParagraphFormat.Alignment = wdalign;设置对齐方式 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; } } }
二、插入图片
Microsoft.Office.Interop.Word;
WordCreateDLL
{
AddPic
{
void AddSimplePic(Document WordDoc,1)">string FName,1)">float Width,1)">float Height,1)">object An,WdWrapType wdWrapType)
{
插入图片
string FileName = @FName; 图片所在路径
object LinkToFile = false ;
object SaveWithDocument = trueobject Anchor = An;
WordDoc.Application.ActiveDocument.Inlineshapes.AddPicture(FileName,1)">ref LinkToFile,1)">ref SaveWithDocument,1)">ref Anchor);
WordDoc.Application.ActiveDocument.Inlineshapes[1].Width = Width;图片宽度
WordDoc.Application.ActiveDocument.Inlineshapes[1].Height = Height;图片高度
将图片设置为四周环绕型
Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.Inlineshapes[1].ConvertToShape();
s.Wrapformat.Type = wdWrapType;
}
}
}
System;
class AddPic { AddSimplePic(Document
WordDoc,1)">string FName,1)">float Width,1)">float Height,1)"> An,WdWrapType
wdWrapType) { 插入图片 string FileName = @FName;图片所在路径 object
LinkToFile = false; object SaveWithDocument = true; object Anchor = An;
WordDoc.Application.ActiveDocument.Inlineshapes.AddPicture(FileName,1)">ref LinkToFile,1)">ref SaveWithDocument,1)"> Anchor);
WordDoc.Application.ActiveDocument.Inlineshapes[图片宽度
WordDoc.Application.ActiveDocument.Inlineshapes[1].Height =
Height;图片高度 将图片设置为四周环绕型 Microsoft.Office.Interop.Word.Shape s =
WordDoc.Application.ActiveDocument.Inlineshapes[].ConvertToShape();
s.Wrapformat.Type = wdWrapType; } }
}
三、插入表格
AddTable
{
void AddSimpleTable(Application WordApp,Document WordDoc,1)">int numrows,1)">int numcolumns,WdLinestyle outStyle,WdLinestyle intStyle)
{
Object nothing = System.Reflection.Missing.Value;
文档中创建表格
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range,numrows,numcolumns,1)">ref nothing,1)"> nothing);
设置表格样式
newTable.Borders.OutsideLinestyle = outStyle;
newTable.Borders.InsideLinestyle = intStyle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;
填充表格内容
newTable.Cell(1,1).Range.Text = "产品详细信息表" ;
newTable.Cell(1).Range.Bold = 2;设置单元格中字体为粗体
合并单元格
newTable.Cell(1).Merge(newTable.Cell(3));
WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;垂直居中
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdalignParagraphCenter;水平居中
2,1)">产品基本信息1).Range.Font.Color =WdColor.wdColorDarkBlue;设置单元格内字体颜色
));
WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;
3,1)">品牌名称:2).Range.Text = ;
纵向合并单元格
newTable.Cell(3).Select();选中一行
object moveUnit = WdUnits.wdLine;
object moveCount = 5;
object moveExtend = WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit,1)">ref moveCount,1)"> moveExtend);
WordApp.Selection.Cells.Merge();
插入图片
string FileName = @ C:\1.jpg" ; 图片所在路径
WordDoc.Application.Selection.Range;
float Width = 200f; 图片宽度
float Height = 200f; 图片高度
将图片设置为四周环绕型
WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
AddPic.AddSimplePic(WordDoc,FileName,Width,Height,Anchor,wdWrapType);
newTable.Cell(12,1)">产品特殊属性));
在表格中增加行
WordDoc.Content.Tables[1].Rows.Add( nothing);
}
}
}
class AddTable { AddSimpleTable(Application
WordApp,1)">int numrows,1)"> numcolumns,WdLinestyle
outStyle,WdLinestyle intStyle) { Object nothing =
System.Reflection.Missing.Value; 文档中创建表格
Microsoft.Office.Interop.Word.Table newTable =
WordDoc.Tables.Add(WordApp.Selection.Range,1)">
nothing,1)">ref nothing); 设置表格样式 newTable.Borders.OutsideLinestyle =
outStyle; newTable.Borders.InsideLinestyle = intStyle;
newTable.Columns[1].Width = 100f; newTable.Columns[ 220f;
newTable.Columns[3].Width = 105f; 填充表格内容 newTable.Cell(1,
"; newTable.Cell(1).Range.Bold =
设置单元格中字体为粗体 合并单元格 newTable.Cell(1,1).Merge(newTable.Cell(1,3));
WordApp.Selection.Cells.VerticalAlignment
=WdCellVerticalAlignment.wdCellAlignVerticalCenter;垂直居中
WordApp.Selection.ParagraphFormat.Alignment =
WdParagraphAlignment.wdalignParagraphCenter;水平居中 填充表格内容
newTable.Cell(2,设置单元格内字体颜色 合并单元格
newTable.Cell());
WordApp.Selection.Cells.VerticalAlignment
=WdCellVerticalAlignment.wdCellAlignVerticalCenter; "; 纵向合并单元格 newTable.Cell(3,3).Select();选中一行
object moveUnit = WdUnits.wdLine; object moveCount = 5;
moveExtend = WdMovementType.wdExtend; WordApp.Selection.MoveDown(
moveUnit,1)">ref moveCount,1)"> moveExtend);
WordApp.Selection.Cells.Merge(); 插入图片 string FileName =
@"";图片所在路径 object Anchor =
WordDoc.Application.Selection.Range; float Width = 200f;图片宽度 float
Height = 200f;将图片设置为四周环绕型 WdWrapType wdWrapType =
Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
AddPic.AddSimplePic(WordDoc,wdWrapType); newTable.Cell(;
newTable.Cell(3)); 在表格中增加行
WordDoc.Content.Tables[ nothing); } }
}
四、插入chart
Microsoft.Office.Interop.Word;
Microsoft.Office.Interop.Graph;
System.Windows.Forms;
System.Drawing;
WordCreateDLL
{
AddChart
{
void AddSimpleChart(Document WordDoc,Word.Application WordApp,Object oEndOfDoc,1)"> [,]data)
{
插入chart
object oMissing = System.Reflection.Missing.Value;
Word.Inlineshape oShape;
object oClasstype = MSGraph.Chart.8 ;
Word.Range wrdRng = WordDoc.Bookmarks.get_Item( oEndOfDoc).Range;
oShape = wrdRng.Inlineshapes.AddOLEObject(ref oClasstype,1)"> oMissing,1)">ref oMissing,1)"> oMissing);
Demonstrate use of late bound oChart and oChartApp objects to
manipulate the chart object with MSGraph.
oChart;
oChartApp;
oChart = oShape.OLEFormat.Object;
oChartApp = oChart.GetType().InvokeMember(Application",BindingFlags.GetProperty,1)">null,oChart,1)">null );
Change the chart type to Line.
object [] Parameters = new Object[];
Parameters[0] = 4; xlLine = 4
oChart.GetType().InvokeMember(ChartTypearameters);
Chart objChart = (Chart)oShape.OLEFormat.Object;
objChart.ChartType = XlChartType.xlColumnClustered;
绑定数据
DataSheet dataSheet;
dataSheet = objChart.Application.DataSheet;
int rownum=data.GetLength(0);
int columnnum=data.GetLength(for ( int i=1;i<=rownum;i++ )
for ( int j = 1; j <= columnnum; j++)
{
dataSheet.Cells[i,j] =data[i-];
}
objChart.Application.Update();
oChartApp.GetType().InvokeMember(Update );
oChartApp.GetType().InvokeMember(Quit设置大小
oShape.Width = WordApp.InchesToPoints(6.25f);
oShape.Height = WordApp.InchesToPoints(3.57f);
}
}
}
Microsoft.Office.Interop.Graph;
System.Windows.Forms;
System.Drawing;
WordCreateDLL
{
AddChart
{
void AddSimpleChart(Document WordDoc,1)"> [,]data)
{
object oMissing = System.Reflection.Missing.Value;
Word.Inlineshape oShape;
object oClasstype = ;
Word.Range wrdRng = WordDoc.Bookmarks.get_Item( oEndOfDoc).Range;
oShape = wrdRng.Inlineshapes.AddOLEObject(ref oClasstype,1)"> oMissing,1)">ref oMissing,1)"> oMissing);
oChart;
oChartApp;
oChart = oShape.OLEFormat.Object;
oChartApp = oChart.GetType().InvokeMember();
object[] Parameters = new Object[];
Parameters[arameters);
Chart objChart = (Chart)oShape.OLEFormat.Object;
objChart.ChartType = XlChartType.xlColumnClustered;
DataSheet dataSheet;
dataSheet = objChart.Application.DataSheet;
int rownum=data.GetLength(int columnnum=data.GetLength(for(int i= )
for (int j = )
{
dataSheet.Cells[i,1)">];
}
objChart.Application.Update();
oChartApp.GetType().InvokeMember();
oChartApp.GetType().InvokeMember();
oShape.Height = WordApp.InchesToPoints();
}
}
}
测试程序
private void button3_Click( sender,EventArgs e)
{
object oEndOfDoc = \\endofdoc" ; /* \endofdoc is a predefined bookmark */
Start Word and create a new document.
Word.Application oWord;
Word.Document odoc;
oWord = new Word.Application();
oWord.Visible = ;
odoc = oWord.Documents.Add( oMissing);
////添加页眉
String HeaderText = 石化盈科 AddHeader.AddSimpleHeader(oWord,HeaderText);
WdParagraphAlignment wdalign = WdParagraphAlignment.wdalignParagraphCenter;
WdColor fontcolor = WdColor.wdColorBlue;
float fontsize = 10AddHeader.AddSimpleHeader(oWord,HeaderText,wdalign);
AddHeader.AddSimpleHeader(oWord,wdalign,fontcolor,fontsize);
Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = odoc.Content.Paragraphs.Add( oMissing);
oPara1.Range.Text = heading 1 ;
oPara1.Range.Font.Bold = ;
oPara1.Format.SpaceAfter = 24; 24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
Insert a paragraph at the end of the document.
Word.Paragraph oPara2;
object oRng = odoc.Bookmarks.get_Item( oEndOfDoc).Range;
oPara2 = odoc.Content.Paragraphs.Add( oRng);
oPara2.Range.Text = heading 2 ;
oPara2.Format.SpaceAfter = 6;
oPara2.Range.InsertParagraphAfter();
插入文字
Word.Paragraph oPara3;
oRng = odoc.Bookmarks.get_Item( oEndOfDoc).Range;
oPara3 = odoc.Content.Paragraphs.Add( oRng);
oPara3.Range.Text = This is a sentence of normal text. Now here is a table: ;
oPara3.Range.Font.Bold = ;
oPara3.Format.SpaceAfter = 24;
oPara3.Range.InsertParagraphAfter();
string text = zhangruichao ;
WdColor textcolor = fontcolor;
float textsize = 12;
AddLine.AddSimpLine(odoc,oEndOfDoc,oRng,text,textcolor,textsize);
插入表格
WdLinestyle OutStyle = WdLinestyle.wdLinestyleThickThinLargeGap;
WdLinestyle InStyle = WdLinestyle.wdLinestyleSingle;
AddTable.AddSimpleTable(oWord,odoc,OutStyle,InStyle);
插入分页
Word.Range wrdRng = odoc.Bookmarks.get_Item( oEndOfDoc).Range;
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
object oPageBreak = Word.WdBreakType.wdPageBreak;
wrdRng.Collapse( oCollapseEnd);
wrdRng.InsertBreak( oPageBreak);
wrdRng.Collapse( oCollapseEnd);
wrdRng.InsertAfter(We're Now on page 2. Here's my chart: );
wrdRng.InsertParagraphAfter();
Insert a chart.
string [,] data= new string [4,1)">];
data[0,1)">1] = 第一月 ;
data[2] = 第二月3] = 第三月4] = 第四月 ;
data[东部5040 ;
data[西部607080 ;
data[3,6] = "0";
data[中部 ;
AddChart.AddSimpleChart(odoc,oWord,data);
wrdRng = odoc.Bookmarks.get_Item( oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();
wrdRng.InsertAfter(THE END. );
Close this form.
this .Close();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。