Generate Barcode in Excel using C#

Excel is a spreadsheet application developed and published by Microsoft. It is most commonly used to store, organize, and track data sets with formulas and functions. In certain cases, we may need to generate and add barcodes in Excel files to embed specific information. We can add machine-readable barcode images to XLSX or XLS files programmatically in .NET applications. In this article, we will learn how to generate a barcode in Excel using C#.

The article shall cover the following topics:

C# API to Generate Barcode in Excel

For adding barcodes to Excel spreadsheets, we will follow a two-step procedure. We will be using the Aspose.Cells for .NET API to create or load an Excel file. The Workbook class of the API allows creating a new Excel workbook or loading an existing Excel file for further processing. The Save() method of this class saves the Workbook on the given file path. The API also provides a Worksheet class to handle all the sheet-level operations.

We will generate and add the barcode image to the Excel sheet using the Aspose.BarCode for .NET API. It provides the BarcodeGenerator class to generate the barcode of the specified EncodeType. The Save() method of this class saves the barcode image to stream in a specific format. It provides BarCodeImageFormat enumeration to specify the save formats. The API also provides the BarCodeReader class to read the barcode from images.

Please either download the DLLs of the APIs or install them using NuGet.

PM> Install-Package Aspose.BarCode
PM> Install-Package Aspose.Cells

Create Excel Spreadsheet and Add Barcode in C#

We can create a new Excel spreadsheet and add a barcode image to the Excel by following the steps given below:

  1. Firstly, create an instance of the BarcodeGenerator class with the EncodeType and text to encode as arguments.
  2. Next, create an instance of the memory stream object.
  3. Then, call the Save() method to save the barcode image to the memory stream.
  4. Next, create an instance of the Workbook class.
  5. Then, add a new Worksheet to the WorksheetCollection of the Workbook.
  6. After that, add the image to the PictureCollection of the Worksheet with the stream object and image position as arguments.
  7. Finally, call the Save() method. It takes the output XLSX file path as an argument.

The following code example demonstrates how to create a new Excel spreadsheet and add a barcode image using C#.

// This code example demonstrates how to add Barcode to a new Excel file.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567");
// Creating memory stream and Saving barcode image to memory stream
System.IO.Stream ms = new System.IO.MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Instantiate Excel class that represents an Excel file
Workbook workbook = new Workbook();
// Adding new sheet
Worksheet sheet = workbook.Worksheets.Add("MySheet");
// The Add method takes the following parameters:
// Upper left row index, the index of the upper left row.
// Upper left column index, the index of the upper left column.
// Image file.
sheet.Pictures.Add(5, 5, ms);
// Save the file
workbook.Save(dataDir + "sample.xlsx");
Create an Excel Spreadsheet and Add a Barcode using C#.

Create an Excel Spreadsheet and Add a Barcode using C#.

Add Barcode to Existing Excel File in C#

We can also add a barcode image to any sheet of an existing Excel workbook by following the steps given below:

  1. Firstly, create an instance of the BarcodeGenerator class with the EncodeType and text to encode as arguments.
  2. Next, create an instance of the memory stream object.
  3. Then, call the Save() method to save the barcode image to the memory stream.
  4. Next, load an existing Excel file using the Workbook class.
  5. Then, access the Worksheet by its index.
  6. After that, add the image to the PictureCollection using the Add() method with the stream object and image position as arguments.
  7. Finally, call the Save() method. It takes the output XLSX file path as an argument.

The following code example demonstrates how to add a barcode image to an existing Excel file using C#.

// This code example demonstrates how to add Barcode to an existing Excel file.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567");
// Creating memory stream and Saving barcode image to memory stream
System.IO.Stream ms = new System.IO.MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Instantiate Excel class that represents an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Access the sheet by its index
Worksheet sheet = workbook.Worksheets[0];
// The Add method takes the following parameters:
// Upper left row index, the index of the upper left row.
// Upper left column index, the index of the upper left column.
// Image file.
sheet.Pictures.Add(5, 5, ms);
// Save the file
workbook.Save(dataDir + "sample_out.xlsx");

Add QR Code to Excel File in C#

Similarly, we can also add a QR code to the Excel file by following the steps mentioned earlier. However, we just need to set the EncodeType as QR in the first step. We may also need to adjust the image position in step # 6.

The following code example demonstrates how to add a QR code to an Excel file using C#.

// This code example demonstrates how to add QR code to an existing Excel file.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "1234567");
// Creating memory stream and Saving barcode image to memory stream
System.IO.Stream ms = new System.IO.MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Instantiate Excel class that represents an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Access the sheet by its index
Worksheet sheet = workbook.Worksheets[0];
// The Add method takes the following parameters:
// Upper left row index, the index of the upper left row.
// Upper left column index, the index of the upper left column.
// Image file.
sheet.Pictures.Add(5, 5, ms);
// Save the file
workbook.Save(dataDir + "sample_out_qr.xlsx");

Read Barcode from Excel File using C#

We can recognize any barcode image embedded on any sheet of the Excel file by following the steps given below:

  1. Firstly, load an existing Excel file using the Workbook class.
  2. Then, access the Worksheet by its index.
  3. Next, save the images to stream from PictureCollection in a loop.
  4. Next, create an instance of the BarCodeReader class with image stream and DecodeType as arguments.
  5. After that, call the ReadBarCodes() method to get the BarCodeResult object.
  6. Finally, show the barcode information.

The following code example demonstrates how to read a barcode image from an Excel file using C#.

// This code example demonstrates how to read barcode from an Excel file.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Instantiate Excel class that represents an Excel file
Workbook workbook = new Workbook(dataDir + "sample_out.xlsx");
Worksheet sheet = workbook.Worksheets[0];
// Save images to stream in a loop
foreach(var img in sheet.Pictures)
{
// Save image to stream
MemoryStream imageStream = new MemoryStream();
img.ToImage(imageStream, new Aspose.Cells.Rendering.ImageOrPrintOptions());
// Recognize the barcode from the image stream above
using (BarCodeReader reader = new BarCodeReader(imageStream, DecodeType.Code128))
{
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeType);
Console.WriteLine("Symbology: " + result.CodeText);
}
}
}

The above code sample shall produce the following output.

Codetext found: Code128
Symbology: 1234567

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create an Excel workbook programmatically;
  • add a new sheet in the Excel workbook;
  • generate and add a barcode image to the Excel spreadsheet;
  • read a barcode image from an Excel file.

Besides, you can learn more about Aspose.BarCode for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also