word 操作

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(11);//设置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(
94158255));
            WApp.ActiveDocument.Background.Fill.BackColor.RGB 
=
                System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(
25235250));
            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, 
00ref 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;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值