Creating an act in word C#

Good time of day. You need to create an act in word based on the data available on winforms. Can you tell me how best to do this or where to start at all? The sheet will contain formatted text and a table with a different number of rows each time, but no more than 10. The approximate appearance of the act can be seen in the photo below. The places where it is covered up, just need to be replaced with their own. Well, as I wrote above, add rows to the table. In the original template, there is only a table header, without strings. enter a description of the image here

Author: Vadim Ovchinnikov, 2018-02-26

1 answers

Use the standard Microsoft.Office.Interop extension.Word and prepare a template from this document with bookmarks in the required places. Short code for working with the document:

Word._Document document;
Word._Application application=new Word.Application();
object missingObj = Missing.Value;
object templatePathObj = documentpath;

//открытие файла
try
{
    document = application.Documents.Add(ref templatePathObj,
            ref missingObj, ref missingObj, ref missingObj);
}
catch
{
    document.Close(ref falseobj, ref missingObj, ref missingObj);
    application.Quit(ref missingObj, ref missingObj, ref missingObj);
    document = null;
    application = null;
}

//заполнение закладок
object bookmarkObj = "закладка";
Word.Range bookmarkRange = document.Bookmarks.get_Item(ref bookmarkObj).Range;
bookmarkRange.Text = "";

//работа с таблицей
// Выбрать уже существующую таблицу внутри документа
// можно по ее порядковому номеру (начиная с 1 и начала документа)
Word.Table _table = _document.Tables[tableNumber];
_table.Rows.Add(ref _missingObj);
Word.Range _currentRange = _table.Cell(rowIndex, columnIndex).Range;
_currentRange.Text="";

//Вывод на печать
document.PrintOut(ref missingObj, ref missingObj, ref missingObj,
        ref missingObj, ref missingObj, ref missingObj, ref missingObj,
        ref missingObj, ref missingObj, ref missingObj, ref missingObj,
        ref missingObj, ref missingObj, ref missingObj, ref missingObj,
        ref missingObj, ref missingObj, ref missingObj);

document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
application.Quit();

For more information about this extension, see here and here.

 3
Author: Tivyram, 2020-05-09 21:13:00