码迷,mamicode.com
首页 > Windows程序 > 详细

C# 操作word

时间:2015-07-13 15:51:42      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

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;

 

C# 操作word

标签:

原文地址:http://www.cnblogs.com/xiaoyaodijun/p/4642922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!