ClosedXML add image

早过忘川 提交于 2019-12-05 20:02:24

I little bit late here, but in case someone else comes looking I've added (limited) image support to ClosedXML.

Fork can be found at https://closedxml.codeplex.com/SourceControl/network/forks/ajwhiteway/ClosedXMLImageSupport

EDIT: Adding some detail to how it works.

New classes, XLPicture and XLMarker, have been added.

Picture can be created by

  XLPicture pic = new XLPicture
  {
    NoChangeAspect = true,
    NoMove = true,
    NoResize = true,
    ImageStream = fIn,
    Name = "Test Image"
  };

As of now it only accepts streams, not files, and it only spits out JPEGs. This can be changed if there is demand.

Once your picture is created you create a marker for where it goes

  XLMarker fMark = new XLMarker
  {
    ColumnId = 2,
    RowId = 2
  };
  pic.AddMarker(fMark);

If you add a single marker it will embed the image directly into the cell. If you add 2 markers it will span the image across the two markers.

Then to add it to the sheet just

 worksheet.AddPicture(pic);

Cheers guys.

ClosedXML now has basic image/picture support. As per https://github.com/ClosedXML/ClosedXML/wiki/How-can-I-insert-an-image :

using (var wb = new XLWorkbook())
{
  var ws = wb.AddWorksheet("Sheet1");

  var imagePath = @"c:\path\to\your\image.jpg";
  var image = ws.AddPicture(ImageLocation)
      .MoveTo(ws.Cell("B3").Address)
      .Scale(.5); // optional: resize picture

  wb.SaveAs("file.xlsx");
}

Where you able to solve it? We are facing a similar problem, we are bound to ClosedXML, inserting pictures will not be supported by ClosedXML unless someone volunteer to write the code... If you where able to solve it we would appreciate if you would share your solution! Regards, Benno

This feature has been included in closed XML from version 0.89.0

This code is for MVC5 but you can use it else where as well.

        public ActionResult Contact()
    {
        //Creating excelsheet and adding workbook with name Picture
        XLWorkbook workbook = new XLWorkbook();
        IXLWorksheet worksheet = workbook.Worksheets.Add("Picture");

        //Adjusting size of the excel cell as per requirement
        worksheet.Column(3).Width = 60;
        worksheet.Row(3).Height = 50;

        var imagePath = @"~/Images/Picture2.png";

        //Adding image to the worksheet and moving it to the cell
        var image = worksheet.AddPicture(Server.MapPath(imagePath))
            .MoveTo(worksheet.Cell(3, 3).Address);
        image.Name = "Logo";

        //Scaling down image as per our cell size
        image.ScaleWidth(.5);
        image.ScaleHeight(.3);

        //Formating the cell with border and color
        worksheet.Cell(3, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
        worksheet.Cell(3, 3).Style.Border.OutsideBorderColor = XLColor.Blue;

        //asving worksheet in memory stream
        MemoryStream stream = new MemoryStream();
        workbook.SaveAs(stream);
        stream.Position = 0;
        //returning the final excelsheet with name Picture
        return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        { FileDownloadName = "Pictures.xlsx" };
    }

You can refer to the link for detailed explaination. Hope it helps somebody

https://www.youtube.com/watch?v=8X0kg0YzI2g&t=242s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!