Introduction
I recently had a requirement to extract the data from a batch of barcode images and store the data in an Excel sheet. It would be time-consuming if I scanned the images one by one and manually entered the data into an Excel sheet. So a more efficient way to accomplish this task is urgently needed.
After several days of searching on forums, I eventually got an easy solution using C#, in which two extra free libraries have also been utilized to deal with barcode images and Excel sheets. Here are the 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 the project
The following is the list of DLLs that are used; please include this document in the source code before using the code.
Extract data from the barcode
Use Directory.GetFiles method to get the names of image files (including their paths) in the specified directory.
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 the path string, return the values in the picFile array.
List<string> list = new List<string>();
for (int i = 0; i < picFile.Length; i++)
{
string scanResult = BarcodeScanner.ScanOne(picFile[i]);
list.Add(scanResult);
picFile[i] = Path.GetFileName(picFile[i]);
}
Convert the List<string> to a string array.
string[] barcodeData = list.ToArray();
Write arrays to Excel
In the above part of the 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 an Excel worksheet.
Create a new workbook using Spire.XLS, and add some text in cells A1 and B1.
Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];
sheet.Range["A1"].Text = "Name";
sheet.Range["B1"].Text = "Data";
sheet.Range["A1"].Style.Font.IsBold = true;
sheet.Range["B1"].Style.Font.IsBold = true;
Invoke InsertArray(string[] stringArray, int firstRow, int firstColumn, bool vertical) from the Spire.XLS namespace to import arrays of strings to the worksheet.
sheet.InsertArray(picFile, 2, 1, true);
sheet.InsertArray(barcodeData, 2, 2, true);
sheet.Columns[0].ColumnWidth = 15f;
sheet.Columns[1].ColumnWidth = 20f;
sheet.Columns[1].IgnoreErrorOptions = IgnoreErrorType.NumberAsText;
// Save the changes to the workbook (assuming you have a save operation here)
// For example: wb.Save("YourFilePath.xlsx");
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.
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 a .xlsx file using the community edition of Spire. XLS.
Thanks for reading.