Extract Data From Barcode And Store In Excel

Introduction

I recently had a requirement to extract the data from a batch of barcode images and store the data in Excel sheet. It must be time consuming if I scan the image one by one and enter manually the data into Excel sheet. So a more efficient way to accomplish this task is urgently needed.

After several days’ searching on forums, I eventually got an easy solution using C#, in which two extra free libraries has also been utilized to deal with barcode image and Excel sheet. Here are main purposes I achieved in my C# console application:

  • Read data from barcode images using Spire.Barcode.
  • Store the barcode data in a string array.
  • Get the name of each image file and store the data in another array.
  • Write the arrays to two columns in Excel using free Spire.XLS.

Add DLLs to project

The following is the list of DLLs which are used, please include this document into the source code before using the code.

DLL

Using the code

  • Extract data from barcode

Use Diretory.GetFiles method to get the names of image files (including their paths) in the specified directory.

  1. string[] picFile = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Image File");  
Initialize a new List<string> to store the scan result of each image. Call Path.GetFileName method to obtain the file name and extension from path string, return the values in picFile array.
  1. List < string > list = new List < string > ();  
  2. for (int i = 0; i < picFile.Length; i++)  
  3. {  
  4.     string scanResult = BarcodeScanner.ScanOne(picFile[i]);  
  5.     list.Add(scanResult);  
  6.     picFile[i] = Path.GetFileName(picFile[i]);  
  7. }  
Convert the List<string> to a string array.
  1. string[] barcodeData = list.ToArray();  
  • Write arrays to Excel
In the above part of code, we successfully get barcode data and names of images and store them in two string arrays. The sample code in the following section demonstrates how to write arrays into the specified cell ranges in Excel worksheet.

Create a new workbook using Spire.XLS, add some text in cell A1 and B1.
  1. Workbook wb = new Workbook();  
  2. Worksheet sheet = wb.Worksheets[0];  
  3. sheet.Range["A1"].Text = "Name";  
  4. sheet.Range["B1"].Text = "Data";  
  5. sheet.Range["A1"].Style.Font.IsBold = true;  
  6. sheet.Range["B1"].Style.Font.IsBold = true;  
Invoke InsertArray(string[] stringArray, int firstRow, int firstColumn, bool isVertical) from Spire.XLS namespace to import arrays of strings to worksheet.
  1. sheet.InsertArray(picFile, 2, 1, true);  
  2. sheet.InsertArray(barcodeData, 2, 2, true);  
  3. sheet.Columns[0].ColumnWidth = 15f;  
  4. sheet.Columns[1].ColumnWidth = 20f;  
  5. sheet.Columns[1].IgnoreErrorOptions = IgnoreErrorType.NumberAsText;  
Save the changes to workbook.
  1. wb.SaveToFile("data.xlsx", FileFormat.Version2010);   
Run the program, hundreds of images from the directory can be scanned within minutes, and it returns the values of image names and barcode data in an Excel worksheet as in the following:

worksheet

Conclusion

So far this method works extremely fine for my requirements. You do not even need to worry if you have a mass of data to process, since there is no rows/columns limitation when you write data into .xlsx file using the community edition of Spire.XLS.

Thanks for reading.

 

Next Recommended Readings