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
Protected void CreateReport_Click (object sender, EventArgs e)
{
Microsoft. Office. Interop. Word. Application appWord = null; // Application
Microsoft. Office. Interop. Word. DocumentClass doc = null; // document
Try
{
AppWord = new Microsoft. Office. Interop. Word. Application ();
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
Object obpic = "pic ";
Object obtable = "obtable ";
Object Nothing = System. Reflection. Missing. Value;
// InlineShape shape = appWord. Selection. InlineShapes. AddPicture (@ "F: \ Picture \ _ DSC1602.JPG", ref Nothing );
// 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
Doc. Bookmarks. get_Item (ref obpic). Range. InlineShapes. AddPicture (@ "F: \ Picture \ _ DSC1602.JPG", ref Nothing );
// Insert a table into the document
// Doc. bookmarks. get_Item (ref obtable ). range. tables. add (doc. bookmarks. get_Item (ref obtable ). range, 12, 3, ref Nothing, ref Nothing );
Microsoft. Office. Interop. Word. Table newTable = doc. Tables. Add (doc. Bookmarks. get_Item (ref obtable). Range, 12, 3, ref Nothing, ref Nothing );
NewTable. Borders. OutsideLineStyle = WdLineStyle. wdLineStyleSingle;
NewTable. Borders. InsideLineStyle = WdLineStyle. wdLineStyleSingle;
// Add content to the last line of the document
Doc. Paragraphs. Last. Range. Text = "";
// Generate the word in Step 4
Object filename = Server. MapPath ("~ ") +" Http://www.cnblogs.com/chenbg2001/admin/file://bg// "+ 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:
Control Panel-> Administrative Tools-> component services-> Computer-> my computer-> DCom configuration-> Microsoft Word documents
After
Click properties to open the Properties dialog box for this 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
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 to the <system. web> section.
"Password =" password "/>
</System. web>
Reference: http://wenku.baidu.com/view/fc8aa56fb84ae45c3b358c98.html
Appendix: Detailed operations on Images
Object filename = @ "C: \ Inetpub \ wwwroot \ TestWebApp \ test.doc"; // file name
Word. Application a = new Word. ApplicationClass (); // create a Word Program
Object Nothing = System. Reflection. Missing. Value; // null
Word. document B =. documents. open (ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing); // create a Word Document Object
// In fact, this macro is executed in this step.
InlineShape shape = a. Selection. InlineShapes. AddPicture (@ "C: \ Documents ents and Settings \ Administrator \ Desktop \ 2003121512223104481.jpg", ref Nothing );
Shape. Height = InchesToPoints (0.5)
Shape. Width = InchesToPoints (0.5)
// Selection. inlineShapes. addPicture FileName: = "C: \ Documents ents and Settings \ Administrator \ Desktop \ 2003121512223104481.bmp", LinkToFile: = False, SaveWithDocument: = True End Sub B. save (); // Save
B. Close (ref Nothing, ref Nothing, ref Nothing); // Close the Word Document
A. Quit (ref Nothing, ref Nothing, ref Nothing); // exit the Word Program