批量生成word十九报告全文内容word,部分内容不同

&&&&&&&&&&&&&&&&&&
posts - 143,comments - 40,trackbacks - 0
最近业务需要批量打印准考证信息
1、根据Table数据进行循环替换,每次替换的时候只替换Word中第一个Table的数据,
2、每次替换之后将Word中第一个Table数据进行复制,将复制Table和上次替换的Table合并为一个Table。由于替换后的Table中不存在占位符,只有复制的Table中存在占位符,所有每次循环根据占位符替换最新数据就可以达到批量生成Word的目的了
Word模板:
批量替换后的Word:
using System.Collections.G
using System.L
using System.W
using System.IO;
using Word = Microsoft.Office.Interop.W
using System.D
namespace WebApplication1
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.V
public WordUtility(string tempFile, string saveFile)
this.tempFile = Path.Combine(HttpContext.Current.Server.MapPath("Word"), @tempFile);
this.saveFile = Path.Combine(HttpContext.Current.Server.MapPath("Temp"), @saveFile);
/// &summary&
/// 模版包含头部信息和表格,表格重复使用
/// &/summary&
/// &param name="dt"&重复表格的数据&/param&
/// &param name="expPairColumn"&word中要替换的表达式和表格字段的对应关系&/param&
/// &param name="simpleExpPairValue"&简单的非重复型数据&/param&
public bool GenerateWord(DataTable dt, Dictionary&string, string& expPairColumn, Dictionary&string, string& simpleExpPairValue)
if (!File.Exists(tempFile.ToString()))
HttpContext.Current.Response.Write("&script&alert('" + string.Format("{0}模版文件不存在,请先设置模版文件。", tempFile.ToString()) + "');&/script&");
return false;
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)
HttpContext.Current.Response.Write("&script&alert('" + "生成失败" + ex.Message + "');&/script&");
return false;
/// &summary&
/// 单个替换 模版没有重复使用的表格
/// &/summary&
/// &param name="dc"&要替换的&/param&
public bool GenerateWord(Dictionary&string, string& dc)
return GenerateWord(null, null, dc);
/// &summary&
/// 替换文件
/// &/summary&
/// &param name="dt"&要更新的数据&/param&
/// &param name="expPairColumn"&当前要替换的数据字典&/param&
/// &returns&&/returns&
private bool GenerateTable(DataTable dt, Dictionary&string, string& expPairColumn)
int tableNums = dt.Rows.C
Word.Table tb = wDoc.Tables[1];
tb.Range.Copy();
Dictionary&string, object& dc = new Dictionary&string, object&();
for (int i = 0; i & tableN 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);
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();
HttpContext.Current.Response.Write("&script&alert('" + "生成模版里的表格失败。" + ex.Message + "');&/script&");
return false;
/// &summary&
/// 替换文件
/// &/summary&
/// &param name="table"&当前Word中表格中要替换的Table&/param&
/// &param name="dc"&要替换的数据字典&/param&
/// &returns&&/returns&
private bool ReplaceTableRang(Word.Table table, Dictionary&string, object& dc)
object replaceArea = Word.WdReplace.wdReplaceA
//替换Word中指定Table的字段信息
foreach (string item in dc.Keys)
object replaceKey =
object replaceValue = dc[item];
table.Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,
missing, ref missing, ref missing, ref missing, ref missing,
replaceValue, ref replaceArea, ref missing, ref missing, ref missing,
return true;
catch (Exception ex)
DisposeWord();
HttpContext.Current.Response.Write("&script&alert('" + string.Format("{0}模版中没有找到指定的要替换的表达式。{1}", tempFile, ex.Message) + "');&/script&");
return false;
/// &summary&
/// 替换不重复数据
/// 当前表格中的所有信息都替换
/// &/summary&
/// &param name="dc"&替换的数据字典&/param&
/// &returns&&/returns&
private bool ReplaceAllRang(Dictionary&string, string& dc)
object replaceArea = Word.WdReplace.wdReplaceA
//替换整个Word文档里面的字段信息
foreach (string item in dc.Keys)
object replaceKey =
object replaceValue = dc[item];
wApp.Selection.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,
missing, ref missing, ref missing, ref missing, ref missing,
replaceValue, ref replaceArea, ref missing, ref missing, ref missing,
return true;
catch (Exception ex)
HttpContext.Current.Response.Write("&script&alert('" + string.Format("{0}模版中没有找到指定的要替换的表达式。{1}", tempFile, ex.Message) + "');&/script&");
return false;
/// &summary&
/// 释放资源
/// &/summary&
private void DisposeWord()
object saveOption = Word.WdSaveOptions.wdSaveC
//释放资源并且保持Word
wDoc.Close(ref saveOption, ref missing, ref missing);
saveOption = Word.WdSaveOptions.wdDoNotSaveC
wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
//Word模板路径
string word_ModelName = "SmallList - 副本.doc";
//生成后的Word文件路径
string word_SaveName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff")+".doc";
//重复替换字典数据
//根据字典的Key字段,找到表格中的对应列,然后根据字典的value字段,找到Word中对应要替换的字段
Dictionary&string, string& dict = new Dictionary&string, string&();
dict.Add("Name", "Name");
dict.Add("Age", "Age");
dict.Add("ShenFenNumber", "ShenFenNumber");
dict.Add("ZhunKaoNumber", "ZhunKaoNumber");
dict.Add("Gender", "Gender");
//不重复数据字典
//Key为Word中需要替换的占位符
Value为替换后的内容
Dictionary&string, string& dictNo = new Dictionary&string, string&();
dictNo.Add("ExamName", "《计算机导论》");
dictNo.Add("ExamTime", "120分钟");
WordUtility createWord = new WordUtility(word_ModelName, word_SaveName);
createWord.GenerateWord(GetDT(), dict, dictNo);
&注:本文部分信息参考其他网络
阅读(...) 评论()有没有软件可以批量生成固定结构的word报告_百度知道
有没有软件可以批量生成固定结构的word报告
我有更好的答案
首先,点击“文件”---“另存为web页”。 在“保存选项”处把“保存整个工作簿”调整为“选择:工作表”。把默认文件名&page.htm &根据实际情况改成你所需要的名字,切记:在改名字时绝对不可以把后面的&.htm&去掉,前面的部分就成。然后,找到刚才保存好的工作表。注意:如果你安装的操作系统是windowsXP或windows2000,可以在文件上面直接点击鼠标右键,选择“打开方式”--“word”。如果你所使用的操作系统是windows98,则需要在工作表上按住shift键同时点击鼠标右键,之后再进行上述操作。最后,用word程序打开该文件后,直接点击“文件”--“另存为”,把文件名改成“工作表word版,把保存类型选成“word文档”,再点击“保存”即可。
采纳率:97%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。批量生成word报告,部分内容不同_百度知道
批量生成word报告,部分内容不同
批量生成word报告,部分内容不同需要补报告,相同内容,只有姓名,地点,时间等部分内容不同。姓名,地点,时间已经由excel统计好了。怎样可以自动批量生成word,最好还能根据时间保存成不同名字
我有更好的答案
可以用邮件合并的方式来完成。就是在word文档里需要替换的地方设置为域,在邮件合并时,数据从excel电子表格中提取,即可。具体操作可以在网上查一下邮件合并。有详细的教程。
采纳率:47%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。查看: 3093|回复: 2
批量根据EXCEL内容自动生成一个WORD表格报告
阅读权限10
在线时间 小时
因工作需要,要将每个人评估出来的数据做一份报告审批,但一人一个报告,人数太多了,工作量挺大的,求大神给个方法或者个小工具能够自动生成报告表格,感谢。
每个人的下面有份模板,麻烦参考下
22:32 上传
点击文件名下载附件
14.05 KB, 下载次数: 103
阅读权限95
在线时间 小时
& & & & & & & &
EXCEL数据写入WORD,给你一个框架,自己改一下:
Sub test()
Dim myword, thispath, mydoc, r&, i&, j&
Set myword = CreateObject(&word.application&)
thispath = ThisWorkbook.Path & &\&
r = Sheet1.[b65536].End(3).Row
For i = 2 To r
& & mydoc = thispath & &授课通知(& & Sheet1.Cells(i, 2) & &).doc&
& & FileCopy thispath & &授课通知(模板).doc&, mydoc
& & With myword
& && &&&.documents.Open mydoc
& && &&&.Visible = True
& && &&&For j = 1 To 5
& && && && &If .Selection.Find.Execute(&数据& & Format(j, &000&)) Then
& && && && && & .Selection.Text = Sheet1.Cells(i, j + 1)
& && && && && & .Selection.MoveRight Unit:=1, Count:=1
& && && && &End If
& && &&&Next j
& && &&&For j = 1 To 3
& && && && &.activedocument.tables(1).cell(2, j) = Sheet1.Cells(i, j + 6)
& && && && &.activedocument.tables(1).cell(4, j) = Sheet1.Cells(i, j + 9)
& && &&&Next j
& && &&&'--------------------------------------------
& && &&&.ActiveWindow.ActivePane.View.SeekView = 9
& && &&&If .Selection.Find.Execute(&数据006&) Then
& && && && &.Selection.Text = &中心小学&
& && &&&End If
& && &&&.ActiveWindow.ActivePane.View.SeekView = 10
& && &&&If .Selection.Find.Execute(&数据007&) Then
& && && && &.Selection.Text = &宁阳县&
& && &&&End If
& && &&&'------------------------------------------
& && &&&.documents.Close True
& & End With
myword.Quit
阅读权限10
在线时间 小时
EXCEL数据写入WORD,给你一个框架,自己改一下:
Sub test()
Dim myword, thispath, mydoc, r&, i&, j&
有点搞不懂,能不能打包成个EXCEL给我!谢谢
最新热点 /1
买书正当时!当当网ExcelHome畅销书每满200-100元,当当活动时间:即日起至6月2日,机会不容错过!
玩命加载中,请稍候
玩命加载中,请稍候
Powered by
本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任! & & 本站特聘法律顾问:徐怀玉律师 李志群律师

我要回帖

更多关于 批量生成word报告 的文章

 

随机推荐