Zhelovanov_Dmitrii_COP/KOP_Labs/NonVisualComponents/WordTableHeaderComponent.cs

244 lines
5.2 KiB
C#
Raw Normal View History

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using KOP_Labs.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Wordprocessing;
namespace KOP_Labs.NonVisualComponents
{
public partial class WordTableHeaderComponent : Component
{
private WordprocessingDocument? _wordDocument;
private Body? _docBody;
public WordTableHeaderComponent()
{
InitializeComponent();
}
public WordTableHeaderComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateDoc<T>(MyTableWithHead<T> myTable)
{
if (!CheckData(myTable._dataList)) return;
_wordDocument = WordprocessingDocument.Create(myTable._filePath, WordprocessingDocumentType.Document);
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
_docBody = mainPart.Document.AppendChild(new Body());
_wordDocument.Close();
using (WordprocessingDocument doc = WordprocessingDocument.Open(myTable._filePath, true))
{
var mainDoc = doc.MainDocumentPart.Document;
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph headerParagraph = new Paragraph();
Run headerRun = new Run(new Text(myTable._fileHeader));
RunProperties runProperties = new RunProperties();
Bold bold = new Bold();
runProperties.Append(bold);
headerRun.Append(runProperties);
headerParagraph.Append(headerRun);
Table table = new Table();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}
));
table.AppendChild<TableProperties>(props);
var _tr = new TableRow();
int indexHeaderHeigh = 0;
int indexHeaderWidth = 0;
foreach (var item in myTable._columnsSettings)
{
_tr.Append(new TableRowProperties(new TableRowHeight
{
Val = Convert.ToUInt32(myTable._heightRow[indexHeaderHeigh])
}));
var tc = new TableCell();
tc.Append(new TableCellProperties(new TableCellWidth
{
Type = TableWidthUnitValues.Dxa,
Width = myTable._widthCol[indexHeaderWidth].ToString(),
}
));
if (string.IsNullOrEmpty(myTable._columnsSettings[indexHeaderWidth]._nameField) ||
string.IsNullOrEmpty(myTable._columnsSettings[indexHeaderWidth]._nameColumn))
{
return;
}
Paragraph tableHeader = new();
var Run = new Run();
var headerProperties = new RunProperties();
headerProperties.Append(new Bold());
Run.AppendChild(headerProperties);
Run.AppendChild(new Text(item.Value._nameColumn));
tableHeader.AppendChild(Run);
tc.Append(tableHeader);
_tr.Append(tc);
indexHeaderWidth++;
}
table.Append(_tr);
indexHeaderHeigh++;
indexHeaderWidth = 0;
for (int i = 1; i < myTable._dataList.Count; i++)
{
var tr = new TableRow();
foreach (var item in myTable._columnsSettings)
{
tr.Append(new TableRowProperties(new TableRowHeight
{
Val = Convert.ToUInt32(myTable._heightRow[indexHeaderHeigh])
}));
var tc = new TableCell();
tc.Append(new TableCellProperties(new TableCellWidth
{
Type = TableWidthUnitValues.Dxa,
Width = myTable._widthCol[indexHeaderWidth].ToString(),
}
));
foreach (var val in myTable._dataList[i].GetType().GetProperties())
{
if (val.Name == item.Value._nameField)
{
var newParagraph = new Paragraph();
var newRun = new Run();
var runPropertiesInd = new RunProperties();
if (indexHeaderWidth == 0)
{
runPropertiesInd.Append(new Bold());
}
newRun.AppendChild(runPropertiesInd);
newRun.AppendChild(new Text(val.GetValue(myTable._dataList[i]).ToString()));
newParagraph.AppendChild(newRun);
tc.Append(newParagraph);
break;
}
}
tr.Append(tc);
indexHeaderWidth++;
}
indexHeaderWidth = 0;
table.Append(tr);
}
mainDoc.Body.Append(headerParagraph);
mainDoc.Body.Append(table);
mainDoc.Save();
}
}
private bool CheckData<T>(List<T> dataList)
{
foreach (var data in dataList)
{
foreach (var value in data.GetType().GetProperties())
{
if (string.IsNullOrEmpty(value.GetValue(data).ToString()))
{
return false;
}
}
}
return true;
}
}
}