Red de conocimiento informático - Material del sitio web - ¿Cómo leer contenido de Word usando C#?

¿Cómo leer contenido de Word usando C#?

Requisito previo:

Importar la biblioteca COM: Biblioteca de objetos de Microsoft Word 11.0.

Se agrega la referencia:

Crear un nuevo Word

objeto oMissing = System.Reflection.Missing.Value;

Word._Application oWord;

Word._Document oDoc;

oWord = new Word.Application();

oWord.Visible = true;

oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,

ref oMissing , ref oMissing);

Abrir documento:

objeto oMissing = System.Reflection.Missing.Value;

Word._Application oWord;

Word._Document oDoc;

oWord = new Word.Application();

oWord.Visible = true;

objeto fileName = @"E : CCCXCXXTestDoc.doc";

oDoc = oWord.Documents.Open(ref fileName,

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);

Importar plantilla

objeto oMissing = System.Reflection.Missing.Value;

Word._Application oWord;

Word._Document oDoc;

oWord = new Word.Application();

oWord.Visible = true;

objeto fileName = @"E:XXXCCXTest.doc";

oDoc = oWord .Documentos.Agregar(ref nombre de archivo, ref oMissing,

ref oMissing, ref oMissing);

.Agregar nueva tabla

objeto oMissing = System.Reflection.Missing.Value;

Word._Application oWord; p>

Word._Document oDoc;

oWord = new Word.Application();

oWord.Visible = true;

oDoc = oWord.Documentos.Add(ref oMissing, ref oMissing,

ref oMissing, ref oMissing);

inicio del objeto = 0;

fin del objeto = 0;

p>

Word.Range tableLocation = oDoc.Range(ref start, ref end);

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing) ;

.Fila de inserción de tabla

objeto oMissing = System.Reflection.Missing.Value;

Word._Application oWord;

Word ._Documento oDoc;

oWord = new Word.Application();

oWord.Visible = true;

oDoc = oWord.Documents.Add(ref oMissing , ref oMissing,

ref oMissing, ref oMissing);

inicio del objeto = 0;

fin del objeto = 0;

Palabra .Range tableLocation = oDoc.Range(ref start, ref end);

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word. Tabla newTable = oDoc .Tables[1];

objeto beforeRow = newTable.Rows[1];

newTable.Rows.Add(ref beforeRow);

.cell Merge

objeto oMissing = System.Reflecti

on.Missing.Value;

Word._Application oWord;

Word._Document oDoc;

oWord = new Word.Application();

oWord.Visible = true;

oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,

ref oMissing, ref oMissing);

inicio del objeto = 0;

fin del objeto = 0;

Word.Range tableLocation = oDoc.Range(inicio de referencia, fin de referencia);

oDoc. Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word.Table newTable = oDoc.Tables[1];

objeto beforeRow = newTable.Rows[ 1];

newTable.Rows.Add(ref beforeRow);

Word.Cell celda = newTable.Cell(1, 1);

celda. Merge(newTable.Cell(1, 2));

.Separación de celdas

objeto oMissing = System.Reflection.Missing.Value;

Word. oWord;

Word._Document oDoc;

oWord = new Word.Application();

oWord.Visible = true;

oDoc = oWord.Documents.Add(ref oMissing,

ref oMissing, ref oMissing);

inicio del objeto = 0;

fin del objeto = 0; /p>

Word.Range tableLocation = oDoc.Range(ref start, ref end);

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word.Table newTable = oDoc.Tables[1];

objeto beforeRow

= newTable.Rows[1];

newTable.Rows.Add(ref beforeRow);

Word.Cell celda = newTable.Cell(1, 1);

cell.Merge(newTable.Cell(1, 2));

objeto Rownum = 2;

objeto Columnnum = 2;

celda. Split(ref Rownum, ref Columnnum);

Inserción mediante control de párrafo

objeto oMissing = System.Reflection.Missing.Value;

objeto oEndOfDoc = "\ endofdoc"; /**//* endofdoc es un marcador predefinido */

//Inicia Word y crea un nuevo documento.

Word._Application oWord;

Word._Document oDoc;

oWord = new Word.Application();

oWord.Visible = true;

oDoc = oWord.Documents.Add (ref oMissing, ref oMissing,

ref oMissing, ref oMissing);

//Inserta un párrafo al principio del documento.

Word.Paragraph oPara1;

oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);

oPara1.Range.Text = "Encabezado 1";

oPara1. Range.Font.Bold = 1;

oPara1.Format.SpaceAfter = 24; //Espaciado de 24 pt después del párrafo.

oPara1.Range.InsertParagraphAfter();