This article is one in the series of 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 the BarcodeGenerator class. The desired data to be encoded into a barcode will be assigned to CodeText property of the BarcodeGenerator. The Code128 symbology will be selected from the EncodeTypes enumeration and then assigned to EncodeTypes property of the BarcodeGenerator as shown below:
Saving Barcode to MemoryStream
Once the properties of BarcodeGenerator 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 the Save method of the BarcodeGenerator.Save method as shown below:
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. The add method of the Pictures collection takes a barcode image in the form of MemoryStream with the numbers of the upper left row and upper left column to specify the position where the barcode image will be inserted. After adding the barcode image to the Worksheet, developers can store the excel file on their system by calling the Save method of the Workbook class.
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 a few lines of code.