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:
First: Load the Excel file.
- Workbook workbook = new Workbook();
- workbook.LoadFromFile("result.xlsx");
-
- Worksheet worksheet = workbook.Worksheets[0];
Second: Extract all images from Excel file.
- PicturesCollection PicCollection = worksheet.Pictures;
- Image picture;
- for (int i = 0; i < PicCollection.Count; i++)
- {
- picture = PicCollection[i].Picture;
- picture.Save(String.Format("img_{0}.png", i));
- }
Here are the extracted images:
Finally: Scan the extracted images using the barcode processing library.
- for (int i = 0; i < PicCollection.Count; i++)
- {
- String PicName = String.Format("img_{0}.png", i);
- if (File.Exists(PicName))
- {
- Console.WriteLine(Spire.Barcode.BarcodeScanner.ScanOne(PicName));
- }
- }
-
- Console.WriteLine("All Done.");
- 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.