Hello,
is it possible to put an image dynamically into a cell of an excel sheet which I want to convert later to a pdf file?
Like this:
Bitmap image = new Bitmap(“test.png”);
cells[1, 1].PutValue(image);
But the output in the cell is just the type string “System.Drawing.Bitmap”.
I use Aspose.Cells for .NET v7.2.0.1
Regards
Andreas
Hi,
Thanks for your posting and using Aspose.Cells for .NET.
Please download and use the latest version:
Aspose.Cells
for .NET v7.2.2.3
First you will read bytes of your images, then you will create a memory stream object from bytes.
Then you will insert the image using the Pictures.Add() method.
Please see the following code. I have attached the output pdf and screenshot for your reference.
C#
byte[] imgBytes = File.ReadAllBytes(imgPath);
MemoryStream ms = new MemoryStream();
ms.Write(imgBytes, 0, imgBytes.Length);
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Pictures.Add(1, 1, ms);
PdfSaveOptions opts = new PdfSaveOptions();
opts.OnePagePerSheet = true;
workbook.Save("output.pdf");
Hopefully, it will be helpful for you. Let us know your feedback
Screenshot:
Thank you very much, it works.
Hi,
Hello,
https://localhost/DCFileService/Library/Firm_1/Logo/Thumb/DealCloudPNG634608601930498149.png
"Hi,
Hi,
var webClient = new WebClient();byte[] imgBytes = webClient.DownloadData("http://cdn.aspose.com/homepage/images/logo.png");MemoryStream ms = new MemoryStream();ms.Write(imgBytes, 0, imgBytes.Length);Workbook workbook = new Workbook();Worksheet worksheet = workbook.Worksheets[0];worksheet.Pictures.Add(1, 1, ms);workbook.Save("K://pic.xlsx");
Hello,
Hi Ashish,
Hello,
Hi,
<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>string logoPath = “http://www.aspose.com/Images/aspose-logo.jpg
”;<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>var webClient = new WebClient();<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>byte[] imgBytes =
webClient.DownloadData(logoPath);<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>MemoryStream ms = new
MemoryStream();<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>ms.Write(imgBytes, 0, imgBytes.Length);<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>Workbook workbook = new Workbook();<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>Worksheet worksheet =
workbook.Worksheets[0];<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>Aspose.Cells.Drawing.Picture
picture =<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”><span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>workbook.Worksheets[0].Shapes.AddPicture(0, 0, ms, 100, 100);
workbook.Save(@“e:\outLinkedPicture.xlsx”);
I have tried adding picture as per your mentioned method like below.
Hi Ashish,
My Issue has been resolved. I bind image first into top left cell and then I called worksheet.AutoFitColumns at end. that was the only reason. Now I have made columns AutoFit except the column in which image is being rendered.
Hi,
Thanks for sharing your workaround.
You can also Autofit all columns first and then reset the width and height of the column and row of the cell where you placed the image.
The code below inserts a picture inside some cell and adjust the cell’s height and width accordingly.
I have attached the input image, output xls file and screenshot for your reference.
C#
{
//Set the height of the first row
Sheet.Cells.SetRowHeightPixel(Row, Height); //height equals to picture height
//Set the width of the first and second column
Sheet.Cells.SetColumnWidthPixel(Column, Width);
//Add a picture inside cell at this row and column
int picId = Sheet.Pictures.Add(Row, Column, PicPath);
Picture pic = Sheet.Pictures[picId];<br>
//Set the height and width of picture
pic.Height = Height;
pic.Width = Width;
}
public static void Run()
{
//Create a workbook
Workbook workbook = new Workbook();
//Access first sheet
Worksheet worksheet = workbook.Worksheets[0];
//Add a picture inside a cell
AddPicture(worksheet, 4, 4, 300, 200, @"f:\downloads\image-koala.png");<br>
//Write the output
workbook.Save(@“f:\downloads\output.xls”);
}
Screenshot: