Use the word template file. Dot to generate a Word document. Doc based on specific data.

Source: Internet
Author: User

1. Create the Word template file person. Dot
2. Create a web application and add Microsoft. Office. InterOP. Word reference.
3. Related sample code
======================================
1. Create the Word template file person. Dot
Use bookmarks to mark the filling position of related fields

2. Create a web application and add Microsoft. Office. InterOP. Word reference.
For more information, see
Http://www.microsoft.com/china/msdn/library/office/office/OfficePrIntopAssFAQ.mspx? MFR = true

3. Related sample code
The following code is adapted from
Http://blog.ccidnet.com/blog-htm-do-showone-uid-9416-type-blog-itemid-145551.html

Protected void button#click (Object sender, eventargs E)
{
Microsoft. Office. InterOP. Word. applicationclass appword = NULL; // Application
Microsoft. Office. InterOP. Word. documentclass Doc = NULL; // document
Try
{
Appword = new applicationclass ();
Appword. Visible = false;
Object objtrue = true;
Object objfalse = false;
Object objtemplate = server. mappath ("person. Dot"); // template path
Object objdoctype = wddocumenttype. wdtypedocument;
Doc = (documentclass) appword. Documents. Add (ref objtemplate, ref objfalse, ref objdoctype, ref objtrue );
// The first step is to generate a Word document
// Define the bookmarks
Object obdd_name = "bm_name"; // name of the last name
Object obdd_sex = "bm_sex"; // sex
Object obdd_birthday = "bm_birthday"; // Date of birth
// Read the data in step 2 and fill in the dataset
System. Data. datatable dt = new datatable ();
DT. Columns. Add ("p_name ");
DT. Columns. Add ("p_sex ");
DT. Columns. Add ("p_birthday ");
Datarow DR = DT. newrow ();
Dr ["p_name"] = "James ";
Dr ["p_sex"] = "male ";
Dr ["p_birthday"] = "1980-01-01 ";
DT. Rows. Add (DR );
// Assign a value to the bookmarks in step 3
// Assign a value to the bookmarks
Doc. bookmarks. get_item (ref obdd_name). range. Text = DT. Rows [0] ["p_name"]. tostring (); // name of the last name
Doc. bookmarks. get_item (ref obdd_sex). range. Text = DT. Rows [0] ["p_sex"]. tostring (); // sex
Doc. bookmarks. get_item (ref obdd_birthday). range. Text = DT. Rows [0] ["p_birthday"]. tostring (); // age
// Generate the word in Step 4
Object filename = server. mappath ("~ ") +" // "+ Dt. Rows [0] [" p_name "]. tostring () +". Doc ";
Object Miss = system. reflection. Missing. value;
Doc. saveas (ref filename, ref miss, ref miss, ref miss, ref Miss );
Object missingvalue = type. missing;
Object donotsavechanges = wdsaveoptions. wddonotsavechanges;
Doc. Close (ref donotsavechanges, ref missingvalue, ref missingvalue );
Appword. application. Quit (ref miss, ref miss, ref Miss );
Doc = NULL;
Appword = NULL;
}
Catch (system. Exception ex)
{
// Catch exceptions. If an exception occurs, clear the instance, exit word, and release resources.
String AA = ex. tostring ();
Object Miss = system. reflection. Missing. value;
Object missingvalue = type. missing;
Object donotsavechanges = wdsaveoptions. wddonotsavechanges;
Doc. Close (ref donotsavechanges, ref missingvalue, ref missingvalue );
Appword. application. Quit (ref miss, ref miss, ref Miss );
Doc = NULL;
Appword = NULL;
}
}

-----
If the above Code encounters a 80070005 error during running
 
Solution 1:
1. choose Control Panel> Administrative Tools> component service> Computer> my computer> DCOM configuration> Microsoft Word documents. Click properties to open the Properties dialog box of the application.
2. Click the ID tab and select an interactive user.
3. click the "Security" tab, select "Custom" in the "Start and activate Permissions" and "Access Permissions" groups, and then click "Custom"> "edit"> Add ASP. net account and iuser_computer name
4. Make sure that each user is allowed to access the service and click OK.
5. Click OK to disable dcomcnfg.

Solution 2:
If the above method cannot solve the problem, it should be a permission problem. Please try the following method:
Use identity simulation in Web. config and add <identity impersonate = "true" username = "your username" Password = "password"/> In the <system. Web> section.

For detailed solutions, see
Http://blog.csdn.net/lizhizhe2000/archive/2007/03/16/1531417.aspx

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.