1、首先需要在word模板中插入书签
2、引用 Microsoft.Office.Interop.Word;
操作类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
namespace LawsRegulationDate.LawenforceManagement.DocumentFile
{
/// <summary>
/// Interaction logic for HomePage.xaml
/// </summary>
public partial class HomePage
{
public HomePage()
{
InitializeComponent();
}
private void 行政执法首页_Click(object sender, RoutedEventArgs e)
{
//模板文件
string urlstring = System.Windows.Forms.Application.StartupPath + "\\File\\WordTemplate\\模板.docx";
if (string.IsNullOrEmpty(urlstring) || !File.Exists(urlstring))
{
Message_Box.Show("模板不存在!");
return;
}
else
{
string fileName = string.Empty;
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
fbd.ShowDialog();
if (fbd.SelectedPath != string.Empty)
{
fileName = fbd.SelectedPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx";
WordEstablish(urlstring, fileName);
}
else
{
Message_Box.Show("选择导出位置");
return;
}
}
}
/// <summary>
/// 调用生产方法
/// </summary>
/// <param name="urlstring">模板文件</param>
/// <param name="fileName">新文件</param>
public void WordEstablish(string urlstring, string fileName)
{
try
{
#region 声明参数
Dictionary<string, string> DList = new Dictionary<string, string>();
DList.Add("标题", "标题");
DList.Add("日期", DateTime.Now.ToString());
DList.Add("内容", "内容");
#endregion
if (WordClass.ExportWord(urlstring, fileName, DList))
{
MessageBox.Show("生成'" + fileName + "'成功!");
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
return;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using System.Collections.Generic;
using System.IO;
namespace LawsRegulationDate
{
public class WordClass
{
/// <summary>
/// 模板生成word
/// </summary>
/// <param name="templateFile">模板文件</param>
/// <param name="fileName">新文件</param>
/// <param name="myDictionary">参数数组</param>
public static bool ExportWord(string templateFile, string fileName, Dictionary<string, string> myDictionary)
{
try
{
//生成word程序对象
Word.Application app = new Word.Application();
//模板文件
string TemplateFile = templateFile;
//模板文件拷贝到新文件
File.Copy(TemplateFile, fileName);
//生成documnet对象
Word._Document doc = new Word.Document();
object Obj_FileName = fileName;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
//打开文件
doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref Visible,
ref missing, ref missing, ref missing,
ref missing);
doc.Activate();
#region 声明参数
if (myDictionary.Count > 0)
{
object what = Word.WdGoToItem.wdGoToBookmark;
object WordMarkName;
foreach (var item in myDictionary)
{
WordMarkName = item.Key;
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref WordMarkName);//光标转到书签的位置
doc.ActiveWindow.Selection.TypeText(item.Value);//插入的内容,插入位置是word模板中书签定位的位置
doc.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置当前定位书签位置插入内容的格式
}
}
#endregion
//输出完毕后关闭doc对象
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);
return true;
}
catch
{
return false;
}
}
}
}