When the content of asp.net is imported into the word documents and word templates, the first example is to directly import the content of asp.net to the word template file. The second example is to import the content to the word documents.
Asp tutorial. net content is imported into word documents and word templates, the first example is a complete import of the content of the asp.net tutorial directly into the word template file, the second is to import the content into the word documents.
Using system;
Using system. web. security;
Using microsoft. office. interop. word;
Using system. io;
/// <Summary>
/// Word summary
/// </Summary>
Public class wordop
{
Public wordop ()
{
//
// Todo: add the constructor logic here
//
}
Private applicationclass wordapp;
Private document worddoc;
Private static bool isopened = false; // determines whether the word template is 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 );
}
// 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;
}
// Create a word file based on the template
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.doc uments. add (ref otemplate, 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 the word template
// Opentempelte (tempname); // fill in the content of the labelid label, that is, the mail subject
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. bKjia. c0m /";
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"); // do a jump to download.
Let's take a look at asp.net to import data to word.
After office2003 is installed, an activex control is installed in the system, which is located in "program filesmicrosoft officeoffice11owssupp. dll ". Through this control, webpage effects on the client page can activate the local office software to open and edit office documents. (In addition, office xp should already contain this activex Control .)
First, use a script to create a local object:
Opendocobj = new activexobject ("sharepoint. opendocuments.2"); // to be compatible with office xp, you can create "sharepoint. opendocuments.1"
Then, call the corresponding method of opendocobj. For example, open an office document on the server:
Opendocobj. viewdocument ("http://www.bKjia. c0m/documents/sample.doc ");
The opendocobjobject uses different officedocument (.doc).xls0000.ppt) to open different programs (word, excel, and powerpoint ). The viewdocument () method also has an overload signature, which allows us to manually specify which program to activate to open the document:
Opendocobj. viewdocument ("http://www.bKjia. c0m/documents/sample.doc", progid of the program to be activated );
How do I open the office program and edit files online?
Opendocobj. editdocument ("http://www.bKjia. c0m/documents/sample.doc ");
You can directly activate word, edit the document in word, and then click Save in word to save the file to the server. Note: In order for word to save the edited document directly to the server, windows identity accessing the current context of the web site must be in the appropriate directory of the server (that is, the http://www.bKjia. c0m/documents "indicates that the physical path on the server corresponding to the virtual directory has the write permission. Otherwise, the SAVE operation fails. After editing, editdocument () returns a bool value to indicate whether the editing operation is successful.
We can also open a document template on the server to create a new document:
Opendocobj. createnewdocument ("http://www.bKjia. c0m/documents/sampletemplate. dot", "http://www.bKjia. c0m/documents /");
You can use the http://www.bKjia. c0m/documents/sampletemplate. dot template to create a new document, where the default new document is saved by http://www.bKjia. c0m/documents /". The program used to create a new document depends on the template file type (for example, the. dot template corresponds to the word ). You also need to pay attention to permissions when saving new documents. The createnewdocument () method returns a bool value to indicate whether the operation is successful.
The first parameter of the createnewdocument () method, in addition to using a template address, can also be directly specified as the progid of the client program to create a new document.
<Html>
<Head>
<Title>
Test_word
</Title>
<Script language = "javascript">
Function validator ()
{
Var opendocobj;
Opendocobj = new activexobject ("sharepoint. opendocuments.2 ");
Opendocobj. editdocument ("c: inetpubwwwroot .doc ");
}
</Script>
</Head>
<Body bgcolor = "# ffffff">
<H1>
<Input type = "button" name = "word" value = http://down.bKjia. c0m/qz. q/"word" onclick = "validator ()">
</H1>
</Body>
</Html>