| This article is one in the series of the articles related to the integration of Aspose.BarCode with other Aspose Components. Today, we will learn about using barcodes in Microsoft Excel Worksheets with the cooperation of Aspose.Cells and Aspose.BarCode. |
| Aspose.Cells is used to create and manage Microsoft Excel Sheets programmatically. In this article, we will use Aspose.Cells to create an excel file and then add a barcode image (generated by Aspose.BarCode) to one of its worksheets. |
Creating the Barcode
First of all, we will create an object of BarCodeBuilder class.
The desired data to be encoded into barcode, will be assigned to CodeText property of the BarCodeBuilder. The Code128 symbology will be selected from the Symbology enumeration and then assigned to SymbologyType property of the BarCodeBuilder as shown below:
[C#]
//Instantiate linear barcode object
BarCodeBuilder builder = new BarCodeBuilder();
//Set the Code text for the barcode
builder.CodeText = "1234567";
//Set the symbology type to Code128
builder.SymbologyType = Symbology.Code128;
[VB.NET]
'Instantiate linear barcode object
Dim builder As BarCodeBuilder = New BarCodeBuilder()
'Set the Code text for the barcode
builder.CodeText = "1234567"
'Set the symbology type to Code128
builder.SymbologyType = Symbology.Code128
The main purpose of this step is to create an instance of BarCodeBuilder and configure its properties.
First of all, we will create an object of BarCodeBuilder class.
The desired data to be encoded into barcode, will be assigned to CodeText property of the BarCodeBuilder. The Code128 symbology will be selected from the Symbology enumeration and then assigned to SymbologyType property of the BarCodeBuilder as shown below:
//Instantiate linear barcode object BarCodeBuilder builder = new BarCodeBuilder(); //Set the Code text for the barcode builder.CodeText = "1234567"; //Set the symbology type to Code128 builder.SymbologyType = Symbology.Code128;
'Instantiate linear barcode object Dim builder As BarCodeBuilder = New BarCodeBuilder() 'Set the Code text for the barcode builder.CodeText = "1234567" 'Set the symbology type to Code128 builder.SymbologyType = Symbology.Code128
The main purpose of this step is to create an instance of BarCodeBuilder and configure its properties.
Saving Barcode to MemoryStream
Once the properties of BarCodeBuilder are configured then we can create and save the barcode image to a MemoryStream. For this purpose, we will create an instance of MemoryStream and then store the barcode image to this MemoryStream by calling Save method of the BarCodeBuilder.BarCodeImage property as shown below:
[C#]
//Creating memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//Saving barcode image to memory stream
builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
[VB.NET]
'Creating memory stream
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream()
'Saving barcode image to memory stream
builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Once the properties of BarCodeBuilder are configured then we can create and save the barcode image to a MemoryStream. For this purpose, we will create an instance of MemoryStream and then store the barcode image to this MemoryStream by calling Save method of the BarCodeBuilder.BarCodeImage property as shown below:
//Creating memory stream System.IO.MemoryStream ms = new System.IO.MemoryStream(); //Saving barcode image to memory stream builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
'Creating memory stream Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream() 'Saving barcode image to memory stream builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Adding Barcode Image to Excel Worksheet using Aspose.Cells
After the barcode image is saved to a MemoryStream then we are done with the barcode image. The only thing is now needed to add this barcode image (saved in the MemoryStream) to the excel worksheet.Aspose.Cells provides Workbook class that represents an excel file. A Workbook class has a collection of Worksheets. Each Worksheet has a collection of Pictures that stores all images for that particular Worksheet. We can insert our barcode image to Pictures collection by calling its Add method. Add method of the Pictures collection takes a barcode image in the form of MemoryStream with the numbers of upper left row and upper left column to specify the position where barcode image will be inserted. After adding barcode image to the Worksheet, developers can store the excel file on their system by calling Save method of the Workbook class as shown in the figure:
[C#]
//Instantiate Excel class that represents an excel file
Workbook excel1 = new Workbook();
//Add the barcode image into the pictures collection of the first worksheet of
//the excel file in the form of a MemoryStream, ms with upper left row=5 and
//upper left column=5
excel1.Worksheets[0].Pictures.Add(5, 5, ms);
//Save the excel file
excel1.Save("MyFile.xls");
[VB.NET]
'Instantiate Excel class that represents an excel file
Dim excel1 As Excel = New Excel()
'Add the barcode image into the pictures collection of the first worksheet of
'the excel file in the form of a MemoryStream, ms with upper left row=5 and
'upper left column=5
excel1.Worksheets(0).Pictures.Add(5, 5, ms)
'Save the excel file
excel1.Save("MyFile.xls")
After the barcode image is saved to a MemoryStream then we are done with the barcode image. The only thing is now needed to add this barcode image (saved in the MemoryStream) to the excel worksheet.Aspose.Cells provides Workbook class that represents an excel file. A Workbook class has a collection of Worksheets. Each Worksheet has a collection of Pictures that stores all images for that particular Worksheet. We can insert our barcode image to Pictures collection by calling its Add method. Add method of the Pictures collection takes a barcode image in the form of MemoryStream with the numbers of upper left row and upper left column to specify the position where barcode image will be inserted. After adding barcode image to the Worksheet, developers can store the excel file on their system by calling Save method of the Workbook class as shown in the figure:
//Instantiate Excel class that represents an excel file Workbook excel1 = new Workbook(); //Add the barcode image into the pictures collection of the first worksheet of //the excel file in the form of a MemoryStream, ms with upper left row=5 and //upper left column=5 excel1.Worksheets[0].Pictures.Add(5, 5, ms); //Save the excel file excel1.Save("MyFile.xls");
'Instantiate Excel class that represents an excel file
Dim excel1 As Excel = New Excel()
'Add the barcode image into the pictures collection of the first worksheet of
'the excel file in the form of a MemoryStream, ms with upper left row=5 and
'upper left column=5
excel1.Worksheets(0).Pictures.Add(5, 5, ms)
'Save the excel file
excel1.Save("MyFile.xls")
Conclusion 
Aspose.Cells is also very simple to use with Aspose.BarCode. It provides a neat and clean API to ease the job of developers for adding barcode functionality to their excel worksheets by just adding few lines of code.
| Aspose.Cells is also very simple to use with Aspose.BarCode. It provides a neat and clean API to ease the job of developers for adding barcode functionality to their excel worksheets by just adding few lines of code. |
About Author
This article is written by Khawaja Salman Sarfraz (Team Leader, Aspose Pty Ltd.) who obtained his Masters degree in Computer Sciences. He has also been providing training and consultancy services on J2EE and .NET platforms.
Contact Author
Download Complete Source Code for this Article
This article is written by Khawaja Salman Sarfraz (Team Leader, Aspose Pty Ltd.) who obtained his Masters degree in Computer Sciences. He has also been providing training and consultancy services on J2EE and .NET platforms.
Contact Author
Download Complete Source Code for this Article

