诗情寻知己
揽几缕、轻挽起,暮暮朝朝与君语。

控制器添加内容

public class BookingController : BaseController
{
        private readonly IBookingService _bookingService;
        private readonly IHostingEnvironment _hostingEnvironment;//获取站内文件路径

        public BookingController(IBookingService bookingService
            IHostingEnvironment hostingEnvironment,IHttpContextAccessor httpContext)
        {
            _bookingService = bookingService;
            _httpContext = httpContext;
            _hostingEnvironment = hostingEnvironment;
        }
   //控制器获取上传文件 [HttpPost]
public async Task<AjaxResult<string>> ImportBooking([FromForm] IFormFile file) { try { AjaxResult<string> result = new AjaxResult<string>(); string[] LimitPictureType = { ".XLS", ".XLSX"}; var fileExtension = Path.GetExtension(file.FileName); string currentPictureExtension = fileExtension.ToUpper(); if (LimitPictureType.Contains(currentPictureExtension)) { string contentRootPath = _hostingEnvironment.ContentRootPath; string filePath = Path.Combine(contentRootPath, "License", "Aspose.Total.lic"); //添加license Aspose.Cells.License license = new Aspose.Cells.License(); license.SetLicense(filePath); result = await _bookingService.ImportBooking(file.OpenReadStream(), UserCode); } else { result.Code = ResultCodeEnum.ERROR; result.Message = "上传的文件格式错误"; } return result; } catch (Exception ex) { throw new Exception("FileUpAvatar", ex); } } }

 

服务方法编写逻辑

public async Task<AjaxResult<string>> ImportBooking(Stream file, string UserCode)
{
       AjaxResult<string> result = new AjaxResult<string>();
       try
       {
                Workbook wb = new Workbook(file);
                Worksheet bookSheet = wb.Worksheets[0];
                for (var i = 0; i < bookSheet.Cells.Rows.Count; i++)
                {
                    for (int j = 0; j < bookSheet.Cells.Columns.Count; j++)
                    {
                        var getvalue = bookSheet.Cells[i, j].StringValue;
                    }
                }
        }
        catch (Exception ex)
        {
             LogHelper.Error("ImportBooking", ex);
        }
        return result;
 }

 

posted on 2023-02-01 11:00  诗情寻知己  阅读(84)  评论(0编辑  收藏  举报