欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 按指定模板导出word,pdf实例代码

程序员文章站 2024-03-31 16:00:46
复制代码 代码如下:///         /// 导出word文件&n...

复制代码 代码如下:

/// <summary>
        /// 导出word文件
        /// </summary>
        /// <param name="templatefile">模板路径</param>
        /// <param name="filenameword">导出文件名称</param>
        /// <param name="filenamepdf">pdf文件名称</param>
        /// <param name="bookmarks">模板内书签集合</param>
        /// <param name="invoiceline">发票条目列表</param>
        public static void generateword(string templatefile, string filenameword, string filenamepdf, dictionary<string, string> bookmarks, list<invoicelineview> invoiceline)
        {
            microsoft.office.interop.word.application app = new microsoft.office.interop.word.application();
            file.copy(templatefile, filenameword, true);
            microsoft.office.interop.word.document doc = new microsoft.office.interop.word.document();
            object obj_filename = filenameword;
            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 (string bookmarkname in bookmarks.keys)
            {

                object bookmarkname = bookmarkname;//获得书签名                   
                range range = doc.bookmarks.get_item(ref bookmarkname).range;//表格插入位置
                range.text = bookmarks[bookmarkname];
            }        
          object issave = true;
            object filename = filenamepdf;
            object fileformat = microsoft.office.interop.word.wdsaveformat.wdformatpdf;
            object lockcomments = false;
            object addtorecentfiles = true;
            object readonlyrecommended = false;
            object embedtruetypefonts = false;
            object savenativepictureformat = true;
            object saveformsdata = false;
            object saveasaoceletter = false;
            object encoding = microsoft.office.core.msoencoding.msoencodingsimplifiedchinesegb18030;
            object insertlinebreaks = false;
            object allowsubstitutions = false;
            object lineending = microsoft.office.interop.word.wdlineendingtype.wdcrlf;
            object addbidimarks = false;
            doc.saveas(ref filename, ref fileformat, ref lockcomments,
                    ref missing, ref addtorecentfiles, ref missing,
                    ref readonlyrecommended, ref embedtruetypefonts,
                    ref savenativepictureformat, ref saveformsdata,
                    ref saveasaoceletter, ref encoding, ref insertlinebreaks,
                    ref allowsubstitutions, ref lineending, ref addbidimarks);
            doc.close(ref issave, ref missing, ref missing);
        }

调用

复制代码 代码如下:

dictionary<string, string> bookmarks = new dictionary<string, string>();
bookmarks.add("contractduedatetime", invoice.invoicetime.adddays(invoice.contractduedate).tostring("d"));
bookmarks.add("customcontactemail", invoice.customcontactemail);
bookmarks.add("customcontactname", invoice.customcontactname);
bookmarks.add("contractduedate", invoice.contractduedate.tostring());
bookmarks.add("customcontacttel", invoice.customcontacttel);
bookmarks.add("customaddress", invoice.customaddress);
bookmarks.add("invoicetime", invoice.invoicetime.tostring());
bookmarks.add("invoiceid", invoice.invoiceid);
bookmarks.add("customname", invoice.customname);
bookmarks.add("customname2", invoice.customname);
bookmarks.add("total", invoice.totalprice.tostring("c"));
bookmarks.add("total1", invoice.totalprice.tostring("c"));
bookmarks.add("totaltax", invoice.totaltax.tostring("c"));
bookmarks.add("totalprice", (invoice.totalprice + invoice.totaltax).tostring("c"));
bookmarks.add("totalprice1", (invoice.totalprice + invoice.totaltax).tostring("c"));
bookmarks.add("totalprice2", (invoice.totalprice + invoice.totaltax).tostring("c"));
bookmarks.add("totalprice3", (invoice.totalprice + invoice.totaltax).tostring("c"));
bookmarks.add("totalprice4", (invoice.totalprice + invoice.totaltax).tostring("c"));
utility.generateword(templatefile, filenameword, filenamepdf, bookmarks, invoiceline);


新建一个word,在需要替换的位置插入书签,使用以上方法即可将书签处替换为指定内容,并且另存为pdf