How to Insert Barcode Control to MS Excel WorkSheetin C# ?

back to home  >  Tech FAQ  >  How to Insert Barcode Control to MS Excel WorkSheetin C#

Question:

Hi, I want toadd some barcode image as user control to MS Excel in my VSTO project. Can you give me some suggestion?

---- Thomas

Answer:

Hi, Thomas.

There is a simple example for how to insert a barcode control to Excel worksheet using C#.

First, you need create a barcode user control as below:

public partial class BarcodeControl : UserControl
{
    public Barcode _barcode;

    public BarcodeControl()
    {
        InitializeComponent();
        _barcode = new Barcode();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        _barcode.Data = "123456";
        _barcode.BarType = BarCodeType.Code128;
        _barcode.Width = this.Width;
        _barcode.Height = this.Height;

        Bitmap bmp = _barcode.CreateBarcode();           

        e.Graphics.DrawImage(bmp, 0, 0);
    }
}

Then, add this barcode control into worksheet.

//get current showing sheet
Worksheet worksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);

//get current range
Excel.Range range = (Excel.Range)Globals.ThisAddIn.Application.Selection;

//add barcode control to current range
Microsoft.Office.Tools.Excel.ControlSitedynamicControl = worksheet.Controls.AddControl(barControl, range, "barcode");

Hope the C# code can be helpful.

---- pqScan Support Team

Online Demo Download Trial How to Encode QR Code with Chinese Words in C# ? How to Add Barcode Image to MS Excel in C# ? How can I Create Barcode to Crystal Report in Visual Studio ? Can I Read Barcode in PDF document using C#? Can I Get a Compression Jpeg Image from PDF ? Merge A Group of Multi-page Tiff to PDF documentusing C#? Translate JPG Image to PDF document in C#? Convert specific pages from PDF to images in C# Is it available for reading all of the barcode in a picture by one time? Do You Support Translate Images to PDF from SQL Database? Question about Image Size.