-
June 2nd, 2009, 10:23 AM
#1
How to insert barcode images into a Microsoft Excel worksheet using C# or VB.NET and
How to insert barcode images into a Microsoft Excel worksheet using C# or VB.NET and Barcode Professional for .NET
Technologies used
- 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:
1- Open Visual Studio and create a new Windows Forms Application.
2- Open the default Form at design time and design it as is shown in the following figure
http://www.neodynamic.com/Support/FA...gure1Small.jpg
3- In order to manipulate Excel documents from .NET, we're going to use the Microsoft Office Primary Interop Assemblies (PIA) provided by Microsoft. PIA is available for Microsoft Office XP and Office 2003. You can get them from the following locations:
Office XP PIA
http://www.microsoft.com/downloads/d...DisplayLang=en
Office 2003 PIA
http://www.microsoft.com/downloads/d...DisplayLang=en
Please, download the correct PIA and install it on your box.
NOTE:
For older versions of Office, please read the following papers at Microsoft website about interoperation between Microsoft .NET managed code and Microsoft Office applications.
http://msdn.microsoft.com/office/arc...ych2_part1.asp
4- With your Windows Forms project opened, add a reference to Microsoft Excel [Version] Object Library as is shown in the following figure:
http://www.neodynamic.com/Support/FA...gure2Small.jpg
And add a reference to Barcode Professional as is shown in the following figure. IMPORTANT: Please, select the correct version of Barcode Professional depending on what version of the .NET Framework you're using.
http://www.neodynamic.com/Support/FA...gure3Small.jpg
In this case we're going to use a fictitious Invoice worksheet that you'll find at the end of this guide available for download.
5- The barcode image will be inserted at B9 cell as you can see in the following figure:
http://www.neodynamic.com/Support/FA...gure4Small.jpg
IMPORTANT: The cell location is very important because we'll reference it in code.
6- In the Windows Forms project, add the following method in the form's code behind file:
Visual Basic .NET
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
Visual C# .NET
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!");
}
7- Add the following code for button's Click events
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
Visual C# .NET
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");
}
8- That's it. Run your project, type some value to encode and click Insert button. The barcode image will appear into the Excel worksheet!
http://www.neodynamic.com/Support/FA...gure5Small.jpg
http://www.neodynamic.com/Support/FA...gure6Small.jpg
Links:
This Demo
More Demos
Download Barcode Professional for Windows Forms
More Information about Neodynamic Barcode Professional for Windows Forms
Neodynamic
.NET Components & Controls
http://www.neodynamic.com
http://www.barcode-for-net.com
-
February 23rd, 2012, 01:46 AM
#2
Re: How to insert barcode images into a Microsoft Excel worksheet using C# or VB.NET
I suggest you to try another barcode generator Here is a free barcode generator that I've been using , KEEPAUTOMATION Bar Code generator for excel, quite easy !http://www.keepautomation.com/produc.../code_128.html
Hope I helped
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|