用C#读取一个WORD模板,把里面的一些东西替换掉再生成一个新的WORD文档怎么做。谁那最好有源代码,急要谢

如题所述

第1个回答  2011-01-25
这是读取的:
public string ReadTemp(string tempPath)
{
string ModelTemp = HttpContext.Current.Server.MapPath(tempPath); // 读取模板文件

string str = "";
Encoding encoding = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
try
{
sr = new StreamReader(ModelTemp, encoding);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
finally
{
sr.Close();
}
return str;
}
这是生成的:
public bool WriteTemp(string str, string filePath)
{
string OutPutPath = "";
OutPutPath = HttpContext.Current.Server.MapPath(filePath); //html存放地址

Encoding encoding = Encoding.GetEncoding("gb2312");
StreamWriter sw = null;

try
{
if (!Directory.Exists(HttpContext.Current.Server.MapPath(filePath.Remove(filePath.LastIndexOf('/')))))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filePath.Remove(filePath.LastIndexOf('/'))));

sw = new StreamWriter(OutPutPath, false, encoding);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;

}
相似回答