Добавьте сохраненное изображение в Excel с помощью npoi visual studio MVC Core. - PullRequest
0 голосов
/ 01 октября 2019

Здравствуйте. У меня есть код для загрузки истории транзакций с веб-сайта в Excel. У меня есть все стили и все работает, мне просто нужно добавить изображение в конце данных Excel. Данные являются динамическими, и то, что вы фильтруете на странице истории транзакций, будет показано, поэтому изображение должно находиться в конце данных после всех транзакций

 public async Task<IActionResult> ExportExcel(TransactionQueryParameter transactionQueryParameter)
    {
        transactionQueryParameter.PageSize = 100;

        var apiNecessaryData = CurrentClientInformation.Value;

        if (transactionQueryParameter.Date != null)
        {
            var currentCulture = CultureInfo.CurrentCulture;
            var currentCultureDateFormat = currentCulture.DateTimeFormat.ShortDatePattern;

            var dateTimes = transactionQueryParameter.Date
                .Replace(" ", "")
                .Replace("-", "|")
                .Split('|');

            DateTime ParseToDate(string dateString)
                => DateTime.ParseExact(dateString, currentCultureDateFormat, currentCulture);

            transactionQueryParameter.StartDate = ParseToDate(dateTimes[0]);
            transactionQueryParameter.EndDate = ParseToDate(dateTimes[1]);
        }

        var searchTransactionHistory =
            _mapper.Map<TransactionQueryParameter, SearchTransactionHistoryRequest>(transactionQueryParameter);



        int pageCount = 1;

        List<TransactionViewModel> transactions = new List<TransactionViewModel>();
        while (true)
        {
            searchTransactionHistory.Page = pageCount;

            var transactionResponse = await _apiClient.TransactionHistory_GetTransactionHistoryAsync(
            searchTransactionHistory,
            apiNecessaryData.AcceptLanguage,
            apiNecessaryData.SessionId,
            apiNecessaryData.IpAddress
            );
            var transactionList = _mapper.Map<IEnumerable<TransactionHistoryItemVm>, IReadOnlyList<ViewModels.TransactionViewModel>>
                (transactionResponse.Data.ToList());
            transactions.AddRange(transactionList);

            pageCount++;


            if (transactionList.Count < 100)
                break;
        }

        byte[] fileContents = null;
        using (var memoryStream = new MemoryStream())
        {
            XSSFWorkbook workbook;
            workbook = new XSSFWorkbook();
            using (FileStream file = new FileStream(@"wwwroot\ExternalResources\ExcelTemplate.xlsx", FileMode.Open, FileAccess.ReadWrite))
            {
                workbook = new XSSFWorkbook(file);
            }
            ISheet excelSheet = workbook.GetSheet("ტრანზაქციების ისტორია");
            //workbook.CreateSheet(ApplicationResources.Transaction_History_Excel_Name);
            //workbook.GetSheet("ტრანზაქციების ისტორია");
            IFont font1 = workbook.CreateFont();
            font1.IsBold = true;
            font1.FontHeightInPoints = 10;
            ICellStyle style1 = workbook.CreateCellStyle();
            ICellStyle style2 = workbook.CreateCellStyle();
            style2.SetFont(font1);
            style2.BorderLeft = BorderStyle.Thin;
            style2.BorderTop = BorderStyle.Thin;
            style2.BorderRight = BorderStyle.Thin;
            style2.BorderBottom = BorderStyle.Thin;
            style2.FillForegroundColor = IndexedColors.BrightGreen.Index;
            style2.FillPattern = FillPattern.SolidForeground;

            style1.SetFont(font1);
            style1.BorderLeft = BorderStyle.Thin;
            style1.BorderTop = BorderStyle.Thin;
            style1.BorderRight = BorderStyle.Thin;
            style1.BorderBottom = BorderStyle.Thin;



            int rowsCell = 5;
            IRow row = excelSheet.CreateRow(4);
            var rowFont = workbook.CreateFont();
            rowFont.FontName = "Calibri";

            row.CreateCell(0).SetCellValue(ApplicationResources.სტატუსი);
            row.Cells[0].CellStyle = style2;
            row.CreateCell(1).SetCellValue(ApplicationResources.თარიღი);
            row.Cells[1].CellStyle = style2;
            row.CreateCell(2).SetCellValue(ApplicationResources.Time);
            row.Cells[2].CellStyle = style2;
            row.CreateCell(3).SetCellValue(" ");
            row.Cells[3].CellStyle = style2;
            row.CreateCell(4).SetCellValue(ApplicationResources.პროდუქტი);
            row.Cells[4].CellStyle = style2;
            row.CreateCell(5).SetCellValue(ApplicationResources.Identificator);
            row.Cells[5].CellStyle = style2;
            row.CreateCell(6).SetCellValue(ApplicationResources.გადახდის_მეთოდი);
            row.Cells[6].CellStyle = style2;
            row.CreateCell(7).SetCellValue(ApplicationResources.სულ_თანხა);
            row.Cells[7].CellStyle = style2;
            row.CreateCell(8).SetCellValue(ApplicationResources.საკომისიო);
            row.Cells[8].CellStyle = style2;
            row.CreateCell(9).SetCellValue(ApplicationResources.ჯამური_თანხა);
            row.Cells[9].CellStyle = style2;



            for (int i = 0; i < transactions.Count; i++)
            {
                row = excelSheet.CreateRow(rowsCell);
                if (transactions[i].Status == "Successful")
                {
                    row.CreateCell(0).SetCellValue(ApplicationResources.Successful);
                }
                else if (transactions[i].Status == "Error")
                {
                    row.CreateCell(0).SetCellValue(ApplicationResources.Error);
                }
                else
                {
                    row.CreateCell(0).SetCellValue(ApplicationResources.პროგრესშია);
                }
                row.GetCell(0).CellStyle.SetFont(rowFont);
                row.Cells[0].CellStyle = style1;
                row.CreateCell(1).SetCellValue(transactions[i].PaymentDate);
                row.Cells[1].CellStyle = style1;
                row.CreateCell(2).SetCellValue(transactions[i].Hour);
                row.Cells[2].CellStyle = style1;
                row.CreateCell(3).SetCellValue(transactions[i].CashFlowDirection);
                row.Cells[3].CellStyle = style1;
                row.CreateCell(4).SetCellValue(transactions[i].ProcessingProductName);
                row.Cells[4].CellStyle = style1;
                row.CreateCell(5).SetCellValue(transactions[i].InvoiceData);
                row.Cells[5].CellStyle = style1;
                row.CreateCell(6).SetCellValue(transactions[i].Method);
                row.Cells[6].CellStyle = style1;
                row.CreateCell(7).SetCellValue(Convert.ToDouble(transactions[i].Amount));
                row.Cells[7].CellStyle = style1;
                row.CreateCell(8).SetCellValue(Convert.ToDouble(transactions[i].Commission));
                row.Cells[8].CellStyle = style1;
                row.CreateCell(9).SetCellValue(Convert.ToDouble(transactions[i].TotalAmount));
                row.Cells[9].CellStyle = style1;

                rowsCell++;

            }
            for (int i = 0; i <= 9; i++)
            {
                excelSheet.SetColumnWidth(i, 5000);
            }

            workbook.Write(memoryStream);
            fileContents = memoryStream.ToArray();

        }

        return File(fileContents, System.Net.Mime.MediaTypeNames.Application.Octet, ApplicationResources.Transaction_History_Excel_Name + "" + DateTime.Now + ".xlsx");

    }

Это весь метод в контроллере. Мое изображение хранится в wwwroot / img

...