diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-07-23 11:33:41 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-07-23 11:33:41 +0300 |
| commit | 3987f9cab0cb3d0d15d0ba784db14be4472f45ca (patch) | |
| tree | 22b2dce8d1703fb5661376a8b23f9ca4e5a98688 /Software/Visual_Studio/Tango.Documents | |
| parent | 3fc4e625860d6c847be251641b533ee280dc4693 (diff) | |
| download | Tango-3987f9cab0cb3d0d15d0ba784db14be4472f45ca.tar.gz Tango-3987f9cab0cb3d0d15d0ba784db14be4472f45ca.zip | |
Implemented color catalogs module.
Diffstat (limited to 'Software/Visual_Studio/Tango.Documents')
| -rw-r--r-- | Software/Visual_Studio/Tango.Documents/ExcelReader.cs | 6 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Documents/ExcelWriter.cs | 94 |
2 files changed, 94 insertions, 6 deletions
diff --git a/Software/Visual_Studio/Tango.Documents/ExcelReader.cs b/Software/Visual_Studio/Tango.Documents/ExcelReader.cs index 3d6b37445..6d8cc10f2 100644 --- a/Software/Visual_Studio/Tango.Documents/ExcelReader.cs +++ b/Software/Visual_Studio/Tango.Documents/ExcelReader.cs @@ -49,7 +49,7 @@ namespace Tango.Documents /// <param name="templatePath">File path to the Excel document.</param> /// <param name="throwOnError">Throw exceptions if encountering an error while parsing the document.</param> public ExcelReader(string templatePath, bool throwOnError = true) - : this(File.OpenRead(templatePath), throwOnError) { } + : this(new FileStream(templatePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), throwOnError) { } #endregion @@ -405,6 +405,10 @@ namespace Tango.Documents prop.SetValue(obj, Convert.ToBoolean(val), null); else if (PType == typeof(byte) || PType == typeof(byte?)) prop.SetValue(obj, Convert.ToByte(val), null); + else if (PType == typeof(System.Windows.Media.Color)) + { + //Do nothing.. + } else prop.SetValue(obj, val, null); } diff --git a/Software/Visual_Studio/Tango.Documents/ExcelWriter.cs b/Software/Visual_Studio/Tango.Documents/ExcelWriter.cs index 63c04bce9..2f1e4c29f 100644 --- a/Software/Visual_Studio/Tango.Documents/ExcelWriter.cs +++ b/Software/Visual_Studio/Tango.Documents/ExcelWriter.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Windows.Media; namespace Tango.Documents { @@ -16,6 +17,8 @@ namespace Tango.Documents private Stream _stream; private SpreadsheetDocument _document; private List<DefinedName> _column_names; + private Worksheet _currentWorkSheet; + private Stylesheet _currentStyleSheet; public ExcelWriter(Stream source) { @@ -37,25 +40,38 @@ namespace Tango.Documents } - public void WriteData<T>(IEnumerable<T> data, String sheetName) + public void WriteData<T>(IEnumerable<T> data, String sheetName, int? headerRowNumber = null) { var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList(); WorksheetPart work_sheet_part = GetWorkSheetPart(sheetName); Worksheet workSheet = work_sheet_part.Worksheet; + _currentWorkSheet = workSheet; SheetData sheetData = workSheet.GetFirstChild<SheetData>(); List<Row> rows = sheetData.Elements<Row>().ToList(); var list = data.ToList(); + int lastRowIndex = headerRowNumber.HasValue ? headerRowNumber.Value : 0; + for (int i = 0; i < list.Count; i++) { var item = list[i]; - var last_row = sheetData.Elements<Row>().Last(); + Row last_row = null; + + if (headerRowNumber.HasValue) + { + last_row = sheetData.Elements<Row>().ElementAt(lastRowIndex - 1); + } + else + { + last_row = sheetData.Elements<Row>().Last(); + } Row newRow = new Row(); newRow.RowIndex = last_row.RowIndex + 1; + lastRowIndex = (int)newRow.RowIndex.Value; sheetData.InsertAfter(newRow, last_row); @@ -65,19 +81,28 @@ namespace Tango.Documents if (definedName != null) { - SetCellRow(newRow, GetCellReference(definedName, i + 2), prop.GetValue(item)); + SetCellRow(newRow, GetCellReference(definedName, (int)(last_row.RowIndex + 1)), prop.GetValue(item)); } } } + + if (_currentStyleSheet != null) + { + _currentStyleSheet.Save(); + _currentStyleSheet = null; + } } private void SetCellRow(Row row, string cellReference, object value) { Cell cell = row.Descendants<Cell>().FirstOrDefault(c => c.CellReference == cellReference); + + bool append = false; if (cell == null) { cell = new Cell(); cell.CellReference = cellReference; + append = true; } cell.CellValue = new CellValue(value.ToString()); @@ -95,8 +120,66 @@ namespace Tango.Documents { cell.DataType = CellValues.Boolean; } + else if (value is System.Windows.Media.Color) + { + cell.DataType = CellValues.String; + + var color = (System.Windows.Media.Color)value; + + if (_currentStyleSheet == null) + { + WorkbookPart wbPart = _document.WorkbookPart; + WorkbookStylesPart stylesPart = wbPart.WorkbookStylesPart; + _currentStyleSheet = stylesPart.Stylesheet; + } - row.Append(cell); + var fill = new Fill(new PatternFill( + new ForegroundColor() + { + Rgb = new DocumentFormat.OpenXml.HexBinaryValue() + { + Value = new ColorConverter().ConvertToString(color).Replace("#", ""), + }, + }, + new BackgroundColor() + { + Rgb = new DocumentFormat.OpenXml.HexBinaryValue() + { + Value = new ColorConverter().ConvertToString(color).Replace("#", ""), + }, + }) + { + PatternType = PatternValues.Solid, + }); + + if (_currentStyleSheet.Fills == null) + { + _currentStyleSheet.Fills = new Fills(); + } + + _currentStyleSheet.Fills.Append(fill); + + _currentStyleSheet.CellFormats.Append(new CellFormat() + { + ApplyFill = true, + FillId = _currentStyleSheet.Fills.Count, + NumberFormatId = (DocumentFormat.OpenXml.UInt32Value)0U, + FontId = (DocumentFormat.OpenXml.UInt32Value)0U, + BorderId = (DocumentFormat.OpenXml.UInt32Value)0U, + FormatId = (DocumentFormat.OpenXml.UInt32Value)0U, + }); + + cell.CellValue = new CellValue(String.Empty); + cell.StyleIndex = new DocumentFormat.OpenXml.UInt32Value(_currentStyleSheet.CellFormats.Count); + + _currentStyleSheet.CellFormats.Count++; + _currentStyleSheet.Fills.Count++; + } + + if (append) + { + row.Append(cell); + } } private WorksheetPart GetWorkSheetPart(String sheetName) @@ -110,7 +193,8 @@ namespace Tango.Documents { Regex rowPatern = new Regex("^.*\\!\\$(.*)\\$\\d*$"); Match match = rowPatern.Match(name.Text); - return match.Groups[1].Value + rowIndex.ToString(); + String s = match.Groups[1].Value + rowIndex.ToString(); + return s; } public void Dispose() |
