We can find that the blog post with images is more attractive than plain text post. Therefore, people may insert some appropriate images in their posts or documents. Generally speaking, the images are related to contents. For example, if your document has mentioned a series of data, you can insert one image, like analysis chart to make reader be clearer about the data information.

Although we can display data information with chart, sometimes we may need to insert image which shows data information more clearly or some other image to make the Excel file be more appealed.

Then, I want to introduce one method about how to write and read image in Excel with C#. This method is based on one development component: Spire.XLS. Before using the following code, please install and download the component. Then, add DLL file in your project.

 

Write Image:

 

private void btnRun_Click(object sender, System.EventArgs e)

{

       Workbook workbook = new Workbook();

       Worksheet sheet = workbook.Worksheets[0];

 

       sheet.Pictures.Add(1,1,@"..\..\..\..\..\..\Data\day.jpg");

 

       workbook.SaveToFile("sample.xlsx");

       ExcelDocViewer(workbook.FileName);

      

}

 

 

private void ExcelDocViewer( string fileName )

{

       try

       {

              System.Diagnostics.Process.Start(fileName);

       }

       catch{}

}

 

Read Image:

 

private void btnRun_Click(object sender, System.EventArgs e)

{

       Workbook workbook = new Workbook();

       workbook.LoadFromFile(@"..\..\..\..\..\..\Data\ImageSample.xls");

       Worksheet sheet = workbook.Worksheets[0];

 

       ExcelPicture pic = sheet.Pictures[0];

      

       using( Form frm1 = new Form())

       {

              PictureBox pic1 = new PictureBox();

              pic1.Image = pic.Picture;

              frm1.Width = pic.Picture.Width;

              frm1.Height = pic.Picture.Height;

              frm1.StartPosition = FormStartPosition.CenterParent;

              pic1.Dock = DockStyle.Fill;

              frm1.Controls.Add(pic1);

              frm1.ShowDialog();

       }

}

 

private void ExcelDocViewer( string fileName )

{

       try

       {

              System.Diagnostics.Process.Start(fileName);

       }

       catch{}

}