CS2-74: Changed using NPOI library to ClosedXML

This commit is contained in:
KharchenkoVV 2021-09-23 14:36:05 +05:00
parent 04d6b1c1a7
commit 9d609ed402
3 changed files with 32 additions and 18 deletions

View File

@ -57,15 +57,14 @@ namespace AsbCloudInfrastructure.Services
.Where(f => f.Name != resultFileName) .Where(f => f.Name != resultFileName)
.Select(f => fileService.GetUrl(f)); .Select(f => fileService.GetUrl(f));
var resultExcelPath = Path.Combine(fileService.RootPath, var tempResultFilePath = Path.Combine(Path.GetTempPath(), "drillingProgram", resultFileName);
$"{idWell}", $"{idFileCategoryDrillingProgram}",
resultFileName);
UniteExcelFiles(fileNames, resultExcelPath); UniteExcelFiles(fileNames, tempResultFilePath);
return await fileService.SaveAsync(idWell, null, idFileCategoryDrillingProgram, var fileInfo = await fileService.MoveAsync(idWell, null, idFileCategoryDrillingProgram,
resultExcelPath, null, token) resultFileName, tempResultFilePath, token).ConfigureAwait(false);
.ConfigureAwait(false);
return fileInfo;
} }
private static void UniteExcelFiles(IEnumerable<string> excelFilesNames, string resultExcelPath) private static void UniteExcelFiles(IEnumerable<string> excelFilesNames, string resultExcelPath)
@ -74,12 +73,25 @@ namespace AsbCloudInfrastructure.Services
const int maxAllowedColumns = 256; const int maxAllowedColumns = 256;
foreach (var excelFileName in excelFilesNames) var filteredFileNames = excelFilesNames.Distinct();
foreach (var excelFileName in filteredFileNames)
{ {
using var sourceExcelFile = new XLWorkbook(excelFileName, XLEventTracking.Disabled); using var sourceExcelFile = new XLWorkbook(excelFileName, XLEventTracking.Disabled);
foreach (var sheet in sourceExcelFile.Worksheets) foreach (var sheet in sourceExcelFile.Worksheets)
{ {
var newSheetName = sheet.Name;
int index = 1;
if (resultExcelFile.Worksheets.Contains(newSheetName))
{
var suffix = $"_{index++}";
if (newSheetName.Length + suffix.Length >= 31)
newSheetName = sheet.Name.Substring(0, (31 - suffix.Length));
newSheetName += suffix;
}
var imagesInfos = sheet.Pictures.Select(p => new ImageInfo var imagesInfos = sheet.Pictures.Select(p => new ImageInfo
{ {
Id = p.Id, Id = p.Id,
@ -93,7 +105,7 @@ namespace AsbCloudInfrastructure.Services
if (sheet.Columns().Count() > maxAllowedColumns) if (sheet.Columns().Count() > maxAllowedColumns)
{ {
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name); var resultSheet = resultExcelFile.Worksheets.Add(newSheetName);
var rngData = GetCellsRange(sheet); var rngData = GetCellsRange(sheet);
@ -114,7 +126,7 @@ namespace AsbCloudInfrastructure.Services
{ {
RemovePicturesFromSheet(sheet); RemovePicturesFromSheet(sheet);
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name); var resultSheet = sheet.CopyTo(resultExcelFile, newSheetName);
CopyImagesToAnotherSheet(imagesInfos, resultSheet); CopyImagesToAnotherSheet(imagesInfos, resultSheet);
} }

View File

@ -29,13 +29,14 @@ namespace AsbCloudInfrastructure.Services
.ThenInclude(c => c.CompanyType); .ThenInclude(c => c.CompanyType);
} }
public async Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFileFullName, CancellationToken token = default) public async Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFilePath, CancellationToken token = default)
{ {
srcFileFullName = Path.GetFullPath(srcFileFullName); destinationFileName = Path.GetFileName(destinationFileName);
if (!File.Exists(srcFileFullName)) srcFilePath = Path.GetFullPath(srcFilePath);
throw new ArgumentException($"file {srcFileFullName} doesn't exist", nameof(srcFileFullName)); if (!File.Exists(srcFilePath))
throw new ArgumentException($"file {srcFilePath} doesn't exist", nameof(srcFilePath));
var sysFileInfo = new System.IO.FileInfo(srcFileFullName); var sysFileInfo = new System.IO.FileInfo(srcFilePath);
//save info to db //save info to db
var fileInfo = new AsbCloudDb.Model.FileInfo() var fileInfo = new AsbCloudDb.Model.FileInfo()
@ -43,7 +44,7 @@ namespace AsbCloudInfrastructure.Services
IdWell = idWell, IdWell = idWell,
IdAuthor = idUser, IdAuthor = idUser,
IdCategory = idCategory, IdCategory = idCategory,
Name = Path.GetFileName(destinationFileName), Name = destinationFileName,
UploadDate = DateTime.Now, UploadDate = DateTime.Now,
IsDeleted = false, IsDeleted = false,
Size = sysFileInfo.Length, Size = sysFileInfo.Length,
@ -53,7 +54,8 @@ namespace AsbCloudInfrastructure.Services
await db.SaveChangesAsync(token).ConfigureAwait(false); await db.SaveChangesAsync(token).ConfigureAwait(false);
var fileId = entry.Entity.Id; var fileId = entry.Entity.Id;
string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId); string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId);
File.Move(srcFileFullName, filePath); Directory.CreateDirectory(Path.GetDirectoryName(filePath));
File.Move(srcFilePath, filePath);
var dto = entry.Entity.Adapt<FileInfoDto>(); var dto = entry.Entity.Adapt<FileInfoDto>();
return dto; return dto;

View File

@ -7,7 +7,7 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Host=192.168.1.70;Database=postgres;Username=postgres;Password=q;Persist Security Info=True", "DefaultConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True",
"ServerConnection": "Host=192.168.1.70;Database=postgres;Username=postgres;Password=q;Persist Security Info=True", "ServerConnection": "Host=192.168.1.70;Database=postgres;Username=postgres;Password=q;Persist Security Info=True",
"LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True" "LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True"
}, },