Neodynamic Barcode Professional 2.5 for .NET Windows Forms or greater
Microsoft .NET Framework 1.x or greater
Microsoft Visual Studio .NET or greater
Microsoft Office Primary Interop Assemblies (PIA)
Due to its flexible design, Barcode Professional allows you to easily insert barcode images into a Microsoft Excel Worksheet using .NET technology.
In the following sample we're going to generate a simple .NET Windows Forms application that will allow you to insert a barcode image into a predefined Excel worksheet.
Follow these steps:
Private Sub AddBarcodeIntoExcel()
'Create barcode professional instance
Dim bc As New Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional
'Set some barcode symbology
bc.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128
'Set value to encode
bc.Code = Me.TextBox1.Text
bc.Text = ""
'Create barcode image
Dim ms As New System.IO.MemoryStream(bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png))
Dim barcodeImage As Bitmap = CType(Image.FromStream(ms), Bitmap)
'Create an Excel App
Dim excelApp As New Microsoft.Office.Interop.Excel.Application()
excelApp.Visible = False
'The Excel doc paths
Dim oMissing As Object = System.Reflection.Missing.Value
Dim destFile As String = "C:\INVOICE_WITH_BARCODE.xls"
Dim excelFile As String = "C:\INVOICE.xls"
'Open the worksheet file
Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
excelBook = excelApp.Workbooks.Open(excelFile)
Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
excelSheet = CType(excelBook.Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
'Find the predefined barcode cell into the worksheet
Dim barcodeCell As Object = "B9"
Dim range As Microsoft.Office.Interop.Excel.Range
range = excelSheet.Range(barcodeCell)
'Copy the barcode image into Clipboard
Clipboard.SetDataObject(barcodeImage)
'Paste the barcode image
range.Select()
excelSheet.Paste()
'Save a copy with barcode
excelSheet.SaveAs(destFile)
'Quit
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
ms.Close()
barcodeImage.Dispose()
MessageBox.Show("Barcode inserted!")
End Sub
private void AddBarcodeIntoExcel()
{
//Create barcode professional instance
Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional bc = new Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional();
//Set some barcode symbology
bc.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128;
//Set value to encode
bc.Code = this.textBox1.Text;
bc.Text = "";
//Create barcode image
System.IO.MemoryStream ms = new System.IO.MemoryStream(bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png));
Bitmap barcodeImage = (Bitmap)Image.FromStream(ms);
//Create an Excel App
Microsoft.Office.Interop.Excel.ApplicationClass excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = false;
//Interop params
object oMissing = System.Reflection.Missing.Value;
//The Excel doc paths
string excelFile = @"C:\INVOICE.xls";
string destFile = @"C:\INVOICE_WITH_BARCODE.xls";
//Open the worksheet file
Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(excelFile, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Sheets.get_Item(1);
//Find the predefined barcode cell into the worksheet
object barcodeCell = "B9";
Microsoft.Office.Interop.Excel.Range range = excelSheet.get_Range(barcodeCell,barcodeCell);
//Copy the barcode image into Clipboard
Clipboard.SetDataObject(barcodeImage);
//Paste the barcode image
range.Select();
excelSheet.Paste(oMissing, oMissing);
//Save a copy with barcode
excelSheet.SaveAs(destFile, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
//Quit
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
ms.Close();
barcodeImage.Dispose();
MessageBox.Show("Barcode inserted!");
}
Visual Basic .NET
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox1.Text.Length > 0 Then
Me.AddBarcodeIntoExcel()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
System.Diagnostics.Process.Start("C:\INVOICE_WITH_BARCODE.xls")
End Sub
private void button1_Click(object sender, System.EventArgs e)
{
if (this.TextBox1.Text.Length > 0)
this.AddBarcodeIntoExcel();
}
private void button2_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start(@"C:\INVOICE_WITH_BARCODE.xls");
}
Sample Files Download
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.