diff --git a/ExcelHelper/ExcelHelper.sln b/ExcelHelper/ExcelHelper.sln index 43edff4..8890cab 100644 --- a/ExcelHelper/ExcelHelper.sln +++ b/ExcelHelper/ExcelHelper.sln @@ -5,6 +5,8 @@ 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 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelHelperFW", "ExcelHelperFW\ExcelHelperFW.csproj", "{652A0603-38E8-4407-A74F-5D1497129646}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {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 + {652A0603-38E8-4407-A74F-5D1497129646}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {652A0603-38E8-4407-A74F-5D1497129646}.Debug|Any CPU.Build.0 = Debug|Any CPU + {652A0603-38E8-4407-A74F-5D1497129646}.Release|Any CPU.ActiveCfg = Release|Any CPU + {652A0603-38E8-4407-A74F-5D1497129646}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ExcelHelper/ExcelHelperFW/ExcelHelper.cs b/ExcelHelper/ExcelHelperFW/ExcelHelper.cs new file mode 100644 index 0000000..4cfbb2e --- /dev/null +++ b/ExcelHelper/ExcelHelperFW/ExcelHelper.cs @@ -0,0 +1,120 @@ +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace ExcelHelperFW +{ + 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/ExcelHelperFW/ExcelHelperFW.csproj b/ExcelHelper/ExcelHelperFW/ExcelHelperFW.csproj new file mode 100644 index 0000000..0619d1d --- /dev/null +++ b/ExcelHelper/ExcelHelperFW/ExcelHelperFW.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {652A0603-38E8-4407-A74F-5D1497129646} + Library + Properties + ExcelHelperFW + ExcelHelperFW + v4.8 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\DocumentFormat.OpenXml.3.0.1\lib\net46\DocumentFormat.OpenXml.dll + + + ..\packages\DocumentFormat.OpenXml.Framework.3.0.1\lib\net46\DocumentFormat.OpenXml.Framework.dll + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ExcelHelper/ExcelHelperFW/Properties/AssemblyInfo.cs b/ExcelHelper/ExcelHelperFW/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7cc04ac --- /dev/null +++ b/ExcelHelper/ExcelHelperFW/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("ExcelHelperFW")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ExcelHelperFW")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("652a0603-38e8-4407-a74f-5d1497129646")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ExcelHelper/ExcelHelperFW/packages.config b/ExcelHelper/ExcelHelperFW/packages.config new file mode 100644 index 0000000..f1e9037 --- /dev/null +++ b/ExcelHelper/ExcelHelperFW/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file