diff --git a/ExcelHelper/ExcelHelper.sln b/ExcelHelper/ExcelHelper.sln new file mode 100644 index 0000000..43edff4 --- /dev/null +++ b/ExcelHelper/ExcelHelper.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34511.84 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelHelper", "ExcelHelper\ExcelHelper.csproj", "{A27BBD28-F1A8-46D6-90E7-7026B6DA54BA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A27BBD28-F1A8-46D6-90E7-7026B6DA54BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A27BBD28-F1A8-46D6-90E7-7026B6DA54BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A27BBD28-F1A8-46D6-90E7-7026B6DA54BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A27BBD28-F1A8-46D6-90E7-7026B6DA54BA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {57173180-D546-4DCE-8829-F7A664D607CF} + EndGlobalSection +EndGlobal diff --git a/ExcelHelper/ExcelHelper/ExcelHelper.cs b/ExcelHelper/ExcelHelper/ExcelHelper.cs new file mode 100644 index 0000000..bcdbba1 --- /dev/null +++ b/ExcelHelper/ExcelHelper/ExcelHelper.cs @@ -0,0 +1,115 @@ +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using System.Text; +using System.Text.RegularExpressions; + +namespace ExcelHelper +{ + public class ExcelHelper + { + private static readonly ExcelHelper _instance = new ExcelHelper(); + + public static ExcelHelper Instance { get { return _instance; } } + + /// + /// Get excel data + /// + /// excel path + /// sheet name + /// row start position (first row = 1) + /// copy path + /// + /// + public List> GetData(string sourcePath, string sheetName, bool removeHeader, int rowStartPosition = 1, string tempPath = "./temp_file.xlsx") + { + File.Copy(sourcePath, tempPath, true); + + List> data = new List>(); + + StringBuilder sb = new StringBuilder(); + using (SpreadsheetDocument doc = SpreadsheetDocument.Open(tempPath, true)) + { + WorkbookPart workbookPart = doc.WorkbookPart; + Sheet sheet = workbookPart.Workbook.Descendants().FirstOrDefault(s => s.Name == sheetName); + + if (sheet == null) + throw new Exception("Cannot find sheet."); + + WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id); + Worksheet worksheet = worksheetPart.Worksheet; + + List rows = worksheet.Descendants().Where(r => r.RowIndex >= rowStartPosition).ToList(); + if (removeHeader) + rows.RemoveAt(0); + + for (int i = 0; i < rows.Count; i++) + { + Dictionary rowPairs = RowToKeyValuePairs(rows[i], workbookPart); + if (rowPairs == null || rowPairs.Count < 1) + continue; + + data.Add(rowPairs); + } + } + + return data; + } + + private Dictionary RowToKeyValuePairs(Row row, WorkbookPart workbookPart) + { + Dictionary pairs = new Dictionary(); + var cells = row.Elements().ToList(); + + for (int col = 0; col < cells.Count; col++) + { + Cell cell = cells[col]; + + string cellReference = cell.CellReference.Value; + string columnName = Regex.Replace(cellReference, @"\d", ""); + //int rowNumber = int.Parse(Regex.Replace(cellReference, "[^0-9]", "")); + + string cellValue = cell.CellValue?.InnerText; + if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString) + { + int.TryParse(cellValue, out int sharedStringId); + cellValue = workbookPart.SharedStringTablePart.SharedStringTable.Elements().ElementAt(sharedStringId).InnerText; + } + + pairs.Add(columnName, cellValue); + } + + return pairs; + } + + private string ConvertValue(Dictionary pairs, string key) + { + if (!pairs.ContainsKey(key)) + return null; + + return pairs[key]; + } + + private T ConvertValue(Dictionary pairs, string key, Func func) + { + if (!pairs.ContainsKey(key) || func == null) + return default(T); + + string value = pairs[key]; + return func(value); + } + + private DateTime? ConvertValueToDateTime(Dictionary pairs, string key) + { + return ConvertValue(pairs, key, x => + { + if (string.IsNullOrEmpty(x)) + return null; + + if (double.TryParse(x, out double d)) + return DateTime.FromOADate(d); + else + return null; + }); + } + } +} diff --git a/ExcelHelper/ExcelHelper/ExcelHelper.csproj b/ExcelHelper/ExcelHelper/ExcelHelper.csproj new file mode 100644 index 0000000..8edc89c --- /dev/null +++ b/ExcelHelper/ExcelHelper/ExcelHelper.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + +