c#word文档怎么替换文字
1个回答
展开全部
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ReplaceToExcel();
}
/// <summary>
/// 替换word中的文本,并导出word
/// </summary>
protected void ReplaceToExcel()
{
Word.Application app = null;
Word.Document doc = null;
//将要导出的新word文件名
string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
string physicNewFile = Server.MapPath(DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc");
try
{
app = new Word.Application();//创建word应用程序
object fileName = Server.MapPath("template.doc");//模板文件
//打开模板文件
object oMissing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//构造数据
Dictionary<string, string> datas = new Dictionary<string, string>();
datas.Add("{name}", "张三");
datas.Add("{sex}", "男");
datas.Add("{provinve}", "浙江");
datas.Add("{address}", "浙江省杭州市");
datas.Add("{education}", "本科");
datas.Add("{telephone}", "12345678");
datas.Add("{cardno}", "123456789012345678");
object replace = Word.WdReplace.wdReplaceAll;
foreach (var item in datas)
{
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.ClearFormatting();
app.Selection.Find.Text = item.Key;//需要被替换的文本
app.Selection.Find.Replacement.Text = item.value;//替换文本
//执行替换操作
app.Selection.Find.Execute(
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref replace,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
}
//对替换好的word模板另存为一个新的word文档
doc.SaveAs(physicNewFile,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
//准备导出word
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-word";
Response.End();
}
catch (System.Threading.ThreadAbortException ex)
{
//这边为了捕获Response.End引起的异常
}
catch (Exception ex)
{
}
finally
{
if (doc != null)
{
doc.Close();//关闭word文档
}
if (app != null)
{
app.Quit();//退出word应用程序
}
//如果文件存在则输出到客户端
if (File.Exists(physicNewFile))
{
Response.WriteFile(physicNewFile);
}
}
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
ReplaceToExcel();
}
/// <summary>
/// 替换word中的文本,并导出word
/// </summary>
protected void ReplaceToExcel()
{
Word.Application app = null;
Word.Document doc = null;
//将要导出的新word文件名
string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
string physicNewFile = Server.MapPath(DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc");
try
{
app = new Word.Application();//创建word应用程序
object fileName = Server.MapPath("template.doc");//模板文件
//打开模板文件
object oMissing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//构造数据
Dictionary<string, string> datas = new Dictionary<string, string>();
datas.Add("{name}", "张三");
datas.Add("{sex}", "男");
datas.Add("{provinve}", "浙江");
datas.Add("{address}", "浙江省杭州市");
datas.Add("{education}", "本科");
datas.Add("{telephone}", "12345678");
datas.Add("{cardno}", "123456789012345678");
object replace = Word.WdReplace.wdReplaceAll;
foreach (var item in datas)
{
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.ClearFormatting();
app.Selection.Find.Text = item.Key;//需要被替换的文本
app.Selection.Find.Replacement.Text = item.value;//替换文本
//执行替换操作
app.Selection.Find.Execute(
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref replace,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
}
//对替换好的word模板另存为一个新的word文档
doc.SaveAs(physicNewFile,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
//准备导出word
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-word";
Response.End();
}
catch (System.Threading.ThreadAbortException ex)
{
//这边为了捕获Response.End引起的异常
}
catch (Exception ex)
{
}
finally
{
if (doc != null)
{
doc.Close();//关闭word文档
}
if (app != null)
{
app.Quit();//退出word应用程序
}
//如果文件存在则输出到客户端
if (File.Exists(physicNewFile))
{
Response.WriteFile(physicNewFile);
}
}
}
}
博思aippt
2024-07-20 广告
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT...
点击进入详情页
本回答由博思aippt提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |