C# 操作word


1.使用Aspose.Words 插件。這個插件的好處是,發布網站的服務不需要安裝office,也可以進行數據生成word文檔

在生成word問當前需要我們先做好一個word模板,需要在“:”后邊添加一個書簽 

注意:Aspose插件,有的版本不支持生成后綴為 .docx的模板,否則提示文檔損壞

 

                string tmppath = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "/DocTemp/createuser.doc");
                Document doc = new Document(tmppath); //載入模板      
                Bookmark name = doc.Range.Bookmarks["name"];
                name.Text = model.Name;
                Bookmark account = doc.Range.Bookmarks["account"];
                account.Text = model.Account;
                Bookmark password = doc.Range.Bookmarks["password"];
                password.Text = model.PassWord;
                Bookmark sex = doc.Range.Bookmarks["sex"];
                sex.Text = model.Sex;
                Bookmark height = doc.Range.Bookmarks["height"];
                height.Text = model.Height;

                Bookmark weight = doc.Range.Bookmarks["weight"];
                weight.Text = model.Weight;
                Bookmark birthday = doc.Range.Bookmarks["birthday"];
                birthday.Text = model.Birthday.ToString("yyyy-MM-dd");
                Bookmark phone = doc.Range.Bookmarks["phone"];
                phone.Text = model.Phone;
                Bookmark email = doc.Range.Bookmarks["email"];
                email.Text = model.Email;
                Bookmark qqnumber = doc.Range.Bookmarks["qqnumber"];
                qqnumber.Text = model.Email;

                Bookmark address = doc.Range.Bookmarks["address"];
                address.Text = model.Address;
                Bookmark presentaddress = doc.Range.Bookmarks["presentaddress"];
                presentaddress.Text = model.PresentAddress;
                Bookmark imgpath = doc.Range.Bookmarks["imgpath"];
                imgpath.Text = model.ImgPath;
                Bookmark hobby = doc.Range.Bookmarks["hobby"];
                hobby.Text = model.Hobby;
                Bookmark goodat = doc.Range.Bookmarks["goodat"];
                goodat.Text = model.GoodAt;

                var path = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "/UserWord/"+model.Name+".doc");
                //重新生成一個word文檔並保存
                doc.Save(path, SaveFormat.Doc);
                //定時刪除下載的文檔
                NeedHelp.time = new Timer(DeletWord, path, 50000, 300000);
                return path;

二.使用vs自帶的類庫  Miscrosoft Word 14.0 Object Library  ,它的缺點是,當你發布網站的服務沒有安裝office服務是,不可以生成word文檔

在生成word問當前需要我們先做好一個word模板,需要在“:”后邊添加一個書簽 

注意:它支持生成后綴為.docx的文件

 object oMissing = System.Reflection.Missing.Value;
                //創建一個Word應用程序實例  
                _Application oWord = new Application();
                //設置為不可見  
                oWord.Visible = false;
                //模板文件地址,這里假設在X盤根目錄  
                object oTemplate = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory + "/DocTemp/usertemp.docx");
                upload(oTemplate.ToString(), SignHelp.GetUserName());

                //以模板為基礎生成文檔  
                _Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
                //聲明書簽數組  
                object[] oBookMark = new object[15];
                //賦值書簽名  
                oBookMark[0] = "name";
                oBookMark[1] = "account";
                oBookMark[2] = "password";
                oBookMark[3] = "sex";

                oBookMark[4] = "height";
                oBookMark[5] = "weight";
                oBookMark[6] = "birthday";
                oBookMark[7] = "phone";

                oBookMark[8] = "email";
                oBookMark[9] = "qqnumber";
                oBookMark[10] = "address";
                oBookMark[11] = "presentaddress";

                oBookMark[12] = "imgpath";
                oBookMark[13] = "hobby";
                oBookMark[14] = "goodat";
                //賦值任意數據到書簽的位置  
                oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = model.Name;
                oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = model.Account;
                oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = model.PassWord;
                oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = model.Sex;

                oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = model.Height.ToString();
                oDoc.Bookmarks.get_Item(ref oBookMark[5]).Range.Text = model.Weight.ToString();
                oDoc.Bookmarks.get_Item(ref oBookMark[6]).Range.Text = model.Birthday.ToString("yyyy-MM-dd");
                oDoc.Bookmarks.get_Item(ref oBookMark[7]).Range.Text = model.Phone;

                oDoc.Bookmarks.get_Item(ref oBookMark[8]).Range.Text = model.Email;
                oDoc.Bookmarks.get_Item(ref oBookMark[9]).Range.Text = model.QqNumber;
                oDoc.Bookmarks.get_Item(ref oBookMark[10]).Range.Text = model.Address;
                oDoc.Bookmarks.get_Item(ref oBookMark[11]).Range.Text = model.PresentAddress;

                oDoc.Bookmarks.get_Item(ref oBookMark[12]).Range.Text = model.ImgPath;
                oDoc.Bookmarks.get_Item(ref oBookMark[13]).Range.Text = model.Hobby;
                oDoc.Bookmarks.get_Item(ref oBookMark[14]).Range.Text = model.GoodAt;


                //string savePath = Server.MapPath("~/UserWord/" + model.Name + ".docx");
                string savePath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory + "/UserWord/" + model.Name + ".docx");
                object filename = savePath;

                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);
                //關閉word  
                oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                //定時器
                NeedHelp.time = new Timer(DeletWord, model.Name, 50000, 300000);
                return savePath;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM