asp.net export word,pdf instance code by the specified template-Practical tips

Source: Internet
Author: User

Copy Code code as follows:

<summary>
Export Word files
</summary>
<param name= "templatefile" > Template path </param>
<param name= "Filenameword" > Export file name </param>
<param name= "filenamepdf" >pdf file name </param>
<param name= "bookmarks" > Template Bookmarks Collection </param>
<param name= "Invoiceline" > Invoice Entry List </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;//Get book signature
Range range = Doc. Bookmarks.get_item (ref BookmarkName). range;//Table Insertion Position
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);
}

Call

Copy Code code as follows:

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);


Create a new word, insert a bookmark where you want to replace it, use the above method to replace the bookmark with the specified content, and save as a PDF

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.