关于word2003-c#编程最后篇
这篇本来想写点dosframer的内容和word2003的一些格式,插入方面的内容,但总感到很凌乱.怕写不好
想来想去,要转gis方面了.以后不知道什么时候有时间写office方面的.
所以这里把关于word2003在c#下编程最后点内容献给大家,不足之处,日后增补.
今天德国把我不喜欢的c罗干了,开心.看德科的样子又伤心.支持荷兰的顶下!
1.关于dsoframer.
这是个不错的东东,可以把自己生成的文档用dsoframer打开打印编辑保存.
具体大家看看http://support.microsoft.com/kb/311765/zh-cn
里面有详细的例子.需要在项目中实现office或html编辑的朋友可以参考.网上资料说明很多.
会c++的朋友还可以扩展.这1点是我极力推荐的原因.
2.关于在word文档中插入图表的问题.
因为graph是个独立的东东,所以要添加引用.
using Microsoft.Office.Interop.Graph;
//插入图表
//strbookmark-要插入图表的书签名字
//dt-图表的数据
//ChartType-是图表类型
//public Microsoft.Office.Interop.Graph.XlChartType ChartType = Microsoft.Office.Interop.Graph.XlChartType.xl3DPie;
private void InsertGraphtoBookmark(string strbookmark,System.Data.DataTable dt,ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
int i, j;
object missing = System.Reflection.Missing.Value;
object oClassType = "MSGraph.Chart.8";
//获取书签对象,换句话,把光标移过来
object bookmark = strbookmark;
WApp.ActiveDocument.Bookmarks.get_Item(ref bookmark).Select();
//WApp.Selection.InlineShapes[0].OLEFormat.Open();
//初始化一张图表
Microsoft.Office.Interop.Graph.Chart wdChart =
(Microsoft.Office.Interop.Graph.Chart)WApp.Selection.InlineShapes.AddOLEObject(ref oClassType, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing).OLEFormat.Object;
//wrdChart.Application.Visible = false;
//改变图表格式
wdChart.ChartType =ChartType;
wdChart.Application.PlotBy = Microsoft.Office.Interop.Graph.XlRowCol.xlColumns;//根据Y轴来画图表
Microsoft.Office.Interop.Graph.Axis axis = (Microsoft.Office.Interop.Graph.Axis)wdChart.Axes(1, 1);//设置X轴的属性
wdChart.Application.DataSheet.Cells.Clear();//清空表格的初始数据
//填充图表数据,起始的行号和列号都是1
for (i = 0; i < dt.Columns.Count; i++)//初始化列名
{
wdChart.Application.DataSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
}
for (i = 0; i < dt.Rows.Count; i++)//填充数据
{
for (j = 0; j < dt.Columns.Count; j++)
{
wdChart.Application.DataSheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
}
}
//axis.MaximumScale = 1;//X轴最大刻度
//axis.MajorUnit = 0.1;
wdChart.Legend.Delete();
wdChart.Width = 500;
//wrdChart.Height = 666;
//oShape.Height = oWord.InchesToPoints(3.57f);
//更新图表并保存退出
wdChart.Application.Update();
wdChart.Application.Quit();
}
//strbookmark-要插入图表的书签名字
//dt-图表的数据
//ChartType-是图表类型
//public Microsoft.Office.Interop.Graph.XlChartType ChartType = Microsoft.Office.Interop.Graph.XlChartType.xl3DPie;
private void InsertGraphtoBookmark(string strbookmark,System.Data.DataTable dt,ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
int i, j;
object missing = System.Reflection.Missing.Value;
object oClassType = "MSGraph.Chart.8";
//获取书签对象,换句话,把光标移过来
object bookmark = strbookmark;
WApp.ActiveDocument.Bookmarks.get_Item(ref bookmark).Select();
//WApp.Selection.InlineShapes[0].OLEFormat.Open();
//初始化一张图表
Microsoft.Office.Interop.Graph.Chart wdChart =
(Microsoft.Office.Interop.Graph.Chart)WApp.Selection.InlineShapes.AddOLEObject(ref oClassType, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing).OLEFormat.Object;
//wrdChart.Application.Visible = false;
//改变图表格式
wdChart.ChartType =ChartType;
wdChart.Application.PlotBy = Microsoft.Office.Interop.Graph.XlRowCol.xlColumns;//根据Y轴来画图表
Microsoft.Office.Interop.Graph.Axis axis = (Microsoft.Office.Interop.Graph.Axis)wdChart.Axes(1, 1);//设置X轴的属性
wdChart.Application.DataSheet.Cells.Clear();//清空表格的初始数据
//填充图表数据,起始的行号和列号都是1
for (i = 0; i < dt.Columns.Count; i++)//初始化列名
{
wdChart.Application.DataSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
}
for (i = 0; i < dt.Rows.Count; i++)//填充数据
{
for (j = 0; j < dt.Columns.Count; j++)
{
wdChart.Application.DataSheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
}
}
//axis.MaximumScale = 1;//X轴最大刻度
//axis.MajorUnit = 0.1;
wdChart.Legend.Delete();
wdChart.Width = 500;
//wrdChart.Height = 666;
//oShape.Height = oWord.InchesToPoints(3.57f);
//更新图表并保存退出
wdChart.Application.Update();
wdChart.Application.Quit();
}
3.给文档添加背景
文档背景添加可以采用插入图片方式,把图片放到文字下面去.模拟的盖章就可以这样处理.
我这里是个整个文档加上默认背景.不过效果出不来,vb版本没问题,郁闷ing
//背景
private void FillBackground(ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
WApp.ActiveDocument.Background.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
WApp.ActiveDocument.Background.Fill.ForeColor.RGB =
System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(94, 158, 255));
WApp.ActiveDocument.Background.Fill.BackColor.RGB =
System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(25, 235, 250));
WApp.ActiveDocument.Background.Fill.Transparency = float.Parse("0.6");
WApp.ActiveDocument.Background.Fill.PresetGradient(
Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal,1,
Microsoft.Office.Core.MsoPresetGradientType.msoGradientDaybreak);
}
private void FillBackground(ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
WApp.ActiveDocument.Background.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
WApp.ActiveDocument.Background.Fill.ForeColor.RGB =
System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(94, 158, 255));
WApp.ActiveDocument.Background.Fill.BackColor.RGB =
System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(25, 235, 250));
WApp.ActiveDocument.Background.Fill.Transparency = float.Parse("0.6");
WApp.ActiveDocument.Background.Fill.PresetGradient(
Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal,1,
Microsoft.Office.Core.MsoPresetGradientType.msoGradientDaybreak);
}
4.水印.
当然插入艺术字也可以.不过2者不一样.水印是针对整个文档,每一页多有.
这样,把给用户的打印模板全+上水印,没授权水印一直存在.当然用户文档的等级也一目了然.
//水印
private void SetWatermark(ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
object missing = System.Reflection.Missing.Value;
WApp.ActiveDocument.Sections[1].Range.Select();
//这个不能少了,少了水印出不来了
WApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
//给文档全部+上水印
WApp.Selection.HeaderFooter.Shapes.AddTextEffect(
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect10,
"请勿带出", "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, ref missing).Select(ref missing);
//文档中加入艺术字
//WApp.ActiveDocument.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect10,
// "请勿带出", "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
// Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, ref missing).Select(ref missing);
WApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject1";
WApp.Selection.ShapeRange.TextEffect.NormalizedHeight = Microsoft.Office.Core.MsoTriState.msoFalse;
WApp.Selection.ShapeRange.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
WApp.Selection.ShapeRange.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
WApp.Selection.ShapeRange.Fill.Solid();
WApp.Selection.ShapeRange.Fill.ForeColor.RGB =
System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.ForestGreen);
WApp.Selection.ShapeRange.Fill.Transparency =float.Parse("0.5");
WApp.Selection.ShapeRange.Rotation = 315;
WApp.Selection.ShapeRange.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
WApp.Selection.ShapeRange.Height =WApp.CentimetersToPoints(float.Parse("4.13"));
WApp.Selection.ShapeRange.Width = WApp.CentimetersToPoints(float.Parse("16.52"));
WApp.Selection.ShapeRange.WrapFormat.AllowOverlap =-1;
WApp.Selection.ShapeRange.WrapFormat.Side =
(Microsoft.Office.Interop.Word.WdWrapSideType)Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
WApp.Selection.ShapeRange.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
WApp.Selection.ShapeRange.RelativeHorizontalPosition =(Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition)
Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
WApp.Selection.ShapeRange.RelativeVerticalPosition =
Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
WApp.Selection.ShapeRange.Left =(float)
Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
WApp.Selection.ShapeRange.Top =(float)
Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
WApp.ActiveWindow.ActivePane.View.SeekView =
Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
}
private void SetWatermark(ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
object missing = System.Reflection.Missing.Value;
WApp.ActiveDocument.Sections[1].Range.Select();
//这个不能少了,少了水印出不来了
WApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
//给文档全部+上水印
WApp.Selection.HeaderFooter.Shapes.AddTextEffect(
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect10,
"请勿带出", "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, ref missing).Select(ref missing);
//文档中加入艺术字
//WApp.ActiveDocument.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect10,
// "请勿带出", "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
// Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, ref missing).Select(ref missing);
WApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject1";
WApp.Selection.ShapeRange.TextEffect.NormalizedHeight = Microsoft.Office.Core.MsoTriState.msoFalse;
WApp.Selection.ShapeRange.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
WApp.Selection.ShapeRange.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
WApp.Selection.ShapeRange.Fill.Solid();
WApp.Selection.ShapeRange.Fill.ForeColor.RGB =
System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.ForestGreen);
WApp.Selection.ShapeRange.Fill.Transparency =float.Parse("0.5");
WApp.Selection.ShapeRange.Rotation = 315;
WApp.Selection.ShapeRange.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
WApp.Selection.ShapeRange.Height =WApp.CentimetersToPoints(float.Parse("4.13"));
WApp.Selection.ShapeRange.Width = WApp.CentimetersToPoints(float.Parse("16.52"));
WApp.Selection.ShapeRange.WrapFormat.AllowOverlap =-1;
WApp.Selection.ShapeRange.WrapFormat.Side =
(Microsoft.Office.Interop.Word.WdWrapSideType)Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
WApp.Selection.ShapeRange.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
WApp.Selection.ShapeRange.RelativeHorizontalPosition =(Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition)
Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
WApp.Selection.ShapeRange.RelativeVerticalPosition =
Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
WApp.Selection.ShapeRange.Left =(float)
Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
WApp.Selection.ShapeRange.Top =(float)
Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
WApp.ActiveWindow.ActivePane.View.SeekView =
Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
}
5.好了,写完了.其实还有好多东西要学习.比如修订,保护...
office的编程部分到这里结束,以后可能要写点EXT+ajax+json+c#方面和mapinfo方面内容.不过这2个进行中,搞完一并发上来.
祝大家心情愉快.