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)
.Select(f => fileService.GetUrl(f));
var resultExcelPath = Path.Combine(fileService.RootPath,
$"{idWell}", $"{idFileCategoryDrillingProgram}",
resultFileName);
var tempResultFilePath = Path.Combine(Path.GetTempPath(), "drillingProgram", resultFileName);
UniteExcelFiles(fileNames, resultExcelPath);
UniteExcelFiles(fileNames, tempResultFilePath);
return await fileService.SaveAsync(idWell, null, idFileCategoryDrillingProgram,
resultExcelPath, null, token)
.ConfigureAwait(false);
var fileInfo = await fileService.MoveAsync(idWell, null, idFileCategoryDrillingProgram,
resultFileName, tempResultFilePath, token).ConfigureAwait(false);
return fileInfo;
}
private static void UniteExcelFiles(IEnumerable<string> excelFilesNames, string resultExcelPath)
@ -74,12 +73,25 @@ namespace AsbCloudInfrastructure.Services
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);
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
{
Id = p.Id,
@ -93,7 +105,7 @@ namespace AsbCloudInfrastructure.Services
if (sheet.Columns().Count() > maxAllowedColumns)
{
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name);
var resultSheet = resultExcelFile.Worksheets.Add(newSheetName);
var rngData = GetCellsRange(sheet);
@ -114,7 +126,7 @@ namespace AsbCloudInfrastructure.Services
{
RemovePicturesFromSheet(sheet);
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
var resultSheet = sheet.CopyTo(resultExcelFile, newSheetName);
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
}

View File

@ -29,13 +29,14 @@ namespace AsbCloudInfrastructure.Services
.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);
if (!File.Exists(srcFileFullName))
throw new ArgumentException($"file {srcFileFullName} doesn't exist", nameof(srcFileFullName));
destinationFileName = Path.GetFileName(destinationFileName);
srcFilePath = Path.GetFullPath(srcFilePath);
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
var fileInfo = new AsbCloudDb.Model.FileInfo()
@ -43,7 +44,7 @@ namespace AsbCloudInfrastructure.Services
IdWell = idWell,
IdAuthor = idUser,
IdCategory = idCategory,
Name = Path.GetFileName(destinationFileName),
Name = destinationFileName,
UploadDate = DateTime.Now,
IsDeleted = false,
Size = sysFileInfo.Length,
@ -53,7 +54,8 @@ namespace AsbCloudInfrastructure.Services
await db.SaveChangesAsync(token).ConfigureAwait(false);
var fileId = entry.Entity.Id;
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>();
return dto;

View File

@ -7,7 +7,7 @@
}
},
"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",
"LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True"
},