C# 通过word模板动态生成Word

C# 通过word模板动态生成Word

 
复制代码
                    object oMissing = System.Reflection.Missing.Value;
                    Word._Application oWord = new Word.Application();
                    oWord.Visible = false;
                    object oTemplate = HttpContext.Current.Server.MapPath("~/files/userword/Temp/LendMoneyDemo.dot");
                    Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
                    object[] oBookMark = new object[10];
                    oBookMark[0] = "username";
                    oBookMark[1] = "useridcard";
                    oBookMark[2] = "totalmoney";
                    oBookMark[3] = "totalday";
                    oBookMark[4] = "startday";
                    oBookMark[5] = "endday";
                    oBookMark[6] = "fuwufee";
                    oBookMark[7] = "httongname";
                    oBookMark[8] = "httongdatea";
                    oBookMark[9] = "hetongdateb";

                   

                    oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = UserInfo.realName;
                    oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = UserInfo.idCard;
                    oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = total.ToString(); ;
                    oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = totalday.ToString();
                    oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = DateTime.Now.ToString("yyyy年MM月dd日");
                    oDoc.Bookmarks.get_Item(ref oBookMark[5]).Range.Text = enddate;
                    oDoc.Bookmarks.get_Item(ref oBookMark[6]).Range.Text = (total / 10).ToString();
                    oDoc.Bookmarks.get_Item(ref oBookMark[7]).Range.Text = UserInfo.realName;
                    oDoc.Bookmarks.get_Item(ref oBookMark[8]).Range.Text = DateTime.Now.ToString("yyyy年MM月dd日");
                    oDoc.Bookmarks.get_Item(ref oBookMark[9]).Range.Text = DateTime.Now.ToString("yyyy年MM月dd日");

                    object filename = HttpContext.Current.Server.MapPath("~/files/userword/Word/" + tokenId + "_LendMoney.docx");

                    oDoc.SaveAs(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);

                    oDoc.Close(ref oMissing, ref oMissing, ref oMissing);

                    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
复制代码

 

posted on 2022-06-05 15:58  漫思  阅读(177)  评论(0编辑  收藏  举报

导航