diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-10-03 14:19:04 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-10-03 14:19:04 +0300 |
| commit | c23d740d6a80db62b7b43ea17639ba4c2ef4e336 (patch) | |
| tree | bc0f437f1697bd232d4dbf964a46f6d62ed2c4db /Software/Visual_Studio/Tango.Documents | |
| parent | 251cf705409697f339828359b6770534116dca5f (diff) | |
| download | Tango-c23d740d6a80db62b7b43ea17639ba4c2ef4e336.tar.gz Tango-c23d740d6a80db62b7b43ea17639ba4c2ef4e336.zip | |
Working on RML Module..
Diffstat (limited to 'Software/Visual_Studio/Tango.Documents')
| -rw-r--r-- | Software/Visual_Studio/Tango.Documents/ExcelWriter.cs | 104 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Documents/Tango.Documents.csproj | 3 |
2 files changed, 106 insertions, 1 deletions
diff --git a/Software/Visual_Studio/Tango.Documents/ExcelWriter.cs b/Software/Visual_Studio/Tango.Documents/ExcelWriter.cs new file mode 100644 index 000000000..613bbd90f --- /dev/null +++ b/Software/Visual_Studio/Tango.Documents/ExcelWriter.cs @@ -0,0 +1,104 @@ +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Tango.Documents +{ + public class ExcelWriter : IDisposable + { + private Stream _stream; + private SpreadsheetDocument _document; + private List<DefinedName> _column_names; + + public ExcelWriter(Stream source) + { + _stream = source; + _document = SpreadsheetDocument.Open(_stream, true); + _column_names = new List<DefinedName>(); + + if (_document.WorkbookPart.Workbook.GetFirstChild<DefinedNames>() != null) + { + foreach (DefinedName name in _document.WorkbookPart.Workbook.GetFirstChild<DefinedNames>()) + { + _column_names.Add(name); + } + } + } + + public ExcelWriter(String fileName) : this(new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + + } + + public void WriteData<T>(IEnumerable<T> data, String sheetName) + { + var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList(); + + WorksheetPart work_sheet_part = GetWorkSheetPart(sheetName); + Worksheet workSheet = work_sheet_part.Worksheet; + SheetData sheetData = workSheet.GetFirstChild<SheetData>(); + List<Row> rows = sheetData.Elements<Row>().ToList(); + + var list = data.ToList(); + + for (int i = 0; i < list.Count; i++) + { + var item = list[i]; + + Row newRow = new Row(); + + sheetData.InsertAfter(newRow, sheetData.Elements<Row>().Last()); + + foreach (var prop in props) + { + var definedName = _column_names.SingleOrDefault(x => x.Name == prop.Name); + + if (definedName != null) + { + SetCellRow(newRow, GetCellReference(definedName, i + 2), prop.GetValue(item)); + } + } + } + } + + private void SetCellRow(Row row, string cellReference, object value) + { + Cell cell = row.Descendants<Cell>().FirstOrDefault(c => c.CellReference == cellReference); + if (cell == null) + { + cell = new Cell(); + cell.CellReference = cellReference; + } + cell.CellValue = new CellValue(value.ToString()); + cell.DataType = CellValues.String; + row.Append(cell); + } + + private WorksheetPart GetWorkSheetPart(String sheetName) + { + var sheet = _document.WorkbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(x => x.Name.Value == sheetName); + string relId = sheet.Id; + return (WorksheetPart)_document.WorkbookPart.GetPartById(relId); + } + + private string GetCellReference(DefinedName name, int rowIndex) + { + Regex rowPatern = new Regex("^.*\\!\\$(.*)\\$\\d*$"); + Match match = rowPatern.Match(name.Text); + return match.Groups[1].Value + rowIndex.ToString(); + } + + public void Dispose() + { + _document.Save(); + _stream.Dispose(); + } + } +} diff --git a/Software/Visual_Studio/Tango.Documents/Tango.Documents.csproj b/Software/Visual_Studio/Tango.Documents/Tango.Documents.csproj index cbfd9c79b..17ba03257 100644 --- a/Software/Visual_Studio/Tango.Documents/Tango.Documents.csproj +++ b/Software/Visual_Studio/Tango.Documents/Tango.Documents.csproj @@ -60,6 +60,7 @@ <Link>GlobalVersionInfo.cs</Link> </Compile> <Compile Include="ExcelReader.cs" /> + <Compile Include="ExcelWriter.cs" /> <Compile Include="Properties\AssemblyInfo.cs"> <SubType> </SubType> @@ -87,7 +88,7 @@ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <ProjectExtensions> <VisualStudio> - <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" /> + <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" /> </VisualStudio> </ProjectExtensions> </Project>
\ No newline at end of file |
