Framework version

main
syneffort 1 year ago
parent ff51117201
commit f7d973960e
  1. 6
      ExcelHelper/ExcelHelper.sln
  2. 120
      ExcelHelper/ExcelHelperFW/ExcelHelper.cs
  3. 58
      ExcelHelper/ExcelHelperFW/ExcelHelperFW.csproj
  4. 36
      ExcelHelper/ExcelHelperFW/Properties/AssemblyInfo.cs
  5. 5
      ExcelHelper/ExcelHelperFW/packages.config

@ -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

@ -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; } }
/// <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;
}
private string ConvertValue(Dictionary<string, string> pairs, string key)
{
if (!pairs.ContainsKey(key))
return null;
return pairs[key];
}
private 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);
}
private 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>{652A0603-38E8-4407-A74F-5D1497129646}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ExcelHelperFW</RootNamespace>
<AssemblyName>ExcelHelperFW</AssemblyName>
<TargetFrameworkVersion>v4.8</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\net46\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\net46\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("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")]

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