java word模板占位符_word模板导出的几种方式:第一种:占位符替换模板导出(只适用于word中含有表格形式的)...

1.占位符替换模板导出(只适用于word中含有表格形式的):

///

/// 使用替换模板进行到处word文件

///

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)

{

tempFile = System.Web.HttpContext.Current.Server.MapPath(tempFile);

saveFile = System.Web.HttpContext.Current.Server.MapPath(saveFile);

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

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

}

///

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

///

/// 重复表格的数据

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

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

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

{

if (!File.Exists(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)

{

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;

for (int j = 1; j <= wDoc.Tables.Count; j++)

{

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

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[j], 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[j], dc);

}

}

return true;

}

catch (Exception ex)

{

DisposeWord();

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();

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)

{

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进程

}

}

demo:

#region 动态创建DataTable数据

DataTable tblDatas = new DataTable("Datas");

DataColumn dc = null;

//赋值给dc,是便于对每一个datacolumn的操作

dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));

dc.AutoIncrement = true;//自动增加

dc.AutoIncrementSeed = 1;//起始为1

dc.AutoIncrementStep = 1;//步长为1

dc.AllowDBNull = false;//

dc = tblDatas.Columns.Add("NO", Type.GetType("System.String"));//1

DataRow newRow;

newRow = tblDatas.NewRow();

newRow["NO"] = info.NO;房屋座落地址 2--info实体类

tblDatas.Rows.Add(newRow);

#endregion

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

Dictionary dic = new Dictionary();

dic.Add("$id$", "NO");//3

#endregion

string tempFile = "~/Doc/doc.doc";

string saveFile = "~/Doc/" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".doc";

WordUtility w = new WordUtility(tempFile, saveFile);

w.GenerateWord(tblDatas, dic, null);

原文:https://www.cnblogs.com/daizhipeng/p/10684137.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值