166 lines
4.0 KiB
C#
166 lines
4.0 KiB
C#
|
|
|||
|
using DocumentFormat.OpenXml;
|
|||
|
using DocumentFormat.OpenXml.ExtendedProperties;
|
|||
|
using DocumentFormat.OpenXml.Packaging;
|
|||
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|||
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|||
|
|
|||
|
using KOP_Labs.Classes;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Linq;
|
|||
|
using System.Runtime.InteropServices;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|||
|
using Bold = DocumentFormat.OpenXml.Wordprocessing.Bold;
|
|||
|
using BottomBorder = DocumentFormat.OpenXml.Wordprocessing.BottomBorder;
|
|||
|
using LeftBorder = DocumentFormat.OpenXml.Wordprocessing.LeftBorder;
|
|||
|
using RightBorder = DocumentFormat.OpenXml.Wordprocessing.RightBorder;
|
|||
|
using Run = DocumentFormat.OpenXml.Wordprocessing.Run;
|
|||
|
using RunProperties = DocumentFormat.OpenXml.Wordprocessing.RunProperties;
|
|||
|
using Table = DocumentFormat.OpenXml.Wordprocessing.Table;
|
|||
|
using Text = DocumentFormat.OpenXml.Wordprocessing.Text;
|
|||
|
using TopBorder = DocumentFormat.OpenXml.Wordprocessing.TopBorder;
|
|||
|
|
|||
|
namespace KOP_Labs.NonVisualComponents
|
|||
|
{
|
|||
|
public partial class WordTableComponent : Component
|
|||
|
{
|
|||
|
private WordprocessingDocument? _wordDocument;
|
|||
|
|
|||
|
private Body? _docBody;
|
|||
|
|
|||
|
public WordTableComponent()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
public WordTableComponent(IContainer container)
|
|||
|
{
|
|||
|
container.Add(this);
|
|||
|
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
public void CreateDoc(MyTable 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);
|
|||
|
for (var i = 0; i < myTable._dataList.Count; i++)
|
|||
|
{
|
|||
|
var tr = new TableRow();
|
|||
|
|
|||
|
for (var j = 0; j < myTable._dataList[i].Length; j++)
|
|||
|
{
|
|||
|
var tc = new TableCell();
|
|||
|
|
|||
|
tc.Append(new TableCellProperties(new TableCellWidth
|
|||
|
{
|
|||
|
Type = TableWidthUnitValues.Dxa,
|
|||
|
Width = "2000"
|
|||
|
}
|
|||
|
));
|
|||
|
|
|||
|
tc.Append(new Paragraph(new Run(new Text(myTable._dataList[i][0, j]))));
|
|||
|
|
|||
|
tr.Append(tc);
|
|||
|
}
|
|||
|
|
|||
|
table.Append(tr);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
mainDoc.Body.Append(headerParagraph);
|
|||
|
mainDoc.Body.Append(table);
|
|||
|
|
|||
|
|
|||
|
mainDoc.Save();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private bool CheckData(List<string[,]> data)
|
|||
|
{
|
|||
|
for (int i = 0; i < data.Count; i++)
|
|||
|
{
|
|||
|
for (int j = 0; j < data[i].Length; j++)
|
|||
|
{
|
|||
|
if (data[i][0, j] == null) {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|