This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from 2nd-Semester-Project/SE2SP-2-Source-Data-M…
…anager Se2 sp 2 source data manager
- Loading branch information
Showing
6 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,64 @@ | ||
using OfficeOpenXml; // dotnet add package EPPlus | ||
using System.Globalization; | ||
|
||
namespace HeatOptimiser | ||
{ | ||
public class SourceDataPoint | ||
{ | ||
public DateTime? TimeFrom; | ||
public DateTime? TimeTo; | ||
public double? HeatDemand; | ||
public double? ElectricityPrice; | ||
} | ||
public class SourceData { | ||
public List<SourceDataPoint> SummerData; | ||
public List<SourceDataPoint> WinterData; | ||
public SourceData(){ | ||
SourceDataManager sourceManager = new SourceDataManager(); | ||
SummerData = sourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 2); | ||
WinterData = sourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 7); | ||
} | ||
} | ||
public class SourceDataManager : ISourceDataManager | ||
{ | ||
public SourceDataManager() | ||
// Example usage: List<SourceDataPoint> SourceList = SourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 2); Columns and Rows start with 1!!!! | ||
public List<SourceDataPoint> LoadXLSXFile(string file, int rowStart, int columnStart, int workSheetNumber = 0) | ||
{ | ||
throw new System.NotImplementedException(); | ||
var sourceList = new List<SourceDataPoint>(); | ||
|
||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // EPPlus license | ||
|
||
using (var package = new ExcelPackage(new FileInfo(file))) | ||
{ | ||
var worksheet = package.Workbook.Worksheets[workSheetNumber]; | ||
|
||
if (worksheet.Dimension == null) | ||
{ | ||
Console.WriteLine("The worksheet is empty."); | ||
return sourceList; | ||
} | ||
for (int row = rowStart; row <= worksheet.Dimension.End.Row; row++) | ||
{ | ||
try { | ||
DateTime temp; | ||
string[] formats = { "dd/MM/yyyy HH.mm.ss", "dd/MM/yyyy HH:mm:ss", "HH.mm.ss", "HH:mm:ss" }; | ||
SourceDataPoint sourceData = new SourceDataPoint | ||
{ | ||
TimeFrom = DateTime.TryParseExact(worksheet.Cells[row, columnStart].Value?.ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp) ? temp : (DateTime?)null, | ||
TimeTo = DateTime.TryParseExact(worksheet.Cells[row, columnStart + 1].Value?.ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp) ? temp : (DateTime?)null, | ||
HeatDemand = worksheet.Cells[row, columnStart + 2]?.Value != null ? double.Parse(worksheet.Cells[row, columnStart + 2].Value.ToString()!) : null, | ||
ElectricityPrice = worksheet.Cells[row, columnStart + 3]?.Value != null ? double.Parse(worksheet.Cells[row, columnStart + 3].Value.ToString()!) : null | ||
}; | ||
sourceList.Add(sourceData); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Error: {e}"); | ||
} | ||
} | ||
} | ||
|
||
return sourceList; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,6 @@ class Program { | |
public static void Main() | ||
{ | ||
new TextBasedUI().example(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.