Проблема с добавлением изображения штрих-кода в Epplus

#c# #.net #excel #asp.net-mvc #epplus

#c# #.net #excel #asp.net-mvc #epplus

Вопрос:

Я пытаюсь вставить штрих-код в ячейку в Excel с помощью Epplus.

Это ASP.NET Проект MVC.

используя следующий код.

 using System.IO;
using System.Web.Mvc;
using EmployeeIDBarcodeGeneration.Models;
using OfficeOpenXml;
using ZXing;   
  
 public void ExportExcel()
        {
            List<EmployeeID> EmployeeIDs = new List<EmployeeID> {
                new EmployeeID { EmployeeName="Employee A",EmployeeIDnumber="A222"},
                new EmployeeID { EmployeeName ="Employee B", EmployeeIDnumber = "A333" },
                new EmployeeID { EmployeeName ="Employee C", EmployeeIDnumber = "A444" }
            };

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            ExcelPackage pck = new ExcelPackage();
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Report");

            ws.Cells["A1"].Value = "EmployeeName";
            ws.Cells["B1"].Value = "EmployeeIDnumber";
            ws.Cells["C1"].Value = "Barcode";
          
            int rowStart = 2;
            BarcodeWriter bw = new BarcodeWriter();
            bw.Format = BarcodeFormat.CODE_39;
            bw.Options.Width = 200;
            bw.Options.Height = 100;
            bw.Options.PureBarcode = false;
           
            foreach (var item in EmployeeIDs)
            {
                ws.Cells[string.Format("A{0}", rowStart)].Value = item.EmployeeName;
                ws.Cells[string.Format("B{0}", rowStart)].Value = item.EmployeeIDnumber;
                Bitmap bitmap = bw.Write(item.EmployeeIDnumber);
                var picture = ws.Drawings.AddPicture(item.EmployeeIDnumber, bitmap);
                ws.Cells[string.Format("C{0}", rowStart)].Value = picture;
                rowStart  ;               
            }
            ws.Cells["A:AZ"].AutoFitColumns();
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AppendHeader("content-disposition", "attachment;filename="   "ExcelReport.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
            Response.End();
        }
  

Но изображение штрих-кода не может быть правильно размещено в ячейке.

Что мне делать?

Спасибо!!

Ответ №1:

Вам нужно установить положение диаграммы:

 picture.SetPosition(rowStart-1, 0, 2, 0);
  

Вот полный код:

 public void DownloadExcelFile()
        {
            List<EmployeeID> EmployeeIDs = new List<EmployeeID> {
                new EmployeeID { EmployeeName="Employee A",EmployeeIDnumber="A222"},
                new EmployeeID { EmployeeName ="Employee B", EmployeeIDnumber = "A333" },
                new EmployeeID { EmployeeName ="Employee C", EmployeeIDnumber = "A444" }
            };

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            ExcelPackage pck = new ExcelPackage();
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Report");

            ws.Cells["A1"].Value = "EmployeeName";
            ws.Cells["B1"].Value = "EmployeeIDnumber";
            ws.Cells["C1"].Value = "Barcode";

            int rowStart = 2;
            BarcodeWriter bw = new BarcodeWriter();
            bw.Format = BarcodeFormat.CODE_39;
            bw.Options.Width = 200;
            bw.Options.Height = 40;
            bw.Options.PureBarcode = false;

            foreach (var item in EmployeeIDs)
            {
                string STsrc = "/Home/BarCodeImg?code="   item.EmployeeIDnumber   "amp;width=300amp;height=50";
                ws.Cells[string.Format("A{0}", rowStart)].Value = item.EmployeeName;
                ws.Cells[string.Format("B{0}", rowStart)].Value = item.EmployeeIDnumber;
                Bitmap bitmap = bw.Write(item.EmployeeIDnumber);
                var picture = ws.Drawings.AddPicture(item.EmployeeIDnumber, bitmap);
                picture.SetPosition(rowStart-1, 0, 2, 0);
                rowStart  ;
            }
            ws.Cells["A:AZ"].AutoFitColumns();
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AppendHeader("content-disposition", "attachment;filename="   "ExcelReport.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
            Response.End();

        }