1. 程式人生 > >C#操作Word總結(二)——設定文件格式並新增文字內容、超連結、新增圖片

C#操作Word總結(二)——設定文件格式並新增文字內容、超連結、新增圖片

      設定文件格式並新增內容

#region 設定文件格式並新增文字內容、超連結
/// <summary>
/// 設定文件格式並新增內容
/// </summary>
/// <param name="filePath">檔名</param>
/// <returns></returns>
public static bool AddContent(string filePath)
{
    try
    {
        Object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word._Application WordApp = new Application();
        WordApp.Visible = true;
        object filename = filePath;
        Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        //設定居左
        WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

        //設定文件的行間距
        WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
        //插入段落
        //WordApp.Selection.TypeParagraph();
        Microsoft.Office.Interop.Word.Paragraph para;
        para = WordDoc.Content.Paragraphs.Add(ref oMissing);
        //正常格式
        para.Range.Text = "This is paragraph 1";
        //para.Range.Font.Bold = 2;
        //para.Range.Font.Color = WdColor.wdColorRed;
        //para.Range.Font.Italic = 2;
        para.Range.InsertParagraphAfter();

        para.Range.Text = "This is paragraph 2";
        para.Range.InsertParagraphAfter();

        //插入Hyperlink
        Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection;
        mySelection.Start = 9999;
        mySelection.End = 9999;
        Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;

        Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
        object linkAddr = @"http://www.cnblogs.com/lantionzy";
        Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
            ref oMissing);
        WordApp.ActiveWindow.Selection.InsertAfter("\n");

        //落款
        WordDoc.Paragraphs.Last.Range.Text = "文件建立時間:" + DateTime.Now.ToString();
        WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

        //儲存
        WordDoc.Save();
        WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        return true;
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);
        return false;
    }
}
#endregion 設定文件格式並新增文字內容、超連結
      文中新增圖片
#region 文件中新增圖片
/// <summary>
/// 文件中新增圖片
/// </summary>
/// <param name="filePath">word檔名</param>
/// <param name="picPath">picture檔名</param>
/// <returns></returns>
public static bool AddPicture(string filePath, string picPath)
{
    try
    {
        Object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word._Application WordApp = new Application();
        WordApp.Visible = true;
        object filename = filePath;
        Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        //移動游標文件末尾
        object count = WordDoc.Paragraphs.Count;
        object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
        WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移動焦點
        WordApp.Selection.TypeParagraph();//插入段落

        object LinkToFile = false;
        object SaveWithDocument = true;
        object Anchor = WordDoc.Application.Selection.Range;
        WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);

        //儲存
        WordDoc.Save();
        WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        return true;
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);
        return false;
    }
}
#endregion 文件中新增圖片
                                                                                             ——部分資料整理自網路


相關推薦

C#操作Word總結——設定格式新增文字內容連結新增圖片

      設定文件格式並新增內容 #region 設定文件格式並新增文字內容、超連結 /// <summary> /// 設定文件格式並新增內容 /// </summary> /// <param name="filePath">檔名&

C# 操作word總結——建立新增頁首頁尾

      最近程式中經常使用到word的操作,我在網上查了一些資料,在這裡整理一下。       使用程式碼建立word文件: #region 新建Word文件 /// <summary> /// 動態生成Word文件並填充內容 /// </summa

C# 操作Excel公式——批量刪除Excel公式保留文字

在Excel表格中,公式很常用,在處理資料時給我們提供了極大的方便。我們可以通過建立公式來批量處理資料,同理,我們也可以通過批量刪除公式來保護資料來源或方便於我們對資料的二次操作。下面的方法將介紹如何通過C#程式設計來批量刪除Excel公式並保留值。   所需工具:Spire.XLS for .

libfastcommon總結中加載配置信息

not eve icon 配置文件信息 獲取 結構化 har div ilo 頭文件為ini_file_reader.h 主要接口  IniContext iniContext;//定義配置文件信息 iniLoadFromFile();//加載文件為結構化配置信息

C#常見問題總結

7月 組件 圖片 水晶報表 datetime 控制臺 col orm value 1、erp系統可以在具有固定ip的擁有多層服務器的局域網中使用嗎?如何使用解決方法:把ini.配置文件字符串中的服務器名改成服務器的,把debug文件夾拷到其他機器上就行,服務器上的服務器名是

C#機房重構-總結

