excel daochu

optimize
wet 2 years ago
parent ff367ea1bb
commit e6cee7cf94

@ -59,6 +59,8 @@ using System.Text.RegularExpressions;
using MathNet.Numerics.Distributions;
using Microsoft.IdentityModel.Tokens;
using Myshipping.Application.EDI.ESL;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace Myshipping.Application
{
@ -93,6 +95,7 @@ namespace Myshipping.Application
private readonly SqlSugarRepository<BookingStatus> _repBookingStatus;
private readonly SqlSugarRepository<BookingEDIExt> _bookingEDIExt;
private readonly SqlSugarRepository<DjyUserConfig> _repUserConfig;
private readonly SqlSugarRepository<BookingExcelTemplate> _excelrep;
private readonly IHttpContextAccessor _httpContextAccessor;
const string CONST_MAPPING_MODULE = "BOOK_OR_CLOSING";
const string CONST_MAPPING_MODULE_ROUTE = "BOOK_OR_CLOSING_RT";
@ -105,7 +108,8 @@ namespace Myshipping.Application
ILogger<BookingOrderService> logger, ISysCacheService cache, IDjyWebsiteAccountConfigService webAccountConfig, ISysDataUserMenu right,
SqlSugarRepository<BookingPrintTemplate> repPrintTemplate, SqlSugarRepository<BookingLetteryard> repLetterYard, SqlSugarRepository<SysUser> repUser,
SqlSugarRepository<BookingOrderUrl> repOrderUrl, SqlSugarRepository<BookingOrderContact> repOrderContact, SqlSugarRepository<BookingSampleBill> repSampleBill,
SqlSugarRepository<DjyUserMailAccount> repUserMail, SqlSugarRepository<SysTenant> repTenant, SqlSugarRepository<BookingStatus> repBookingStatus, SqlSugarRepository<BookingEDIExt> bookingEDIExt,
SqlSugarRepository<BookingExcelTemplate> excelrep, SqlSugarRepository<DjyUserMailAccount> repUserMail,
SqlSugarRepository<SysTenant> repTenant, SqlSugarRepository<BookingStatus> repBookingStatus, SqlSugarRepository<BookingEDIExt> bookingEDIExt,
IHttpContextAccessor httpContextAccessor)
{
this._logger = logger;
@ -133,6 +137,7 @@ namespace Myshipping.Application
this._repBookingStatus = repBookingStatus;
this._bookingEDIExt = bookingEDIExt;
this._repUserConfig = repUserConfig;
this._excelrep = excelrep;
_httpContextAccessor = httpContextAccessor;
}
@ -1712,7 +1717,55 @@ namespace Myshipping.Application
}
else if (printType == BookingPrintTemplateType.ExcelTemplate)
{
//todo:excel模板打印功能
var opt = App.GetOptions<PrintTemplateOptions>();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var fileAbsPath = Path.Combine(dirAbs, printTemplate.FilePath);
_logger.LogInformation($"准备调用EXCEL生成id{bookingId},文件:{printTemplate.FileName}");
var result = new FileStream(fileAbsPath, FileMode.Open);
var excelwork = new HSSFWorkbook(result);
var sheet = excelwork.GetSheetAt(0);
var order = await _rep.AsQueryable().Where(x => x.Id == bookingId).FirstAsync();
var entity = await _excelrep.AsQueryable().Where(x => x.Pid == templateId).OrderBy(x => x.Row).ToListAsync();
//单元格样式
var cellStyle = NpoiExcelExportHelper._.CreateStyle(excelwork, HorizontalAlignment.Center, VerticalAlignment.Center, 10, true, 0);
for (int _row = 0; _row < entity.Max(x => x.Row); _row++)
{
if (entity.Where(x => x.Row == (_row + 1)).Count() > 0)
{
///创建行
var row = NpoiExcelExportHelper._.CreateRow(sheet, _row, 25);
for (int _cellNum = 0; _cellNum < entity.Max(x => x.Column); _cellNum++)
{
if (entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Count() > 0)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(order))
{
var name = entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Select(x => x.Field).FirstOrDefault();
var _name = descriptor.Name.ToLower();
if (name == _name)
{
var value = descriptor.GetValue(order).ToString();
var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, _cellNum + 1, value);
}
}
}
}
}
}
var fileFullPath = Path.Combine(App.WebHostEnvironment.WebRootPath, App.GetOptions<TempFileOptions>().Path);//服务器路径
if (!Directory.Exists(fileFullPath))
{
Directory.CreateDirectory(fileFullPath);
}
fileName = string.Format("订舱{0}.xls", DateTime.Now.Ticks);
_logger.LogInformation("导出excel:" + Path.Combine(fileFullPath, fileName));
var filestream = new FileStream(Path.Combine(fileFullPath, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite);
excelwork.Write(filestream);
}
//记录打印次数和时间,用于前端动态展示常用的打印类型

