dev
dengyu 3 months ago
parent 2e7e2ae9b6
commit f8cb21b5c8

@ -0,0 +1,179 @@
using DS.Module.Core;
using DS.Module.ExcelModule.Model;
using LanguageExt.ClassInstances;
using LanguageExt;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using MiniExcelLibs;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.SS.UserModel;
using OfficeOpenXml;
using System.Data;
using Spire.Xls;
namespace DS.Module.ExcelModule
{
public class EPPlusService : IEPPlusService
{
private readonly IServiceProvider _serviceProvider;
private static string GetString(object obj)
{
try
{
if (obj == null) return "";
return obj.ToString();
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
///将指定的Excel的文件转换成DataTable Excel的第一个sheet
/// </summary>
/// <param name="fullFielPath">文件的绝对路径</param>
/// <returns></returns>
public DataTable WorksheetToTable(string fullFielPath, int SheetIndex = 1)
{
try
{
string[] PathNames = fullFielPath.Split(new string[] { @"/" }, StringSplitOptions.RemoveEmptyEntries);
var ExcelFileName = PathNames[PathNames.Length - 1];
if (ExcelFileName.EndsWith(".xls"))
{
var newExcelFileName = PathNames[PathNames.Length - 1].Replace(".xls", ".xlsx");
var _newFileFullname = Path.GetDirectoryName(fullFielPath) + "/" + newExcelFileName;
Workbook book = new Workbook();
book.LoadFromFile(fullFielPath);
book.SaveToFile(_newFileFullname, ExcelVersion.Version2013);
fullFielPath = _newFileFullname;
}
FileInfo existingFile = new FileInfo(fullFielPath);
ExcelPackage package = new ExcelPackage(existingFile);
ExcelWorksheet worksheet = package.Workbook.Worksheets[SheetIndex];//选定 指定页
return WorksheetToTable(worksheet);
}
catch (Exception e)
{
throw;
}
}
public DataTable WorksheetToTable_NoTitle(string fullFielPath, int SheetIndex = 1)
{
try
{
string[] PathNames = fullFielPath.Split(new string[] { @"/" }, StringSplitOptions.RemoveEmptyEntries);
var ExcelFileName = PathNames[PathNames.Length - 1];
if (ExcelFileName.EndsWith(".xls"))
{
var newExcelFileName = PathNames[PathNames.Length - 1].Replace(".xls", ".xlsx");
var _newFileFullname = Path.GetDirectoryName(fullFielPath) + "/" + newExcelFileName;
Workbook book = new Workbook();
book.LoadFromFile(fullFielPath);
book.SaveToFile(_newFileFullname, ExcelVersion.Version2013);
fullFielPath = _newFileFullname;
}
FileInfo existingFile = new FileInfo(fullFielPath);
ExcelPackage package = new ExcelPackage(existingFile);
ExcelWorksheet worksheet = package.Workbook.Worksheets[SheetIndex];//选定 指定页
return WorksheetToTable_NoTitle(worksheet);
}
catch (Exception e)
{
throw;
}
}
/// <summary>
/// 将worksheet转成datatable
/// </summary>
/// <param name="worksheet">待处理的worksheet</param>
/// <returns>返回处理后的datatable</returns>
public DataTable WorksheetToTable(ExcelWorksheet worksheet)
{
//获取worksheet的行数
int rows = worksheet.Dimension.End.Row;
//获取worksheet的列数
int cols = worksheet.Dimension.End.Column;
DataTable dt = new DataTable(worksheet.Name);
DataRow dr = null;
for (int i = 1; i <= rows; i++)
{
if (i > 1)
dr = dt.Rows.Add();
for (int j = 1; j <= cols; j++)
{
//默认将第一行设置为datatable的标题
if (i == 1)
dt.Columns.Add(GetString(worksheet.Cells[i, j].Value));
//剩下的写入datatable
else
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
}
return dt;
}
public DataTable WorksheetToTable_NoTitle(ExcelWorksheet worksheet)
{
//获取worksheet的行数
int rows = worksheet.Dimension.End.Row;
//获取worksheet的列数
int cols = worksheet.Dimension.End.Column;
DataTable dt = new DataTable(worksheet.Name);
DataRow dr = null;
for (int i = 1; i <= rows; i++)
{
//if (i > 1)
dr = dt.Rows.Add();
for (int j = 1; j <= cols; j++)
{
//默认将第一行设置为datatable的标题
if (i == 1)
{
dt.Columns.Add(GetString(worksheet.Cells[i, j].Value));
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
//剩下的写入datatable
else
{
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
}
}
return dt;
}
}
}

@ -56,6 +56,7 @@ namespace DS.Module.ExcelModule
return DataResult<string>.Success(path);
}
public MemoryStream ExportExcelStreamByColumn(ExportByColumnReq req)
{
var filename = Guid.NewGuid() + ".xlsx";

@ -0,0 +1,19 @@
using DS.Module.Core;
using DS.Module.ExcelModule.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DS.Module.ExcelModule
{
public interface IEPPlusService
{
public DataTable WorksheetToTable(string fullFielPath, int SheetIndex = 1);
public DataTable WorksheetToTable_NoTitle(string fullFielPath, int SheetIndex = 1);
}
}

@ -15,6 +15,7 @@
<PackageReference Include="DotNetCore.CAP" Version="8.0.0" />
<PackageReference Include="DotNetCore.CAP.InMemoryStorage" Version="8.0.0" />
<PackageReference Include="Enums.NET" Version="5.0.0" />
<PackageReference Include="EPPlus" Version="7.3.1" />
<PackageReference Include="fasterflect" Version="3.0.0" />
<PackageReference Include="GZY.Quartz.MUI" Version="2.6.0" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.14" />
@ -50,6 +51,7 @@
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="Snowflake.Core" Version="2.0.0" />
<PackageReference Include="Snowflake.Data" Version="2.2.0" />
<PackageReference Include="Spire.XLS" Version="14.9.1" />
<PackageReference Include="SqlSugar.IOC" Version="2.0.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.167" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />

@ -17,7 +17,11 @@ using DS.WMS.Core.Sys.Entity;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using NPOI.HPSF;
using SqlSugar;
using DS.Module;
using DS.Module.ExcelModule;
using DS.Module.ExcelModule.Model;
namespace DS.WMS.ContainerManagement.Info.Method;
@ -27,6 +31,7 @@ public class CM_BaseInfoService : ICM_BaseInfoService
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
private readonly IEPPlusService epplusService;
/// <summary>
///
@ -38,6 +43,7 @@ public class CM_BaseInfoService : ICM_BaseInfoService
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
epplusService = _serviceProvider.GetRequiredService<IEPPlusService>();
}
/// <summary>
@ -264,6 +270,11 @@ public class CM_BaseInfoService : ICM_BaseInfoService
/// <returns></returns>
public DataResult CM_DealExcel(CM_DealExcelReq req)
{
var _dt = epplusService.WorksheetToTable("excel文件地址");
return DataResult.Successed($"调用成功!{JsonConvert.SerializeObject(req)}", MultiLanguageConst.DataUpdateSuccess);
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
@ -280,6 +291,8 @@ public class CM_BaseInfoService : ICM_BaseInfoService
{
return DataResult.Successed($"", MultiLanguageConst.DataUpdateSuccess);
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);

@ -45,6 +45,7 @@
<ProjectReference Include="..\DS.Module.Core\DS.Module.Core.csproj" />
<ProjectReference Include="..\DS.Module.DjyRulesEngine\DS.Module.DjyRulesEngine.csproj" />
<ProjectReference Include="..\DS.Module.DjyServiceStatus\DS.Module.DjyServiceStatus.csproj" />
<ProjectReference Include="..\DS.Module.ExcelModule\DS.Module.ExcelModule.csproj" />
<ProjectReference Include="..\DS.Module.MQ\DS.Module.MQ.csproj" />
<ProjectReference Include="..\DS.Module.Nuget\DS.Module.Nuget.csproj" />
<ProjectReference Include="..\DS.Module.PrintModule\DS.Module.PrintModule.csproj" />

Loading…
Cancel
Save