asp.net content import Word documents with Word templates

Source: Internet
Author: User
Tags reflection

ASP tutorial. NET content import into Word documents and Word templates, the first instance is a complete import of the contents of the ASP.net tutorial directly into the Word template file, and the second is to import the content into the Word document.

using System;
Using System.Web.Security;
Using Microsoft.Office.Interop.Word;
Using System.IO;
<summary>
Word's summary description
</summary>
public class Wordop
{
Public Wordop ()
{
//
TODO: Add constructor logic here
//
}
Private ApplicationClass WordApp;
Private document WordDoc;
private static bool isopened=false;//to determine if Word templates are occupied
public void SaveAs (string Strfilename,bool isreplace)
{
if (Isreplace && file.exists (strFileName))
{
File.delete (strFileName);
}
Object missing = Type.Missing;
object filename = strFileName;
Worddoc.saveas (ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
Ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
Define a Word.Application Object
public void Activewordapp ()
{
WordApp = new ApplicationClass ();
}
public void Quit ()
{
Object missing = System.Reflection.Missing.Value;
Wordapp.application.quit (ref missing, ref missing, ref missing);
isopened = false;
}
New Word files based on templates
public void Opentempelte (string strtemppath)
{
Object missing = Type.Missing;
Object missing = System.Reflection.Missing.Value;
Activewordapp ();
Wordapp.visible = false;
Object otemplate = (object) Strtemppath;
Try
{
while (isopened)
{
System.threading.thread.sleep (500);
}
WordDoc = WordApp.Documents.Add (ref otemplate, ref missing, ref missing, ref missing);
Isopened = true;
Worddoc.activate ();
}
catch (Exception ex)
{
Quit ();
isopened = false;
throw new Exception (ex.message);
}
}
public void filllable (string labelid,string content)
{
Open Word Template
Opentempelte (Tempname); Fill in the contents of the Labelid label, that is, the subject item of the letter
Object BKMC = Labelid;
if (wordapp.activedocument.bookmarks.exists (labelid) = = True)
{
Wordapp.activedocument.bookmarks.get_item (ref BKMC). Select ();
}
Wordapp.selection.typetext (content);
SaveAs (Saveasfilename);
Quit ();
}
}

Call method

String path = Server.MapPath ("Download");
String TemplatePath = path + "file://www.111cn.net/";
Wordop wop = new Wordop ();
Wop.opentempelte (TemplatePath);
Wop.filllable ("id", "1");
Wop.filllable ("Usr_name", "Test");
Wop.saveas (path + "File://savetest.doc", true/);
Wop.quit ();
Response.Redirect (@ "/download/savetest.doc");//Make a jump for downloading.

Take a look at a asp.net import data into Word

After installing office2003, an ActiveX control is installed on the system, which is located in the program FilesMicrosoft Officeoffice11owssupp.dll. This control enables Web page effects on the client page to activate the local office software to open and edit Office documents. (another, Office XP should already contain this ActiveX control.) )

First, create a local object with script:

 

opendocobj = new ActiveXObject ("Sharepoint.opendocuments.2"); To be compatible with Office XP, you can create "Sharepoint.opendocuments.1"

Then, invoke the corresponding method of the opendocobj. For example, open an Office document on the server:

 

opendocobj.viewdocument ("Http://www.111cn.net/documents/sample.doc");

The Opendocobj object opens different programs (Word, Excel, PowerPoint) based on the different Office document types (. doc,. xls,. ppt) in the parameter. The ViewDocument () method also has an overloaded signature, which allows us to manually specify which program is activated to open the document:

  

opendocobj.viewdocument ("Http://www.111cn.net/documents/sample.doc", the ProgID of the program to be activated);

So what about opening Office programs to edit files online?

 

opendocobj.editdocument ("Http://www.111cn.net/documents/sample.doc");

You can directly activate Word, edit the document in Word, and then directly click on the Save function in Word, you can save the file to the server. Note: In order for Word to save the edited document directly to the server, Windows identity that accesses the current context of the Web site must have a corresponding directory for the server (that is, "http://www.111cn.net/documents" The physical path on the server for this virtual directory has the appropriate write permission, otherwise the Save action will fail. When the edit is complete, editdocument () returns a bool value to reflect the success of the edit operation.

We can also create a new document by opening a document template on the server:

 

opendocobj.createnewdocument ("Http://www.111cn.net/documents/sampletemplate.dot", "http://www.111cn.net/ Documents/");

You can use the Http://www.111cn.net/documents/sampletemplate.dot template to create a new document, where the default new document is saved as "http://www.111cn.net/documents/." The program that you use when you create a new document depends on the type of template file (for example, the. dot template corresponds to Word). The preservation of new documents also requires attention to permissions issues. The CreateNewDocument () method also returns a bool value to reflect the success of the operation.

The first parameter of the CreateNewDocument () method, in addition to the address of one template, can be directly specified as the ProgID of the client program that you want to use to create a new document.

 

<title>

Test_word

</titl E>

<script language= "javascript"

Function validator ()

{

var Opendo CObj;

Opendocobj = new ActiveXObject ("Sharepoint.opendocuments.2");

Opendocobj.editdocument ("C:inetpubwwwroot. Doc");

}

</script>

<body bgcolor= "#ffffff"

<H1&G t;

<input type= "button" name= "word" value= http://down.111cn.net/qz.q/"word" onclick= "validator ()"

p>

</body>

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.