Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5012607047 | ||
|
|
ce985d3b60 |
@@ -124,7 +124,7 @@ namespace Ivanov_App
|
||||
private void buttonExcel_Click(object sender, EventArgs e)
|
||||
{
|
||||
((Control)sender).BackColor = Color.White;
|
||||
excelTable.CreateDoc(new Ivanov_components.Models.TableConfig
|
||||
excelTable.CreateDoc(new TableConfig
|
||||
{
|
||||
FilePath = "table.xlsx",
|
||||
Header = "Example",
|
||||
@@ -134,7 +134,15 @@ namespace Ivanov_App
|
||||
{ "1", "1", "1" },
|
||||
{ "1", "2", "2" },
|
||||
{ "1", "3", "3" }
|
||||
}
|
||||
},
|
||||
new string[,] {
|
||||
{ "", "", "" },
|
||||
},
|
||||
new string[,] {
|
||||
{ "2", "2", "2" },
|
||||
{ "3", "3", "3" },
|
||||
{ "4", "4", "4" }
|
||||
},
|
||||
}
|
||||
});
|
||||
((Control)sender).BackColor = Color.Green;
|
||||
@@ -143,7 +151,7 @@ namespace Ivanov_App
|
||||
private void excelSaveHeader_Click(object sender, EventArgs e)
|
||||
{
|
||||
((Control)sender).BackColor = Color.White;
|
||||
excelWithCustomTable.CreateDoc(new Ivanov_components.Models.TableWithHeaderConfig<Student>
|
||||
excelWithCustomTable.CreateDoc(new TableWithHeaderConfig<Student>
|
||||
{
|
||||
FilePath = "header.xlsx",
|
||||
Header = "Students",
|
||||
@@ -156,7 +164,6 @@ namespace Ivanov_App
|
||||
(3, 0, "Course", "Course"),
|
||||
},
|
||||
Data = students,
|
||||
NullReplace = "Çàëóïêà)"
|
||||
});
|
||||
((Control)sender).BackColor = Color.Green;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace Ivanov_components
|
||||
creator.CreateTableWithHeader();
|
||||
creator.CreateMultiHeader(config);
|
||||
var array = new string[config.Data.Count, config.Headers.Count];
|
||||
|
||||
for (var j = 0; j < config.Data.Count; j++)
|
||||
{
|
||||
for (var i = 0; i < config.Headers.Count; i++)
|
||||
@@ -41,9 +42,7 @@ namespace Ivanov_components
|
||||
if (name != null)
|
||||
{
|
||||
object? value = config.Data[j]?.GetType().GetProperty(name)!.GetValue(config.Data[j], null);
|
||||
array[j, i] = value == null
|
||||
? config.NullReplace
|
||||
: value.ToString();
|
||||
array[j, i] = value?.ToString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,15 @@ namespace Ivanov_components.Helpers
|
||||
{
|
||||
public class WorkWithExcel : IContext
|
||||
{
|
||||
// внутренний счетчик строк для отслеживания позиции текущей строки в Excel.
|
||||
private uint _index;
|
||||
// храним данные листа
|
||||
private SheetData? _sheetData;
|
||||
// индекс строки, с которой начинается работа в таблице.
|
||||
private uint _startRowIndex;
|
||||
// столбцы
|
||||
private Columns? _columns;
|
||||
// гистограмма
|
||||
private DocumentFormat.OpenXml.Drawing.Charts.Chart? _chart;
|
||||
|
||||
private SheetData SheetData => _sheetData ??= new SheetData();
|
||||
@@ -23,12 +28,14 @@ namespace Ivanov_components.Helpers
|
||||
{
|
||||
_chart = ChartGenerator.GenerateBarChart(config);
|
||||
}
|
||||
|
||||
public void CreateMultiHeader<T>(TableWithHeaderConfig<T> config)
|
||||
{
|
||||
var counter = 1u;
|
||||
var num = 2;
|
||||
if (config.ColumnsRowsWidth != null)
|
||||
{
|
||||
// ширина столбцов
|
||||
foreach (var item in config.ColumnsRowsWidth.Where(x => x.Column > 0))
|
||||
{
|
||||
Columns.Append(new Column
|
||||
@@ -41,8 +48,10 @@ namespace Ivanov_components.Helpers
|
||||
counter++;
|
||||
}
|
||||
|
||||
// высота строки
|
||||
counter = _startRowIndex;
|
||||
num = 5;
|
||||
// строка уже есть
|
||||
if ((from r in SheetData.Elements<Row>()
|
||||
where (uint)r.RowIndex == counter
|
||||
select r).Any())
|
||||
@@ -65,13 +74,17 @@ namespace Ivanov_components.Helpers
|
||||
}
|
||||
|
||||
const uint styleIndex = 2u;
|
||||
// ищем заголовок
|
||||
if (config.Headers == null) return;
|
||||
{
|
||||
// количество заголовков, кроме заголовка в колонке 0
|
||||
var num3 = config.Headers.Count(x => x.ColumnIndex > 0);
|
||||
// извлекаем текст заголовка и создаем ячейку в колонке
|
||||
CreateCell(0, _startRowIndex,
|
||||
config.Headers.FirstOrDefault<(int, int, string, string)>
|
||||
(((int ColumnIndex, int RowIndex, string Header, string PropertyName) x)
|
||||
=> x is { ColumnIndex: 0, RowIndex: 0 }).Item3, styleIndex);
|
||||
// создаем все остальные заголовки
|
||||
for (var i = 0; i < num3; i++)
|
||||
{
|
||||
CreateCell(i + 1, _startRowIndex, config.Headers.FirstOrDefault<(int, int, string, string)>
|
||||
@@ -82,13 +95,16 @@ namespace Ivanov_components.Helpers
|
||||
}
|
||||
private static void GenerateStyle(OpenXmlPartContainer workbookPart)
|
||||
{
|
||||
// рабочая книга
|
||||
var workbookStylesPart = workbookPart.AddNewPart<WorkbookStylesPart>();
|
||||
workbookStylesPart.Stylesheet = new Stylesheet();
|
||||
// шрифт
|
||||
var fonts = new DocumentFormat.OpenXml.Spreadsheet.Fonts
|
||||
{
|
||||
Count = (UInt32Value)2u,
|
||||
KnownFonts = BooleanValue.FromBoolean(value: true)
|
||||
};
|
||||
// обычный шрифт
|
||||
fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font
|
||||
{
|
||||
FontSize = new FontSize
|
||||
@@ -108,6 +124,7 @@ namespace Ivanov_components.Helpers
|
||||
Val = new EnumValue<FontSchemeValues>(FontSchemeValues.Minor)
|
||||
}
|
||||
});
|
||||
// полужирный шрифт
|
||||
fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font
|
||||
{
|
||||
FontSize = new FontSize
|
||||
@@ -129,6 +146,7 @@ namespace Ivanov_components.Helpers
|
||||
Bold = new Bold()
|
||||
});
|
||||
workbookStylesPart.Stylesheet.Append(fonts);
|
||||
// заливка базовая
|
||||
var fills = new Fills
|
||||
{
|
||||
Count = (UInt32Value)1u
|
||||
@@ -141,10 +159,12 @@ namespace Ivanov_components.Helpers
|
||||
}
|
||||
});
|
||||
workbookStylesPart.Stylesheet.Append(fills);
|
||||
// границы
|
||||
var borders = new Borders
|
||||
{
|
||||
Count = (UInt32Value)2u
|
||||
};
|
||||
// пустая граница
|
||||
borders.Append(new Border
|
||||
{
|
||||
LeftBorder = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(),
|
||||
@@ -153,6 +173,7 @@ namespace Ivanov_components.Helpers
|
||||
BottomBorder = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(),
|
||||
DiagonalBorder = new DiagonalBorder()
|
||||
});
|
||||
// тонкая граница
|
||||
borders.Append(new Border
|
||||
{
|
||||
LeftBorder = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder
|
||||
@@ -172,11 +193,14 @@ namespace Ivanov_components.Helpers
|
||||
Style = (EnumValue<BorderStyleValues>)BorderStyleValues.Thin
|
||||
}
|
||||
});
|
||||
|
||||
workbookStylesPart.Stylesheet.Append(borders);
|
||||
// форматы для ячеек
|
||||
var cellFormats = new CellFormats
|
||||
{
|
||||
Count = (UInt32Value)3u
|
||||
};
|
||||
// используется обычный шрифт, без границ и заливок.
|
||||
cellFormats.Append(new CellFormat
|
||||
{
|
||||
NumberFormatId = (UInt32Value)0u,
|
||||
@@ -185,6 +209,7 @@ namespace Ivanov_components.Helpers
|
||||
BorderId = (UInt32Value)0u,
|
||||
FillId = (UInt32Value)0u
|
||||
});
|
||||
// тонкая граница
|
||||
cellFormats.Append(new CellFormat
|
||||
{
|
||||
NumberFormatId = (UInt32Value)0u,
|
||||
@@ -193,6 +218,7 @@ namespace Ivanov_components.Helpers
|
||||
BorderId = (UInt32Value)1u,
|
||||
FillId = (UInt32Value)0u
|
||||
});
|
||||
// текст с тонкой границей и полужирным шрифтом
|
||||
cellFormats.Append(new CellFormat
|
||||
{
|
||||
NumberFormatId = (UInt32Value)0u,
|
||||
@@ -211,6 +237,7 @@ namespace Ivanov_components.Helpers
|
||||
}
|
||||
public void CreateHeader(string header)
|
||||
{
|
||||
// жирный текст в ячейку А1
|
||||
_index = 1u;
|
||||
var cell = CreateCell("A", _index);
|
||||
var run = new DocumentFormat.OpenXml.Spreadsheet.Run();
|
||||
@@ -222,6 +249,7 @@ namespace Ivanov_components.Helpers
|
||||
cell.DataType = (EnumValue<CellValues>)CellValues.InlineString;
|
||||
_index++;
|
||||
}
|
||||
// создаем таблицу
|
||||
public void CreateTable(string[,] data)
|
||||
{
|
||||
for (var i = 0; i < data.GetLength(0); i++)
|
||||
@@ -234,6 +262,7 @@ namespace Ivanov_components.Helpers
|
||||
|
||||
_index += (uint)data.GetLength(0);
|
||||
}
|
||||
// создаем ячейки
|
||||
private Cell CreateCell(string columnName, uint rowIndex)
|
||||
{
|
||||
var columnName2 = columnName;
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
public List<(int Column, int Row)>? ColumnsRowsWidth { get; init; }
|
||||
public List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)>? Headers { get; init; }
|
||||
public List<T>? Data { get; init; }
|
||||
public string NullReplace { get; set; } = "null";
|
||||
|
||||
public void CheckFields()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user