打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#根据Word模版生成Word文件

本文实例为大家分享了C#根据Word模版生成Word文的具体代码,供大家参考,具体内容如下

1、指定的word模版

2、生成word类

添加com Microsoft word 11.0 Object Library 引用

using System;

using System.Collections.Generic;

using System.Data;

using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;

using System.IO;

namespace Headfree.DefUI

{

public class WordUtility

{

private object tempFile = null;

private object saveFile = null;

private static Word._Document wDoc = null; //word文档

private static Word._Application wApp = null; //word进程

private object missing = System.Reflection.Missing.Value;

public WordUtility(string tempFile, string saveFile)

{

this.tempFile = Path.Combine(Application.StartupPath, @tempFile);

this.saveFile = Path.Combine(Application.StartupPath, @saveFile);

}

///

/// 模版包含头部信息和表格,表格重复使用

///

/// 重复表格的数据

/// word中要替换的表达式和表格字段的对应关系

/// 简单的非重复型数据

public bool GenerateWord(DataTable dt, Dictionary expPairColumn, Dictionary simpleExpPairValue)

{

if (!File.Exists(tempFile.ToString()))

{

MessageBox.Show(string.Format("{0}模版文件不存在,请先设置模版文件。", tempFile.ToString()));

return false;

}

try

{

wApp = new Word.Application();

wApp.Visible = false;

wDoc = wApp.Documents.Add(ref tempFile, ref missing, ref missing, ref missing);

wDoc.Activate();// 当前文档置前

bool isGenerate = false;

if (simpleExpPairValue != null && simpleExpPairValue.Count > 0)

isGenerate = ReplaceAllRang(simpleExpPairValue);

// 表格有重复

if (dt != null && dt.Rows.Count > 0 && expPairColumn != null && expPairColumn.Count > 0)

isGenerate = GenerateTable(dt, expPairColumn);

if (isGenerate)

wDoc.SaveAs(ref saveFile, 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);

DisposeWord();

return true;

}

catch (Exception ex)

{

MessageBox.Show("生成失败" + ex.Message);

return false;

}

}

///

/// 单个替换 模版没有重复使用的表格

///

/// 要替换的

public bool GenerateWord(Dictionary dc)

{

return GenerateWord(null, null, dc);

}

private bool GenerateTable(DataTable dt, Dictionary expPairColumn)

{

try

{

int tableNums = dt.Rows.Count;

Word.Table tb = wDoc.Tables[1];

tb.Range.Copy();

Dictionary dc = new Dictionary();

for (int i = 0; i < tableNums; i++)

{

dc.Clear();

if (i == 0)

{

foreach (string key in expPairColumn.Keys)

{

string column = expPairColumn[key];

object value = null;

value = dt.Rows[i][column];

dc.Add(key, value);

}

ReplaceTableRang(wDoc.Tables[1], dc);

continue;

}

wDoc.Paragraphs.Last.Range.Paste();

foreach (string key in expPairColumn.Keys)

{

string column = expPairColumn[key];

object value = null;

value = dt.Rows[i][column];

dc.Add(key, value);

}

ReplaceTableRang(wDoc.Tables[1], dc);

}

return true;

}

catch (Exception ex)

{

DisposeWord();

MessageBox.Show("生成模版里的表格失败。" + ex.Message);

return false;

}

}

private bool ReplaceTableRang(Word.Table table, Dictionary dc)

{

try

{

object replaceArea = Word.WdReplace.wdReplaceAll;

foreach (string item in dc.Keys)

{

object replaceKey = item;

object replaceValue = dc[item];

table.Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing,

ref missing);

}

return true;

}

catch (Exception ex)

{

DisposeWord();

MessageBox.Show(string.Format("{0}模版中没有找到指定的要替换的表达式。{1}", tempFile, ex.Message));

return false;

}

}

private bool ReplaceAllRang(Dictionary dc)

{

try

{

object replaceArea = Word.WdReplace.wdReplaceAll;

foreach (string item in dc.Keys)

{

object replaceKey = item;

object replaceValue = dc[item];

wApp.Selection.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing,

ref missing);

}

return true;

}

catch (Exception ex)

{

MessageBox.Show(string.Format("{0}模版中没有找到指定的要替换的表达式。{1}", tempFile, ex.Message));

return false;

}

}

private void DisposeWord()

{

object saveOption = Word.WdSaveOptions.wdSaveChanges;

wDoc.Close(ref saveOption, ref missing, ref missing);

saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程

}

}

}

3、效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
asp.net 填充word 模版标签并下载使用笔记
类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
C#实现 word、pdf、ppt 转为图片
webbrowser控件显示word文档
部分Office 2007文件格式转换为xps和pdf代码整理
简单几步提高PPt制作效率
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服