一,迴圈方法 1.for迴圈 這個是我舉得例子,這個是迴圈輸出在datagridview中選中的一行元組, int rowindex = dataGridView1.NewRowIndex; for (int j = 0; j < dataGridView1.C

C++面向物件總結--友元函式

類的友元函式 類的友元函式是定義在類外部,但有權訪問類的私有成員(private)和保護成員(protected)成員。儘管友元函式的原型在類的定義中出現過,但是友元函式並不是成員函式。#includ

SQL操作知識總結

3-1萬用字元過濾--LIKESELECT prod_id, prod_nameFROM ProductsWHERE prod_name LIKE 'Fish%';  關鍵詞 "LIKE 'Fish%'",作用篩選以Fish開頭的Products表中,顯示prod_id和pr

VC++中文操作--- .iniCFile64

其中 文件操作 ray rom 數組 ini文件 情人節 set 清空   各種關於文件的操作在程序設計中是十分常見,如果能對其各種操作都了如指掌,就可以根據實際情況找到最佳的解決方案,從而在較短的時間內編寫出高效的代碼,因而熟練的掌握文件操作是十分重要的。本文將對Visu

Zookeeper--配置講解客戶端使用

臨時 結束 java .com per 客戶端使用 .cn 事務 客戶端 Java操作zookeeper 創建節點 創建子節點 CreateMode.EPHEMETAL臨時節點保證本次回話有效,本次會話結束臨時節點消失,可以實現分布式事務鎖

Spring Boot幹貨系列:配置解析

set test profile ava java prefix 標註 了解 pre Spring Boot:配置文件解析 前言 上一篇介紹了Spring Boot的入門,知道了Spring Boot使用“習慣優於配置”(項目中存在大量的配置,此外

MFC多中獲取檢視指標

多文件系列 MFC多文件(一):檢視中獲取文件指標 MFC多文件(二):文件中獲取檢視指標 MFC多文件(三):文件與檢視繫結     問題 文件與檢視繫結後,在文件中如何獲取檢視指標?   解決方法 1、NaviMarkDo

java學習筆記之webservice--WSDL及用myeclipse測試webservice

 >>接上篇 一、WSDL 定義:web services description language,用來描述web服務的xml格式的資訊。 標籤的解釋 1. <types>:定義了服務的namespace和關鍵資訊的型別(方法的引數型別和返回值的

C#基礎總結 —— C#開發工具 Visual Studio(IDE)

暫停 基本上 必備 img 包含 adl 裏的 方案 運行 一、Visual Studio   Visual Studio 是微軟公司的一個開發工具集,是C#開發必備利器。下面附上VS2013簡體中文社區版的下載地址:    鏈接:https://pan.baidu.com

Python的語言基礎總結之循環與字符串操作

整數 範圍 輸入 IV 字符串 spl 余額 upper ict 一、循環和分支   1.  條件分支語句   if  條件:     代碼塊   else:     代碼塊   2.  循環語句之while   思考1:求1+2+3+....+10的值    sum =

Photoshop操作總結

 互補色 兩種色光以適當地比例混合而能產生白色感覺時,則這兩種顏色就稱為"互為補色"。 色彩中的互補色:紅,  黃,  藍三原色,紅色與綠色互補,藍色與橙色互補,紫色與黃色互補。  示例 我在球體上做了互補色的練習,首先需要畫出立體球,畫法在第

C++面試總結

1.struct和class的區別 C++中的struct對C中的struct進行了擴充,它已經不再是一個包含不同資料型別的資料結構了。 struct與class 的區別是預設的訪問控制。 預設的繼承訪問許可權,struct是public的,class是private的。  &

Redis總結C#中如何使用redis(轉載)

應用場景 bsp rgs 安裝 plain read 輸出 day space 上一篇講述了安裝redis《Redis總結(一)Redis安裝》,同時也大致介紹了redis的優勢和應用場景。本篇著重講解.NET中如何使用redis和C#。   Redis官網提供了很多開

C++ STL開發溫習與總結: 2.C++儲存技術

 C++ STL開發溫習與總結(二):2.C++儲存技術       使用了多年C++,沒有系統的溫習總結過,所以準備溫習《C++STL程式設計師開發指南》,本系列篇章將會是溫習總結該書本概念和技術。       本節討論的C++儲存技術保局哦C++儲存型別,C++儲存

C開發lua模組 --- 操作table和呼叫lua函式

C語言沒有類似 lua_pushtable 和 lua_totable 的方法,這也很容易理解,因為C語言中沒有一種資料型別可以和lua中的table相對應的,那C函式怎麼操作table型別的引數或者返回table型別的結果呢,lua API中提供了一系列的操作table