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.
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.
- 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.
- 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();
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.
- 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 isVertical) from Spire.XLS namespace to import arrays of strings to 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 workbook.
- 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 .xlsx file using the community edition of Spire.XLS.
Thanks for reading.