#!c:\Perl\bin\perl.exe
# xl2m.pl 
# Convert Excel data into valid MARC21 records

use strict;
use warnings;
use MARC::Record;
use Spreadsheet::ParseExcel::Simple;

# Open the Excel file
my $fh;
unless ( $fh = Spreadsheet::ParseExcel::Simple->read( $ARGV[0] ) ) {
	die "No input Excel file.\n";
} else {
	print "Preparing MARC records ... \n\n";
}

# Open the Output filehandle
open( OUT, ">$ARGV[1]" );
binmode OUT, ":encoding(UTF-8)"; # Handle UTF-8 data sans warnings

my $rec_num = 0;

# Open the worksheet
foreach my $sheet ( $fh->sheets ) {
	while ( $sheet->has_data ) {
		my @row = $sheet->next_row;
		$rec_num++;
		my $bib_id	= $row[0];
		my $title	= $row[1];
		#[...]

		my $record = MARC::Record->new();
		$record->leader('00054nam a22002891a 4500');
		
		# Create the tags
		if ($title) {
			$record->append_fields(
				MARC::Field->new( '245', 1, 0, 'a' => $title )
			);
		}
		# [...]
		print OUT $record->as_usmarc();
		print ".";
	}
}
close(OUT);

print "\n\n$rec_num records created.\n";