用C#读取一个WORD模板,把里面的一些东西替换掉再生成一个新的WORD文档怎么做。谁那最好有源代码,急要谢
展开全部
这是读取的:
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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询