@ -439,7 +439,7 @@ namespace Myshipping.Application
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("/BookingTemplate/AddOrUpdateExcelTemplate")]
public async Task<dynamic> AddOrUpdateExcelTemplate(BookingExcelTemplateDto dto)
public async Task AddOrUpdateExcelTemplate(BookingExcelTemplateDto dto)
{
@ -450,7 +450,6 @@ namespace Myshipping.Application
await _excelrep.InsertAsync(entity);
}
return null;
}
/// <summary>
/// 获取excel模板详情

@ -40,7 +40,7 @@ namespace Myshipping.Application
public BookingTemplateService(SqlSugarRepository<BookingTemplate> rep, SqlSugarRepository<BookingOrder> repOrder, SqlSugarRepository<BookingPrintTemplate> repPrintTemplate, ILogger<BookingTemplate> logger)
{
_excelrep = excelrep;
_repOrder = repOrder;
_rep = rep;
@ -123,97 +123,6 @@ namespace Myshipping.Application
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
/// <summary>
/// 导出excel
/// </summary>
/// <param name="bookingId"></param>
/// <param name="excelTemplateId"></param>
/// <param name="printTemplateId"></param>
/// <returns></returns>
[HttpGet("/BookingTemplate/ExportExcel")]
public async Task<dynamic> ExportExcel([FromQuery] long bookingId, long excelTemplateId,long printTemplateId)
{
if (bookingId == 0 || excelTemplateId == 0|| printTemplateId==0)
{
throw Oops.Bah("请传入正确参数");
}
var printTemplate = await _repPrintTemplate.AsQueryable().Filter(null, true).FirstAsync(x => x.Id== printTemplateId);
if (printTemplate == null)
{
throw Oops.Bah(BookingErrorCode.BOOK115);
}
var opt = App.GetOptions<PrintTemplateOptions>();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var fileAbsPath = Path.Combine(dirAbs, printTemplate.FilePath);
_logger.LogInformation($"查找EXCEL模板文件{fileAbsPath}");
if (!File.Exists(fileAbsPath))
{
throw Oops.Bah(BookingErrorCode.BOOK115);
}
_logger.LogInformation($"准备调用EXCEL生成id{bookingId},文件:{printTemplate.FileName}");
var result = new FileStream(fileAbsPath, FileMode.Open) ;
var excelwork = new HSSFWorkbook(result);
var sheet = excelwork.GetSheetAt(0);
var order = await _repOrder.AsQueryable().Where(x => x.Id == bookingId).FirstAsync();
var entity = await _excelsubrep.AsQueryable().Where(x => x.Pid == excelTemplateId).OrderBy(x => x.Row).ToListAsync();
//单元格样式
var cellStyle = NpoiExcelExportHelper._.CreateStyle(excelwork, HorizontalAlignment.Center, VerticalAlignment.Center, 10, true, 0);
for (int _row = 0; _row < entity.Max(x => x.Row); _row++)
{
if (entity.Where(x => x.Row == (_row + 1)).Count() > 0)
{
///创建行
var row = NpoiExcelExportHelper._.CreateRow(sheet, _row, 25);
for (int _cellNum = 0; _cellNum < entity.Max(x => x.Column); _cellNum++)
{
if (entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Count() > 0)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(order))
{
var name = entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Select(x => x.Field).FirstOrDefault();
var _name = descriptor.Name.ToLower();
if (name == _name)
{
var value = descriptor.GetValue(order).ToString();
var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, _cellNum + 1, value);
}
}
}
}
}
}
var fileFullPath = Path.Combine(App.WebHostEnvironment.WebRootPath, App.GetOptions<TempFileOptions>().Path);//服务器路径
if (!Directory.Exists(fileFullPath))
{
Directory.CreateDirectory(fileFullPath);
}
var fileName = string.Format("订舱{0}.xls", DateTime.Now.Ticks);
_logger.LogInformation("导出excel:" + Path.Combine(fileFullPath, fileName));
var filestream = new FileStream(Path.Combine(fileFullPath, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite);
excelwork.Write(filestream);
//var result = new FileStreamResult(new FileStream(Path.Combine(fileFullPath, fileName), FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
return fileName;
}
}
}

Loading…
Cancel
Save