Excel C#/VB.NET


What is Excel Image and why we insert it?
Excel image is certainly an image inserted in the Excel document. As the same with all the images, excel image can help users understand the information more easily with a fast speed. But it does not mean the way we insert images is the same. The Excel supports many kind of formats for images, such as BMP, JPEG, JP2, GIF,TIFF, PNG, EXIF, WBMP, MBM. Thus, great convenience is provided for you to insert images of different formats. The text below I will show you how to insert images into Excel with Spire.XLS for C#, VB.NET.


How to insert an image in Excel with Spire.XLS?
Spire.XLS, as a professional Excel component, can introduce a simple way to write Excel image with C#, VB.NET. Before we start, please make sure that Spire.XLS and Visual Studio are installed in your
computer. If you have no Spire.XLS, you can freely download it by clicking here.

Step 1: Create Excel Image project and add reference.
Create the Excel Image project in Visual Studio. Then, add a button in Form 1. Finally, click the button and add Spire. XLS dll as reference.


Step 2: Add the following code to the top of the file.

C#
using Spire.Xls;

VB.NET
Imports Spire.Xls



Step 3: Add the following codes to the method
C#
private void button1_Click(object sender, EventArgs e)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Pictures.Add(1, 1, @"..\..\..\..\..\..\Data\day.jpg");
            workbook.SaveToFile("sample.xls");
            ExcelDocViewer(workbook.FileName);
        }

        private void ExcelDocViewer(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }



VB,NET
Dim workbook As Workbook = New Workbook()
        Dim sheet As Worksheet = workbook.Worksheets(0)
        sheet.Pictures.Add(1,1,"..\..\..\..\..\..\Data\day.jpg")
        workbook.SaveToFile("sample.xls")
        ExcelDocViewer(workbook.FileName)
End Sub
Private Sub ExcelDocViewer(ByVal fileName As String)
        Try
               System.Diagnostics.Process.Start(fileName)
        Catch
        End Try
End Sub



Step 4: Run the project Excel Image

Press F5 to run the project, then click button1 in Form1, you can see picture 4.1 appears:





More About Spire. XLS

Spire. XLS, as a professional Excel component, allows its users to operate Excel document directly such as save to stream, save as web response, copy, lock/unlock worksheet, set up workbook properties, etc. Click to learn more



Source

4 comments: