Excel helper

main
syneffort 1 year ago
parent 902e0fa185
commit cbbc2bc5c4
  1. 122
      PLibs/ExcelHelper/ExcelHelper.cs
  2. 58
      PLibs/ExcelHelper/ExcelHelper.csproj
  3. 36
      PLibs/ExcelHelper/Properties/AssemblyInfo.cs
  4. 5
      PLibs/ExcelHelper/packages.config
  5. 115
      PLibs/ExcelHelperCore/ExcelHelper.cs
  6. 13
      PLibs/ExcelHelperCore/ExcelHelperCore.csproj
  7. 31
      PLibs/PLibs.sln

@ -0,0 +1,122 @@
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;
namespace ExcelHelper
{
public class ExcelHelper
{
private static readonly ExcelHelper _instance = new ExcelHelper();
public static ExcelHelper Instance { get { return _instance; } }
/// <summary>
/// Get excel data
/// </summary>
/// <param name="sourcePath">excel path</param>
/// <param name="sheetName">sheet name</param>
/// <param name="rowStartPosition">row start position (first row = 1)</param>
/// <param name="tempPath">copy path</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public List<Dictionary<string, string>> GetData(string sourcePath, string sheetName, bool removeHeader, int rowStartPosition = 1, Func<Dictionary<string, string>, bool> continueFunc = null, string tempPath = "./temp_file.xlsx")
{
File.Copy(sourcePath, tempPath, true);
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
StringBuilder sb = new StringBuilder();
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(tempPath, true))
{
WorkbookPart workbookPart = doc.WorkbookPart;
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName);
if (sheet == null)
throw new Exception($"Cannot find sheet. (Sheet name: {sheetName})");
WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
Worksheet worksheet = worksheetPart.Worksheet;
List<Row> rows = worksheet.Descendants<Row>().Where(r => r.RowIndex >= rowStartPosition).ToList();
if (removeHeader)
rows.RemoveAt(0);
for (int i = 0; i < rows.Count; i++)
{
Dictionary<string, string> rowPairs = RowToKeyValuePairs(rows[i], workbookPart);
if (rowPairs == null || rowPairs.Count < 1)
continue;
if (continueFunc != null && continueFunc(rowPairs))
continue;
data.Add(rowPairs);
}
}
return data;
}
private Dictionary<string, string> RowToKeyValuePairs(Row row, WorkbookPart workbookPart)
{
Dictionary<string, string> pairs = new Dictionary<string, string>();
var cells = row.Elements<Cell>().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<SharedStringItem>().ElementAt(sharedStringId).InnerText;
}
pairs.Add(columnName, cellValue);
}
return pairs;
}
public string ConvertValue(Dictionary<string, string> pairs, string key)
{
if (!pairs.ContainsKey(key))
return null;
return pairs[key];
}
public T ConvertValue<T>(Dictionary<string, string> pairs, string key, Func<string, T> func)
{
if (!pairs.ContainsKey(key) || func == null)
return default(T);
string value = pairs[key];
return func(value);
}
public DateTime? ConvertValueToDateTime(Dictionary<string, string> pairs, string key)
{
return ConvertValue<DateTime?>(pairs, key, x =>
{
if (string.IsNullOrEmpty(x))
return null;
if (double.TryParse(x, out double d))
return DateTime.FromOADate(d);
else
return null;
});
}
}
}

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FFEDB9F8-4285-493B-A30E-900C53D0CB55}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ExcelHelper</RootNamespace>
<AssemblyName>ExcelHelper</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DocumentFormat.OpenXml, Version=3.0.1.0, Culture=neutral, PublicKeyToken=8fb06cb64d019a17, processorArchitecture=MSIL">
<HintPath>..\packages\DocumentFormat.OpenXml.3.0.1\lib\net40\DocumentFormat.OpenXml.dll</HintPath>
</Reference>
<Reference Include="DocumentFormat.OpenXml.Framework, Version=3.0.1.0, Culture=neutral, PublicKeyToken=8fb06cb64d019a17, processorArchitecture=MSIL">
<HintPath>..\packages\DocumentFormat.OpenXml.Framework.3.0.1\lib\net40\DocumentFormat.OpenXml.Framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="ExcelHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해
// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면
// 이러한 특성 값을 변경하세요.
[assembly: AssemblyTitle("ExcelHelper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ExcelHelper")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에
// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면
// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요.
[assembly: ComVisible(false)]
// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다.
[assembly: Guid("ffedb9f8-4285-493b-a30e-900c53d0cb55")]
// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다.
//
// 주 버전
// 부 버전
// 빌드 번호
// 수정 버전
//
// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를
// 기본값으로 할 수 있습니다.
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DocumentFormat.OpenXml" version="3.0.1" targetFramework="net45" />
<package id="DocumentFormat.OpenXml.Framework" version="3.0.1" targetFramework="net45" />
</packages>

@ -0,0 +1,115 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Text.RegularExpressions;
using System.Text;
namespace ExcelHelperCore
{
public class ExcelHelper
{
private static readonly ExcelHelper _instance = new ExcelHelper();
public static ExcelHelper Instance { get { return _instance; } }
/// <summary>
/// Get excel data
/// </summary>
/// <param name="sourcePath">excel path</param>
/// <param name="sheetName">sheet name</param>
/// <param name="rowStartPosition">row start position (first row = 1)</param>
/// <param name="tempPath">copy path</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public List<Dictionary<string, string>> GetData(string sourcePath, string sheetName, bool removeHeader, int rowStartPosition = 1, string tempPath = "./temp_file.xlsx")
{
File.Copy(sourcePath, tempPath, true);
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
StringBuilder sb = new StringBuilder();
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(tempPath, true))
{
WorkbookPart workbookPart = doc.WorkbookPart;
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().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<Row> rows = worksheet.Descendants<Row>().Where(r => r.RowIndex >= rowStartPosition).ToList();
if (removeHeader)
rows.RemoveAt(0);
for (int i = 0; i < rows.Count; i++)
{
Dictionary<string, string> rowPairs = RowToKeyValuePairs(rows[i], workbookPart);
if (rowPairs == null || rowPairs.Count < 1)
continue;
data.Add(rowPairs);
}
}
return data;
}
private Dictionary<string, string> RowToKeyValuePairs(Row row, WorkbookPart workbookPart)
{
Dictionary<string, string> pairs = new Dictionary<string, string>();
var cells = row.Elements<Cell>().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<SharedStringItem>().ElementAt(sharedStringId).InnerText;
}
pairs.Add(columnName, cellValue);
}
return pairs;
}
public string ConvertValue(Dictionary<string, string> pairs, string key)
{
if (!pairs.ContainsKey(key))
return null;
return pairs[key];
}
public T ConvertValue<T>(Dictionary<string, string> pairs, string key, Func<string, T> func)
{
if (!pairs.ContainsKey(key) || func == null)
return default(T);
string value = pairs[key];
return func(value);
}
public DateTime? ConvertValueToDateTime(Dictionary<string, string> pairs, string key)
{
return ConvertValue<DateTime?>(pairs, key, x =>
{
if (string.IsNullOrEmpty(x))
return null;
if (double.TryParse(x, out double d))
return DateTime.FromOADate(d);
else
return null;
});
}
}
}

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.1" />
</ItemGroup>
</Project>

@ -0,0 +1,31 @@

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", "{FFEDB9F8-4285-493B-A30E-900C53D0CB55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelHelperCore", "ExcelHelperCore\ExcelHelperCore.csproj", "{0DC75403-4084-462B-936B-FA86EA90811B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FFEDB9F8-4285-493B-A30E-900C53D0CB55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFEDB9F8-4285-493B-A30E-900C53D0CB55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFEDB9F8-4285-493B-A30E-900C53D0CB55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFEDB9F8-4285-493B-A30E-900C53D0CB55}.Release|Any CPU.Build.0 = Release|Any CPU
{0DC75403-4084-462B-936B-FA86EA90811B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DC75403-4084-462B-936B-FA86EA90811B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DC75403-4084-462B-936B-FA86EA90811B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DC75403-4084-462B-936B-FA86EA90811B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3B2E5C38-A49D-48ED-8CA3-7FBB06E93B60}
EndGlobalSection
EndGlobal
Loading…
Cancel
Save