C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法
更新時(shí)間:2015年09月18日 12:12:03 作者:我心依舊
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法,涉及C#針對(duì)word文檔的讀取、插入、保存等技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法。分享給大家供大家參考。具體如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Demo
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string path = @"C:\Documents and Settings\Administrator\桌面\output.doc";
string add = @"C:\Documents and Settings\Administrator\桌面\file";
private Microsoft.Office.Interop.Word.ApplicationClass applicationClass;
private Microsoft.Office.Interop.Word.Document doc;
private void button1_Click(object sender, EventArgs e)
{
Ex();
}
void Ex()
{
Open(path);
string[] files = System.IO.Directory.GetFiles(add);
foreach (string s in files)
{
InsertFile(s);
}
SaveAs(path);
}
/// <summary>
/// 打開輸出word文檔
/// </summary>
/// <param name="strFileName"></param>
public void Open(string strFileName)
{
applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
doc = applicationClass.Documents.Open(ref fileName, 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);
doc.Activate();
}
/// <summary>
/// 向打開的word文檔中插入word文檔
/// </summary>
/// <param name="strFileName"></param>
public void InsertFile(string strFileName)
{
object missing = System.Reflection.Missing.Value;
object confirmConversion = false;
object link = false;
object attachment = false;
applicationClass.Selection.InsertFile(strFileName, ref missing, ref confirmConversion, ref link, ref attachment);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
applicationClass.Selection.InsertBreak(ref pBreak);
}
/// <summary>
/// 最后保存word文檔
/// </summary>
/// <param name="strFileName"></param>
public void SaveAs(string strFileName)
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
doc.SaveAs(ref fileName, 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);
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
winform壁紙工具為圖片添加當(dāng)前月的日歷信息
使用用winform做了一個(gè)設(shè)置壁紙小工具,為圖片添加當(dāng)月的日歷并設(shè)為壁紙,可以手動(dòng)/定時(shí)設(shè)置壁紙,最主要的特點(diǎn)是在圖片上生成當(dāng)前月的日歷信息,感興趣的你可以參考下2013-03-03
詳解如何通過C#/VB.NET調(diào)整PDF文檔頁邊距
PDF邊距是頁面主要內(nèi)容區(qū)域和頁面邊緣之間的距離。與Word頁邊距不同,PDF文檔的頁邊距很難更改。本文將介紹如何在不更改頁面大小的情況下使用C#/VB.NET?代碼調(diào)整PDF文檔的頁邊距,需要的可以參考一下2023-04-04
Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
C#實(shí)現(xiàn)可捕獲幾乎所有鍵盤鼠標(biāo)事件的鉤子類完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)可捕獲幾乎所有鍵盤鼠標(biāo)事件的鉤子類,以完整實(shí)例形式分析了C#捕獲鍵盤鼠標(biāo)事件的鉤子操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

