zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    setTimeout,setInterval你不知道的…
    浏览器console的用法
    命题和命题的否定可以同时成立吗?
    Web云笔记--CSS
    dreamweaver代码提示失效
    游戏平台代表--PS4【推荐】
    Mac上好用的视频播放器有哪些?
    新浪博客“网络繁忙请稍后再试”
    人工智能成功识别“色情暴力”信息??…
    JS查错小工具-三生有幸【推荐】
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/4642922.html
Copyright © 2011-2022 走看看