zoukankan      html  css  js  c++  java
  • c# 操作word

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Office.Interop.Word;
    using System.Reflection;
    
    
    namespace WordAndExcel.Word
    {
        public partial class makeWord : System.Web.UI.Page
        {
            Microsoft.Office.Interop.Word.Application appWord = null;
            Microsoft.Office.Interop.Word.Document docWord = null;
            Range range = null;
            object missing = null;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Document wordDoc = OpenWord(Server.MapPath("./file/law.doc"));
                MakeWord(wordDoc, Server.MapPath("./file/laws.doc"));
            }
            /// <summary>
            /// 打开Word
            /// </summary>
            /// <param name="str_Path">文件路径</param>
            /// <returns></returns>
            protected Document OpenWord(object str_Path)
            {
                missing = Missing.Value;
                appWord = new Microsoft.Office.Interop.Word.Application();
                appWord.Visible = false;
                docWord = appWord.Documents.Open(ref str_Path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                return docWord;
            }
            /// <summary>
            /// 操作Word
            /// </summary>
            /// <param name="docWord">文档对象</param>
            protected void MakeWord(Document docWord, object str_Path)
            {
                try
                {
                    object startPostion = (docWord.Characters.Count - 1) as object;
                    Range tableLocation = docWord.Range(ref startPostion, ref startPostion); ///文檔對象 從頭開始
                    ///
                    Microsoft.Office.Interop.Word.Table newTable = docWord.Tables.Add(tableLocation, 5, 2, ref missing, ref missing);
                    ///table 樣式
                    newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
                    newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
                    for (int i = 0; i < 5; i++)
                    {
                        for (int j = 0; j < 2; j++)
                        {
                            newTable.Rows[i + 1].Cells[j + 1].Width = (float)(255F);
                            newTable.Rows[i + 1].Cells[j + 1].Height = (float)(155F);
                            TableAddText(newTable.Rows[i + 1].Cells[j + 1].Range);
                        }
    
                    }
                }
                catch (Exception ee)
                {
                    Response.Write(ee.ToString());
                }
                finally
                {
    
                    object saveChange = true;
                    docWord.SaveAs(ref str_Path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                    docWord.Close(ref  saveChange, ref missing, ref missing);
                    appWord.Quit(ref saveChange, ref missing, ref missing);
                }
            }
    
            public void TableAddText(Range range)
            {
                range.Text = "John:(寄)";
                // appWord.Selection.Text = "童新:(寄)";
                // appWord.Selection.TypeParagraph();
                // appWord.Selection.Text = "童新:(收)";
                range.Text += "Jerry:(收)";
                object Anchor = range;
               
               // 插入圖片
                 string FileName = Server.MapPath("./file/law.jpg");//图片所在路径
                 object LinkToFile = false;
                 object SaveWithDocument = true;
                // object Anchor = docWord.Application.Selection.Range;
                docWord.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                docWord.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
                docWord.Application.ActiveDocument.InlineShapes[1].Height = 20f;//图片高度
                
                //插入Hyperlink
                Microsoft.Office.Interop.Word.Selection mySelection = appWord.ActiveWindow.Selection;
                mySelection.Start = 9999;
                mySelection.End = 9999;
                Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;
                Microsoft.Office.Interop.Word.Hyperlinks myLinks = docWord.Hyperlinks;
                object linkAddr = @"http://www.cnblogs.com/tx720/";
                Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,ref missing);
                appWord.ActiveWindow.Selection.InsertAfter("
    ");
    
    
                //添加舉行形狀 并且添加文字
               Shape s = range.Document.Shapes.AddShape(1, 90, 20, 70, 20, ref Anchor);
               s.TextFrame.TextRange.Text = "dds";
    
               
            }
        }
    }
    欢迎加入 QQ群:633205769
  • 相关阅读:
    Mysql有什么办法解决主备延迟的引起的过期读问题?
    jdk 8 交集 差集
    【立flag】_(:з」∠)_
    Linux基础01 虚拟机安装操作系统,网络类型(网口),网段, 分区,分配网络, KDUMP,网络配置, 快照, 克隆, Xshell优化, Linux历史
    oracle(enquences & latches )lock (oracle 锁大全)
    oracle 密码详解以及破解
    oracle 大表在线删除列操作(alter table table_name set unused )
    MacOs 使用 github 个人令牌(Personal access token)
    web网站在线安全扫描
    vue 实现快捷键录入功能
  • 原文地址:https://www.cnblogs.com/tx720/p/3427727.html
Copyright © 2011-2022 走看看