zoukankan      html  css  js  c++  java
  • winfrom嵌入word


    使用微软的DSOFRAMER控件实现方法:
    先下载DsoFramer_KB311765_x86.exe ,自己百度一下有很多。
    安装,默认安装目录C:DsoFramer
    可以先注册下: 开始菜单----运行 输入 regsvr32.exe C:DsoFramerdsoframer.ocx

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    namespace WinFormWordDemo
    {

    public partial class Form1 : Form
    {
    Microsoft.Office.Interop.Word.Document doc = null;
    public Form1()
    {
    InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    axFramerControl1.Titlebar = false;//隐藏标题
    axFramerControl1.Toolbars = false;
    axFramerControl1.Menubar = false;
    }
    private void button1_Click(object sender, EventArgs e)
    {
    OpenFileDialog File = new OpenFileDialog();
    File.Multiselect = true;
    File.Title = "请选择文件";
    //File.Filter = "*.doc|*.docx";
    if (File.ShowDialog() == DialogResult.OK)
    {
    string filePath = File.FileName;
    this.axFramerControl1.Open(filePath);
    var CurrentDocument = this.axFramerControl1.ActiveDocument;
    if (CurrentDocument != null)
    {
    doc = (Microsoft.Office.Interop.Word.Document)CurrentDocument;
    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    object missing = System.Reflection.Missing.Value;
    //doc.Application.Selection.TypeText(pText);
    //doc.Content.Text = doc.Content.Text+ "牛叉叉,北京人";//普通文本写入;
    InsertText(doc,"小五测试福裕路", 30,50, Microsoft.Office.Interop.Word.WdColor.wdColorRed);

    }
    }
    }


    /// <summary>
    /// 设置字体样式以及方向 
    /// </summary>
    /// <param name="doc">文档</param>
    /// <param name="pText">写入的文本</param>
    /// <param name="pFontSize">字体颜色</param>
    /// <param name="pFontBold">字体加粗</param>
    /// <param name="pFontColor">字体颜色</param>
    public void InsertText(Microsoft.Office.Interop.Word.Document doc,string pText, int pFontSize, int pFontBold, Microsoft.Office.Interop.Word.WdColor pFontColor)
    {
    doc.Application.Selection.TypeParagraph();//换行
                doc.Application.Selection.Font.Size = pFontSize;
    doc.Application.Selection.Font.Bold = pFontBold;
    doc.Application.Selection.Font.Color = pFontColor;
    //doc.WdParagraphAlignment ptextAlignment
    //doc.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
    doc.Application.Selection.TypeText(pText);
    }
    private void button2_Click(object sender, EventArgs e)
    {
    //导入,导出,保存;字体格式,加粗,换行;
    axFramerControl1.Save();
    }

    /// <summary>
    /// 另存为
    /// </summary>
    /// <param name="doc"></param>
    /// <param name="pFileName"></param>
    public void SaveWord(string pFileName)
    {
    object myNothing = System.Reflection.Missing.Value;
    object myFileName = pFileName;
    object myWordFormatDocument = null; //doc.Words.w.WdSaveFormat.wdFormatDocument;
    object myLockd = false;
    object myPassword = "";
    object myAddto = true;
    try
    {
    doc.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
    ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
    ref myNothing, ref myNothing, ref myNothing);
    doc.Close(ref myNothing, ref myNothing, ref myNothing);
    //doc.Quit(ref myNothing, ref myNothing, ref myNothing);

    }
    catch(Exception ex)
    {


    string errorMsg = ex.Message;
    }
    }
    }
    }

  • 相关阅读:
    hihocoder 微软编程之美2015 初赛 第二场(暴力+字典序+图论+思维算法)
    hihocoder 微软编程之美2015 初赛 第一场 (树算法 + 暴力思想 + 搜索思想)
    山东省第四届ACM程序设计竞赛A题:Rescue The Princess(数学+计算几何)
    poj 2336 Ferry Loading II ( 【贪心】 )
    HDU 2037 今年暑假不AC ( 起始与终止时间 【贪心】)
    hdu 2015校赛1002 Dual horsetail (思维题 )
    poj 3041 Asteroids(二分图 *【矩阵实现】【最小点覆盖==最大匹配数】)
    poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
    【P1326】超级教主
    Tyvj 9.10 总结 (其实只是发一下心情)
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/14710577.html
Copyright © 2011-2022 走看看