From dace3f5c05892478ea328074f9f0502bfdab0d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Fri, 21 Dec 2018 19:59:06 +0100 Subject: [PATCH] Added tables --- .../Interop/OpenXml.cs | 75 ++++++++++++++++++- .../ViewModels/TagExplorer.cs | 2 +- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/MicroStationTagExplorer.Core/Interop/OpenXml.cs b/src/MicroStationTagExplorer.Core/Interop/OpenXml.cs index 548e429..0e880da 100644 --- a/src/MicroStationTagExplorer.Core/Interop/OpenXml.cs +++ b/src/MicroStationTagExplorer.Core/Interop/OpenXml.cs @@ -7,7 +7,25 @@ namespace MicroStationTagExplorer.Core.Interop { internal class OpenXml { - public static void ExportValues(string path, object[,] values, int nRows, int nColumns, string sheetName, string tableName) + private static string ToRange(uint columnNumber) + { + uint end = columnNumber; + string name = string.Empty; + while (end > 0) + { + uint modulo = (end - 1) % 26; + name = Convert.ToChar(65 + modulo).ToString() + name; + end = (uint)((end - modulo) / 26); + } + return name; + } + + private static string ToRange(uint startRow, uint endRow, uint startColumn, uint endColumn) + { + return ToRange(startColumn) + startRow.ToString() + ":" + ToRange(endColumn) + endRow.ToString(); + } + + public static void ExportValues(string path, object[,] values, uint nRows, uint nColumns, string sheetName, string tableName) { var spreadsheetDocument = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook); @@ -29,14 +47,13 @@ namespace MicroStationTagExplorer.Core.Interop var sheetData = worksheetPart.Worksheet.GetFirstChild(); WriteValues(sheetData, values, nRows, nColumns); - - + WriteTable(worksheetPart, 1U, values, nRows, nColumns); workbookpart.Workbook.Save(); spreadsheetDocument.Close(); } - public static void WriteValues(SheetData sheetData, object[,] values, int nRows, int nColumns) + public static void WriteValues(SheetData sheetData, object[,] values, uint nRows, uint nColumns) { for (int r = 0; r < nRows; r++) { @@ -54,5 +71,55 @@ namespace MicroStationTagExplorer.Core.Interop } } } + + private static void WriteTable(WorksheetPart worksheetPart, uint tableID, object[,] values, uint nRows, uint nColumns) + { + string range = ToRange(1, nRows, 1, nColumns); + + var autoFilter = new AutoFilter + { + Reference = range + }; + + var tableColumns = new TableColumns() + { + Count = nColumns + }; + + for (int c = 0; c < nColumns; c++) + { + var column = new TableColumn() + { + Id = (UInt32Value)(c + 1U), + Name = values[0, c].ToString() + }; + tableColumns.Append(column); + } + + var tableStyleInfo = new TableStyleInfo() + { + Name = "TableStyleMedium2", + ShowFirstColumn = true, + ShowLastColumn = false, + ShowRowStripes = true, + ShowColumnStripes = true + }; + + var table = new Table() + { + Id = tableID, + Name = $"Table{tableID}", + DisplayName = $"Table{tableID}", + Reference = range, + TotalsRowShown = false + }; + + table.Append(autoFilter); + table.Append(tableColumns); + table.Append(tableStyleInfo); + + var tableDefinitionPart = worksheetPart.AddNewPart($"rId{tableID}"); + tableDefinitionPart.Table = table; + } } } diff --git a/src/MicroStationTagExplorer.Core/ViewModels/TagExplorer.cs b/src/MicroStationTagExplorer.Core/ViewModels/TagExplorer.cs index d150b63..376680e 100644 --- a/src/MicroStationTagExplorer.Core/ViewModels/TagExplorer.cs +++ b/src/MicroStationTagExplorer.Core/ViewModels/TagExplorer.cs @@ -534,7 +534,7 @@ namespace MicroStationTagExplorer.Core.ViewModels public void ExportTagsFile(string fileName) { - OpenXml.ExportValues(fileName, Project.TagValues, Project.Tags.Count + 1, 6, "Tags", "Tags"); + OpenXml.ExportValues(fileName, Project.TagValues, (uint)Project.Tags.Count + 1U, 6U, "Tags", "Tags"); } public void ExportTagsExcel()