Read Barcodes From Excel File

Introduction

In my last article, I presented a solution to generate many barcodes and then store the barcode images in an Excel file. And an idea just occurred to me; since the barcode images are inserted into an Excel file, can I read these barcodes from an existing Excel file. So I gave it a try.

The solution is easy. Barcodes are inserted into an Excel file as pictures. So just extract all images from the Excel file. Then scan the extracted images using the barcode processing library. And the work is done.

Code

Screenshot of the Excel file:

Read Barcodes from Excel file

First: Load the Excel file.

  1. Workbook workbook = new Workbook();  
  2. workbook.LoadFromFile("result.xlsx");  
  3.   
  4. Worksheet worksheet = workbook.Worksheets[0]; 

Second: Extract all images from Excel file.

  1. PicturesCollection PicCollection = worksheet.Pictures;  
  2. Image picture;  
  3. for (int i = 0; i < PicCollection.Count; i++)  
  4. {  
  5.     picture = PicCollection[i].Picture;  
  6.     picture.Save(String.Format("img_{0}.png", i));  

Here are the extracted images:



Finally: Scan the extracted images using the barcode processing library.

  1. for (int i = 0; i < PicCollection.Count; i++)  
  2. {  
  3.     String PicName = String.Format("img_{0}.png", i);  
  4.     if (File.Exists(PicName))  
  5.     {  
  6.         Console.WriteLine(Spire.Barcode.BarcodeScanner.ScanOne(PicName));  
  7.     }  
  8. }  
  9.   
  10. Console.WriteLine("All Done.");  
  11. Console.ReadLine(); 

Output:



Conclusion

You are welcome to test this solution to read barcodes from an Excel file. If you have a better solution to fulfill the task, please share it with me.


Similar Articles