ASP tutorial. NET C # Word files operations
1, reference
You need to reference a COM library: Microsoft Word 11.0 the Object library. Different versions, there will be different version numbers.
such as version 2010 Office is the Microsoft Word 14.0 Object Library.
2, refer to the corresponding name space:
Using Microsoft.Office.Core;
Using Word = Microsoft.Office.Interop.Word;
3. Open an existing Word document
public void Opendocfile (string docname)
{
object omissing = System.Reflection.Missing.Value; A parameter that needs to be used frequently when programming
Word. ApplicationClass WordApp = null; This is a word program, under which you can open multiple documents at the same time, try not to open multiple word programs at the same time, or you will be wrong.
Word. Document doc = null; The first Word document that you need to open
Word. Document doc2 = null; Another Word document that needs to be opened
WordApp = new Word. ApplicationClass ();
WordApp. Visible = true; Whether the Word program you open is visible.
Object DocObject = DocName; Because of the object used in COM operations, there are some changes that need to be made
if (file.exists (DocName))//If the filename to be opened exists, use Doc to open the
{
Doc = WordApp. Documents.Add (ref DocObject, ref omissing, ref omissing, ref omissing);
Doc. Activate (); Set the current file as the active document
Paragraphscount = doc. Content.Paragraphs.Count; In this document, the number of paragraphs, which is the document, has several paragraphs.
}
else//If the filename does not exist, use DOC2 to open the
{
DOC2 = WordApp. Documents.Add (ref omissing, ref omissing, ref omissing, ref omissing);
Docfilename = DocName;
Doc2. Activate (); Set the current file as the active document
}
}
4, get a paragraph of specific text content
public string Paragraph (int index)
{
Word. Paragraph para;
Para = doc. Content.paragraphs[index]; This is a set corresponding to a paragraph
Return para. Range.Text;
}
5, Doc content with the help of the clipboard copy and paste
<summary>
Copy the contents of a section of doc to the Clipboard
</summary>
<param name= "index" > section serial number </param>
public void copyparagraph (int index)
{
Word. Paragraph para;
Para = doc. Content.paragraphs[index];
Para. Range.Copy ();
}
<summary>
Paste the contents of the Clipboard into the DOC2 document
</summary>
public void Pasteparagraph ()
{
if (doc2!= null)
{
Word. Paragraph para = doc2. CONTENT.PARAGRAPHS.ADD (ref omissing);
Try
{
Para. Range.Paste ();
Para. Range.insertparagraphbefore ();
}
catch (Exception e)
{
Throw e;
}
}
}
6. Close Word Programs and documents
<summary>
Close Word Program
</summary>
public void Closeword ()
{
if (WordApp!= null)
{
if (Doc!= null)
{
Word._document DOCC = doc as word._document;
Docc. Close (ref omissing, ref omissing, ref omissing);
}
if (doc2!= null)
{
Word._document DOCC = doc2 as word._document;
Docc. Close (ref omissing, ref omissing, ref omissing);
}
WordApp. Quit (ref omissing, ref omissing, ref omissing);
}
}
7, replace the contents of the document
<summary>
Replace content in a document
</summary>
<param name= "oldstring" > Original content </param>
<param name= "newstring" > Replaced content </param>
public void Replace (string oldstring, String newstring)
{
Doc2. Content.Find.Text = oldstring;
Object FindText, ReplaceWith, ReplaceAll;
FindText = oldstring;
replacewith = newstring;
ReplaceAll = Word. Wdreplace.wdreplaceall;
Doc2. Content.Find.Execute (ref FindText,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
Ref replacewith,
Ref ReplaceAll,
ref omissing,
ref omissing,
ref omissing,
ref omissing);
}
Above, if you want to replace the carriage return character with a space, you can use the
Replace ("^p", "");
On it, this is the same as the replacement function used in Word.
8, save the contents of the document
<summary>
Save a Word document (save DOC2 only)
</summary>
public void Savedocfile ()
{
if (doc2!= null)
{
if (!string. IsNullOrEmpty (Docfilename))
{
Object docname = docfilename;//name of the file to save, including path
Replace ("^p^p", "^p");
Doc2. SAVEAS2 (ref docname,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing,
ref omissing);
}
}
}
A lot of data from the Internet, found that many are repeated. There is much more knowledge, which is not mentioned at all. For later use convenience. Summarize all of this knowledge for Up-and use