NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Diagnostics.SymbolStore;
using System.Runtime.CompilerServices;
using System.Diagnostics.Tracing;
namespace WordInteropTesting
{
class Program
{
static void Main(string[] args)
{
//заполним будущие данные для таблицы - двумерный массив строк 3х4
string[,] source = new string[3, 4];
source[0, 0] = "00";
source[0, 1] = "01";
source[0, 2] = "02";
source[0, 3] = "03";
source[1, 0] = "10";
source[1, 1] = "11";
source[1, 2] = "12";
source[1, 3] = "13";
source[2, 0] = "20";
source[2, 1] = "21";
source[2, 2] = "22";
source[2, 3] = "23";
List<CellRangeRC> spans = new List<CellRangeRC>() {
new CellRangeRC(2, 0, 2, 3)
};
//List<CellRangeRC> spans1 = new List<CellRangeRC>() {
// new CellRangeRC(0, 0, 0, 1)
// };
CreateTable(source, spans);
//CreateTable(source, spans1);
}
static public void CreateTable(string[,] source, List<CellRangeRC> spans)
{
try
{
Application _application = new Application(); //создаем прил-е ворд
object _missingObj = Missing.Value; //пустой параметр (знач-е не указано)
//создаем док-нт ворд (документ1)
Document _document = _application.Documents.Add(ref _missingObj, ref _missingObj, ref _missingObj, ref _missingObj);
//объект selection хранит тек.позицию курсора(или выделенный диапазон)
Selection Selection = _document.Application.Selection;
int NumRows = source.GetLength(0); //сохр.кол-во стр
int NumColumns = source.GetLength(1); //сохр. кол-во стб
Table _table = _document.Tables.Add(Selection.Range, NumRows, NumColumns);
//красота таблицы
_table.AllowAutoFit = true;
_table.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitFixed);
_table.set_Style("сетка таблицы");
WdParagraphAlignment cellsHorizontalAlignment = WdParagraphAlignment.wdAlignParagraphCenter;
WdCellVerticalAlignment cellsVerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
WdLineSpacing lineSpacingRule = WdLineSpacing.wdLineSpaceSingle;
//заполняем таблицу по одной ячейке каждая (в процессе наводим красоту ячейки)
for (int i = 0; i < NumRows; i++)
{
for (int j = 0; j < NumColumns; j++)
{
_table.Cell(i + 1, j + 1).Range.InsertAfter(source[i, j]);
_table.Cell(i + 1, j + 1).Range.ParagraphFormat.Alignment = cellsHorizontalAlignment;
_table.Cell(i + 1, j + 1).VerticalAlignment = cellsVerticalAlignment;
_table.Cell(i + 1, j + 1).Range.ParagraphFormat.LineSpacingRule = lineSpacingRule;
}
}
//горизонтально
//foreach (var c in spans) {
// _table.Cell(c.R1+1, c.C1+1).Merge(_table.Cell(c.R1+1, c.C2+1));
//}
//_application.Visible = true;

//вертикально
foreach (var c in spans)
{
object begCell = _table.Cell(c.R1 + 1, c.C1 + 1).Range.Start;
object endCell = _table.Cell(c.R2 + 1, c.C2 + 1).Range.End;
Range wordcellrange = _document.Range(ref begCell, ref endCell);
wordcellrange.Select();
_application.Selection.Cells.Merge();
}
_application.Visible = true;
}
catch (Exception error) { throw error; }
}
}
public struct CellRangeRC
{
public int R1;
public int C1;
public int R2;
public int C2;
public CellRangeRC(int r1, int c1, int r2, int c2)
{
this.R1 = r1;
this.C1 = c1;
this.R2 = r2;
this.C2 = c2;
}
public override string ToString()
{
return $"[({R1},{C1}); ({R2},{C2})]";
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.