-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/siisltd/Curiosity.Utils
- Loading branch information
Showing
30 changed files
with
1,647 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
5 changes: 5 additions & 0 deletions
5
src/FileData/Curiosity.FileDataReaderWriters.Npoi/CHANGELOG.md
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## [1.0.0] - 2024-02-02 | ||
|
||
Package was released. |
54 changes: 54 additions & 0 deletions
54
...FileData/Curiosity.FileDataReaderWriters.Npoi/Curiosity.FileDataReaderWriters.Npoi.csproj
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
|
||
<PackageId>Curiosity.FileDataReaderWriters.Npoi</PackageId> | ||
<Title>Curiosity.FileDataReaderWriters.Npoi</Title> | ||
<Description>Npoi realization for working with xls/xlsx/csv files</Description> | ||
<Summary>Single and multifile writing to xlsx file. Uses streaming version of workbook to reduce peak memory consumption.</Summary> | ||
<PackageTags>Curiosity; FileDataReaderWriters; Npoi; siisltd</PackageTags> | ||
<Language>English</Language> | ||
|
||
<AssemblyVersion>1.0.0</AssemblyVersion> | ||
<FileVersion>1.0.0</FileVersion> | ||
<PackageVersion>1.0.0</PackageVersion> | ||
|
||
<Authors>Max Markelow (@markeli), Andrei Vinogradov (@anri-vin), Andrey Ioch (@DevCorvette), Timur Sidoriuk (@shockthunder)</Authors> | ||
<Company>SIIS Ltd</Company> | ||
<Copyright>SIIS Ltd, 2024</Copyright> | ||
|
||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
|
||
<PackageProjectUrl>https://github.com/siisltd/Curiosity.Utils</PackageProjectUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/siisltd/Curiosity.Utils</RepositoryUrl> | ||
<PackageReleaseNotes>https://github.com/siisltd/Curiosity.Utils/tree/master/src/FileData/Curiosity.FileDataReaderWriters.Npoi/CHANGELOG.md</PackageReleaseNotes> | ||
|
||
<LangVersion>10</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>true</IsPackable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageIcon>siisltd.png</PackageIcon> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\..\siisltd.png" Pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Curiosity.FileDataReaderWriters\Curiosity.FileDataReaderWriters.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NPOI" Version="2.6.2" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DocumentationFile>bin\Release\Curiosity.FileDataReaderWriters.Npoi.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
</Project> |
191 changes: 191 additions & 0 deletions
191
src/FileData/Curiosity.FileDataReaderWriters.Npoi/NpoiXlsFileWriter.cs
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 |
---|---|---|
@@ -0,0 +1,191 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Curiosity.FileDataReaderWriters.Style; | ||
using Curiosity.FileDataReaderWriters.Writers; | ||
using ICSharpCode.SharpZipLib.Zip; | ||
using Microsoft.Extensions.Logging; | ||
using NPOI.SS.UserModel; | ||
using NPOI.XSSF.Streaming; | ||
|
||
namespace Curiosity.FileDataReaderWriters.Npoi; | ||
|
||
/// <summary> | ||
/// Класс для записи данных в xlsx / xls формате для NPOI | ||
/// </summary> | ||
public class NpoiXlsFileWriter : IFileWriter | ||
{ | ||
private int _currentRow = 0; | ||
private int _currentCell = 0; | ||
private readonly string _outputFilePath; | ||
private readonly ILogger _logger; | ||
|
||
private readonly SXSSFWorkbook _workbook; | ||
private readonly ISheet _sheet; | ||
private readonly ICellStyle _dataStyle; | ||
private FileStream _fileStream; | ||
|
||
private Dictionary<int, ICellStyle> _cellStyles = new(); | ||
|
||
/// <summary> | ||
/// Конструктор для записи эксель файла через NPOI | ||
/// </summary> | ||
/// <param name="outputFilePath">Адрес итогового файла</param> | ||
/// <param name="rowAccessWindowSize">Количество строк, находящихся в памяти писателя</param> | ||
public NpoiXlsFileWriter(string outputFilePath, ILogger logger, int rowAccessWindowSize = 100) | ||
{ | ||
_outputFilePath = outputFilePath; | ||
_logger = logger; | ||
_workbook = new SXSSFWorkbook(rowAccessWindowSize); | ||
_sheet = _workbook.CreateSheet(); | ||
|
||
//настройка формата даты для ячеек | ||
var format = _workbook.CreateDataFormat(); | ||
_dataStyle = _workbook.CreateCellStyle(); | ||
_dataStyle.DataFormat = format.GetFormat("yyyy-MM-dd HH:mm:ss"); | ||
} | ||
|
||
public int AddFormat(FormatSettings formatSettings) | ||
{ | ||
var font = _workbook.CreateFont(); | ||
font.IsBold = formatSettings.FontStyle.HasFlag(FontStyle.Bold); | ||
font.IsItalic = formatSettings.FontStyle.HasFlag(FontStyle.Italic); | ||
font.FontHeightInPoints = formatSettings.FontSize; | ||
|
||
var style = _workbook.CreateCellStyle(); | ||
style.SetFont(font); | ||
|
||
style.Alignment = formatSettings.TextAlignment switch | ||
{ | ||
TextAlignment.Left => HorizontalAlignment.Left, | ||
TextAlignment.Center => HorizontalAlignment.Center, | ||
TextAlignment.Right => HorizontalAlignment.Right, | ||
TextAlignment.Justify => HorizontalAlignment.Justify, | ||
_ => throw new ArgumentOutOfRangeException(nameof(formatSettings.TextAlignment)) | ||
}; | ||
|
||
style.WrapText = formatSettings.WrapText; | ||
|
||
var format = _workbook.CreateDataFormat(); | ||
style.DataFormat = format.GetFormat(formatSettings.DataFormat); | ||
|
||
var formatNumber = _cellStyles.Count; | ||
_cellStyles.Add(formatNumber, style); | ||
|
||
return formatNumber; | ||
} | ||
|
||
public int AddDefaultFormat() | ||
{ | ||
var formatNumber = _cellStyles.Count; | ||
_cellStyles.Add(formatNumber, _workbook.CreateCellStyle()); | ||
|
||
return formatNumber; | ||
} | ||
|
||
public void AddHeaders(IReadOnlyList<CellData> data) | ||
{ | ||
if (data is null) | ||
throw new ArgumentNullException(nameof(data)); | ||
|
||
for (var i = 0; i < data.Count; i++) | ||
{ | ||
var datum = data[i]; | ||
Append(datum.Value, datum.Format); | ||
} | ||
|
||
EndLine(); | ||
} | ||
|
||
public void AppendLine(IReadOnlyList<CellData> data) | ||
{ | ||
if (data == null) throw new ArgumentNullException(nameof(data)); | ||
|
||
for (var i = 0; i < data.Count; i++) | ||
{ | ||
var datum = data[i]; | ||
Append(datum.Value, datum.Format); | ||
} | ||
|
||
EndLine(); | ||
} | ||
|
||
public void Append(object? value, int? format = null) | ||
{ | ||
if (value is null || (value is string str && String.IsNullOrWhiteSpace(str))) | ||
{ | ||
_currentCell++; | ||
return; | ||
} | ||
|
||
var row = _sheet.GetRow(_currentRow) ?? _sheet.CreateRow(_currentRow); | ||
var cell = row.CreateCell(_currentCell); | ||
|
||
switch (value) | ||
{ | ||
case byte val: | ||
cell.SetCellValue(val); | ||
break; | ||
case short val: | ||
cell.SetCellValue(val); | ||
break; | ||
case int val: | ||
cell.SetCellValue(val); | ||
break; | ||
case long val: | ||
cell.SetCellValue(val); | ||
break; | ||
case float val: | ||
cell.SetCellValue(val); | ||
break; | ||
case double val: | ||
cell.SetCellValue(val); | ||
break; | ||
case decimal val: | ||
cell.SetCellValue(Convert.ToDouble(val)); | ||
break; | ||
case bool val: | ||
cell.SetCellValue(val); | ||
break; | ||
case IRichTextString val: | ||
cell.SetCellValue(val); | ||
break; | ||
case string val: | ||
cell.SetCellValue(val); | ||
break; | ||
case DateTime val: | ||
cell.SetCellValue(val); | ||
cell.CellStyle = _dataStyle; | ||
break; | ||
default: | ||
_logger.LogWarning("Используем обычный ToString() (Type={Type}, Value={Value})", value.GetType(), value); | ||
cell.SetCellValue(value.ToString()); | ||
break; | ||
} | ||
|
||
if (format is not null) | ||
cell.CellStyle = _cellStyles[(int)format]; | ||
|
||
_currentCell++; | ||
} | ||
|
||
public void EndLine() | ||
{ | ||
_currentRow++; | ||
_currentCell = 0; | ||
} | ||
|
||
public void Flush() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Записываем данные в файл и закрываем стрим. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
_fileStream = File.Open(_outputFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); | ||
_workbook.Write(_fileStream); | ||
_fileStream.Close(); | ||
} | ||
} |
Oops, something went wrong.