Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri Apr 20, 2018 12:49 am

Hello,

I am wondering is there a way to import an import an image into a cell instead of locate on specific col, row. Following sample only import an image into a col, row position.

Thanks a lot.

using System;
using System.Data.OleDb;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using Spire.Xls;

namespace WriteImages
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnRun_Click(object sender, System.EventArgs e)
{
//Create a Workbook
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\..\..\..\..\Data\WriteImages.xlsx");
//Get the first sheet
Worksheet sheet = workbook.Worksheets[0];

//Add an image to the specific cell
sheet.Pictures.Add(14, 5, @"..\..\..\..\..\..\Data\SpireXls.png");

//Save and Launch
workbook.SaveToFile("Output.xlsx",ExcelVersion.Version2010);
ExcelDocViewer(workbook.FileName);
}
private void ExcelDocViewer( string fileName )
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch{}
}

private void btnClose_Click(object sender, EventArgs e)
{
Close();
}


}
}

tonyfu88
 
Posts: 3
Joined: Wed Apr 18, 2018 9:50 am

Fri Apr 20, 2018 2:44 am

Hello,

Thanks for your post. You could change the cell size and picture size to make the picture locate in the cell. Please refer to the below code snippet to insert a picture into a cell. In addition, you could use picture.ResizeBehave = ResizeBehaveType.MoveAndResize to make the picture move and resize with the cell. But I found the functions is not working. I have posted the issue to our DEV team. If there is any update, we will let you know.
Code: Select all
   Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"Test.xlsx");
 
            Worksheet sheet = workbook.Worksheets[0];
               
            sheet.Range["F4"].Text = "Align Picture Within A Cell:";
            sheet.Range["F4"].Style.VerticalAlignment = VerticalAlignType.Top;

            string picPath = @"log.bmp";
            ExcelPicture picture = sheet.Pictures.Add(4, 6, picPath);

            picture.Width = 300;
            picture.Height = 160;

            sheet.Range["F4"].ColumnWidth = 50;
            sheet.Range["F4"].RowHeight=150;

            picture.LeftColumnOffset = 100;
            picture.TopRowOffset = 25;
         
            picture.ResizeBehave = ResizeBehaveType.MoveAndResize; //not working
            workbook.SaveToFile("AlignPicture.xlsx", ExcelVersion.Version2013);


Best regards,
Simon
E-iceblue support
Last edited by Simon.yang on Mon Apr 23, 2018 3:40 am, edited 1 time in total.
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Mon Apr 23, 2018 12:48 am

Dear Simon,

Thanks a lot for your response. I got 'Spire.Xls.ExcelPicture' does not contain a definition for 'ResizeBehave' and no extension method 'ResizeBehave' accepting a first argument of type 'Spire.Xls.ExcelPicture' could be found (are you missing a using directive or an assembly reference?) during compilation when trying your solution.

If I comment line picture.ResizeBehave = ResizeBehaveType.MoveAndResize; //not working
It could run. I am wondering is there anything goes wrong? The other thing I like to have is automatic adjust cell width and height to fit import image.

Thanks a lot.

Regards,
Tony

tonyfu88
 
Posts: 3
Joined: Wed Apr 18, 2018 9:50 am

Mon Apr 23, 2018 8:47 am

Hello Tony,

Thanks for your feedback. As the free version doesn't support the feature, you will encounter the error. You could use the latest one(Spire.XLS Pack(Hotfix) Version:8.4.6) to have a try.
With regard to adjusting cell width and height to fit import image automatic, I am sorry there isn't a workaround for it at present.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Apr 24, 2018 6:23 am

Dear Simon,

Appreciated your great support. I will try your suggestion later.

Regards,
Tony

tonyfu88
 
Posts: 3
Joined: Wed Apr 18, 2018 9:50 am

Thu May 10, 2018 9:38 am

Hello,

Glad to inform you the ResizeBehave issue has been fixed. Welcome to download Spire.XLS Pack(Hotfix) Version:8.5.1.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.XLS