主要就是首先制作一个模板,生成过程中主要进行替换:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static void prepareword_hz(string htid)
{
//贷款申请信息获得
HttpServerUtility _server = HttpContext.Current.Server;
string TemplateFile = _server.MapPath("~\\basedatas\\huizhi.doc");
string idname = htid + "hz.doc";
string fname = "~\\doctemp\\" + idname;
string FileName = _server.MapPath(fname);
object Obj_FileName = FileName;
//保存
File.Copy(TemplateFile, FileName, true);
object Visible = false;
object ReadOnly = false;
object Missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationClass wordapp = new Microsoft.Office.Interop.Word.ApplicationClass();//定义word应用程序
//打开文件
Microsoft.Office.Interop.Word._Document wd = wordapp.Documents.Open(ref Obj_FileName, ref Missing,
ref ReadOnly, ref Missing,
ref Missing, ref Missing,
ref Missing, ref Missing,
ref Missing, ref Missing,
ref Missing, ref Visible,
ref Missing, ref Missing,
ref Missing, ref Missing);
wd.Activate();
//定义替换类型为替换发现的第一个
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
wordapp.Selection.Find.ClearFormatting();
wordapp.Selection.Find.Replacement.ClearFormatting();
wordapp.Selection.Find.Wrap = WdFindWrap.wdFindContinue;
wordapp.Selection.Find.Forward = true;
BLLibrary.Bdk_hetong dkhetongdal = new Bdk_hetong();
Model.dk_hetongEntity dkhetong = new dk_hetongEntity();
dkhetong = dkhetongdal.Getdk_hetong(Convert.ToInt32(htid));
wordapp.Selection.Find.Text = "$sq_year";
wordapp.Selection.Find.Replacement.Text = System.DateTime.Today.Year.ToString();
wordapp.Selection.Find.Execute(ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref replaceAll, ref Missing, ref Missing, ref Missing, ref Missing);
//年度,"$sq_year"为在模板中设定的特定符号
wordapp.Selection.Find.Text = "$jk_name";
wordapp.Selection.Find.Replacement.Text = dkhetong.jk_name.ToString();
wordapp.Selection.Find.Execute(ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref replaceAll, ref Missing, ref Missing, ref Missing, ref Missing);
//学生姓名
wordapp.Selection.Find.Text = "$shi";
BLLibrary.Bcode_dw dwdal = new Bcode_dw();
Model.code_dwEntity dw = new code_dwEntity();
dw = dwdal.Getcode_dw(dkhetong.shi);
wordapp.Selection.Find.Replacement.Text = dw.dwname.ToString();
wordapp.Selection.Find.Execute(ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref replaceAll, ref Missing, ref Missing, ref Missing, ref Missing);
//市
wordapp.Selection.Find.Text = "$xian";
dw = dwdal.Getcode_dw(dkhetong.xian);
wordapp.Selection.Find.Replacement.Text = dw.dwname.ToString();
wordapp.Selection.Find.Execute(ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref replaceAll, ref Missing, ref Missing, ref Missing, ref Missing);
//县
// 保存word文档
wd.SaveAs(ref Obj_FileName, 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);
// 关闭,释放
wd.Close(ref Missing, ref Missing, ref Missing);
wordapp.Quit(ref Missing, ref Missing, ref Missing);
wd = null;
if (wordapp != null)
{
wordapp = null;
}
//wordapp = null;
//GC.Collect();
//下载文档
HttpContext.Current.Response.Redirect("~/doctemp/downword.aspx?id=" + idname);
}
下载:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace sydweb.doctemp
{
public partial class downword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string FullFileName = Request.QueryString["id"];
FileInfo DownloadFile = new FileInfo(HostingEnvironment.ApplicationPhysicalPath + "doctemp\\" + FullFileName); // 需要转换为绝对路径,否则会自动认到C盘系统里那个IIS目录下面去,而且,无法通过URI的方式来进行数据流读取。如果你生成的文件不在web目录下,也需要明确指出。
// 下面到就是读取文件,通过数据流的方式下载了。
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
}
}