Method to import the page content into the word template under asp.net

  • 2020-05-10 17:59:37
  • OfStack

1. Create a new word template by looking for the word document that needs to be filled in with the program, and using a "bookmark "(insert --) where you need to type > Bookmarks - > Input id - > ok) can be saved after marking.
2, modify security on the word template to add everyone readable in case the file cannot be opened.
3. Add "reference" to the project and find "Microsoft Word 10.0 Object Library" or "Microsoft Word 11.0 Object Library".
4. Create a new class called WordOp.cs means the class that operates word.
It reads as follows:
 
using System; 
using System.Web.Security; 
using Microsoft.Office.Interop.Word; 
using System.IO; 
/// <summary> 
/// Word  Summary of  
/// </summary> 
public class WordOp 
{ 
public WordOp() 
{ 
// 
// TODO:  Add the constructor logic here  
// 
} 
private ApplicationClass WordApp ; 
private Document WordDoc; 
private static bool isOpened=false;// judge word Whether the 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, 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 1 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 based on template Word file  
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 the Word template  
// OpenTempelte(tempName); // right LabelId The tag fills the content Content, 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(); 
} 
} 

5. It can be called in the background of the page that needs to be used, for example:
 
string path = Server.MapPath("download"); 
string templatePath = path + "file://downloadczql.doc/"; 
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 for downloading . 

Related articles: