Word.Application app = new Word.Application();
//模板檔案
string TemplateFile = @"D:\lin\Invoice.doc";
//產生的具有模板樣式的新檔案
string FileName = @"D:\lin\" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";
string Fname = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";
//FileName = Server.MapPath("~/UpLoad/TestWord/" + Fname );
//模板檔案拷貝到新檔案
File.Copy(TemplateFile, FileName);
Word.Document doc = new Word.Document();
object Obj_FileName = FileName;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
//開啟檔案
doc = app.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);
doc.Activate();
foreach (Word.Bookmark bm in doc.Bookmarks)
{
if (bm.Name == "BookMark_Date")
{
bm.Select();
bm.Range.Text = "2008";//ViewState["FK_ProdurcePlanID"].ToString();
}
// else if (bm.Name == "class")
// {
// bm.Select();
// bm.Range.Text = ViewState["_class"].ToString();
// }
}
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);
Response.Write("<script language='javascript'>alert('產生word成功!');</script>");