Hong
From: http://blog.csdn.net/lshzfq/
The whole process is like this.
1.
First, you need to introduce the DLL of the word. If the word is installed, a msword9.olb file will appear under the installation directory (add reference) note that the above file may have different file names due to different office versions, and the parameters of the open and save methods below will also be different because of different versions, there are 16 open parameters in office2003, and there are about 12 parameters in 2000. Be sure to pay attention when calling them.
2.
Add <identity impersonate = "true"/> to the webconfig file to simulate the identity. If not, Program During the operation, an error is reported indicating that access is denied (and you need to prepare a Word template with bookmarks in advance)
3.
Create an interface and add using Word to the top of the interface;
The following is the specific function: (I have not sorted out the function here, and it may be useless)
Open the file:
Private word. Document opendoc (string strdocpath, ref word. Application wordapp, int flag)
{
If (! File. exists (strdocpath ))
Return NULL;
Object filename = (object) strdocpath;
Object isvisible = missing;
Object readonly = missing;
// Make word visible, so you can see what's happening
Wordapp. Visible = false;
// Create an instance of word. Document through open
Word. Document Doc = NULL;
Try
{
// Doc = wordapp. documents. open (ref filename, ref missing, ref readonly, ref missing, ref isvisible, ref missing, ref missing );
Doc = wordapp. documents. open (ref filename, ref missing, ref missing );
// If (flag = 1)
// Liststyle (DOC );
Return Doc;
}
Catch (exception ex)
{
Throw new exception (ex. Message );
Return NULL;
}
}
Replace template content:
String strwordtemplate = server. mappath ("../test/ 文 .doc "); // Here is your template file
Word. Application wordapp = new word. applicationclass (); // Define a word. Application Object
Word. Document Doc = opendoc (strwordtemplate, ref wordapp, 1 ); // Define a word. Document Object
Try
{
// The following is the data retrieved from the database. Sorry. Code A little bad
Datatable temptable = This. createtable ("select * From workflow_bw where appid =" + convert. toint32 (appid) + "and contentid =" + convert. toint32 (contentid ));
If (temptable. Rows. Count> 0)
{
String temptime = temptable. Rows [0] ["swtime"]. tostring (). Trim ();
Int Pos = temptime. indexof ("");
String all = temptime. substring (0, POS );
Int pre = all. indexof ("-");
Int next = all. lastindexof ("-");
String year = all. substring (0, pre). Trim ();
String month = all. substring (pre + 1, Next-Pre-1). Trim ();
String day = all. substring (next + 1, all. Length-next-1). Trim ();
Foreach (word. Bookmark BM in Doc. bookmarks) // This is the key: convenient matching of all bookmarks in the document
{
Switch (BM. Name)
{
Case "advice": // Replace the content of the advice bookmarks.
BM. Select ();
BM. range. TEXT = This. createtable ("select advice from workflow_advice where appid =" + convert. toint32 (appid) + "and contentid =" + convert. toint32 (this. contentid) + "and stepid = 1 "). rows [0] ["advice"]. tostring (). trim ();
Break;
Case "day ":
BM. Select ();
BM. range. Text = Day;
Break;
Case "lwdw ":
BM. Select ();
BM. range. Text = temptable. Rows [0] ["lwdw"]. tostring (). Trim ();
Break;
Case "lwh ":
BM. Select ();
BM. range. Text = temptable. Rows [0] ["SWH"]. tostring (). Trim ();
Break;
Case "month ":
BM. Select ();
BM. range. Text = month;
Break;
Case "nowyear ":
BM. Select ();
BM. range. Text = year;
Break;
Case "subject ":
BM. Select ();
BM. range. Text = temptable. Rows [0] ["subject"]. tostring (). Trim ();
Break;
Case "SWH ":
BM. Select ();
BM. range. TEXT = temptable. rows [0] ["lsh"]. tostring (). trim (). substring (4, temptable. rows [0] ["lsh"]. tostring (). trim (). length-4 );
Break;
}
}
}
Object fn = (object) server. mappath ("../test/temp.doc ");
Doc. saveas (ref FN, ref missing, ref missing ); // save as one file
Response. Redirect ("../test/temp.doc"); // open another file with IE, and then directly call the print function in IE.
// Doc. saveas (ref FN, ref missing, ref missing, ref missing, ref missing );
}
Catch (exception ERR)
{
This. tbxml. Text = err. tostring ();
}
Finally
{
Doc. Close (ref missing, ref missing, ref missing );
Wordapp. Quit (ref missing, ref missing, ref missing );
Wordapp = NULL;
}
}
One of the main problems is Doc. Close (ref missing, ref missing, ref missing );
Wordapp. Quit (ref missing, ref missing, ref missing );
Wordapp = NULL;
This code is not enough, and every time the printing is closed, it will be wrong, and winword.exe is not in the process. I don't know what's going on.
I am not very familiar with some objects in word, such as the application document Selection Range bookmark object. Please refer to the help on msdn for details.