• 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
  • 相关阅读:
    @jackychua博客
    c#类与对象
    SQL SERVER 触发器
    .NET平台及C#面向对象编程
    数据库设计指南【转】
    HTTP 协议是一种请求/响应型的协议
    各种字符编码方式详解及由来(ANSI,GB2312,GBK,Big5,UNICODE,UTF8,UTF16)
    常用协议端口 POP3,IMAP,SMTP,Telnet,HTTP,HTTPS
    asp.net Request.Form Request.para Request.Querystring 区别
    Gzipstream 解压问题
  • 原文地址:https://www.cnblogs.com/tx720/p/3427727.html
Copyright © 2020-2023  润新知