Im looking for some suggestions on some code I just wrote. I have a friend who works in the real estate business and he needed some help importing some data into a microsoft excel database. A friend or business associate sent him a rather large text file with contacts. He wasnt sure what file the program was from and didnt know how to import it. He only needed the name and address for each record. I told him to send me the file and I would see what I could do.
Looked like a good chance to see if I could write some usefull code. I have to say I was impressed how easy it was to get something working with my limited perl knowledge and in such short time (about 25 minutes). Now Im looking for suggestions on my code. Since I have no idea how to use OLE services I tried out the Spreadsheet::WriteExcel module.
Sample line of data from text file
"1909","W","22th St ","Santa Ana CA","92688-2328", "C002","Jose Z Cuerva","Jose","Z","Cuerva","","","","", "","","1909 W 22th St","Santa Ana CA","92688-2328", "","(714)555-1212","Single Fam Res","SA","974","5", "3","1.0","","","1","$137,500","F","09/09/1999", "456645","","$108,573","25","$199,999",".15A","6,352", "022-022-22","1922","Oceanside MTG","$199,999","","VAR","FHA"
My Code
#!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my ($inp_line, $num_addr, $dir_addr, $str_addr, $city_addr, $zip_addr, + $junk, $name, $junk2) = ''; my $inp_file = shift @ARGV || die "\nusage: $0 inpfile\n"; open (INP, "$inp_file") or die "\nCan't open file [$inp_file]: $!\n"; my $workbook = Spreadsheet::WriteExcel->new("$inp_file.xls"); my $worksheet = $workbook->addworksheet(); $worksheet->write(0,0,"NAME"); $worksheet->write(0,1,"ADDRESS"); $worksheet->write(0,2,"CITY"); $worksheet->write(0,3,"ZIP"); my $row = 2; my $temp = ''; while (<INP>) { chomp; ($num_addr,$dir_addr,$str_addr,$city_addr,$zip_addr,$junk,$name,$j +unk2) = split /\",\"/; $num_addr =~ /^\"(.*)$/; $num_addr = $1; $worksheet->write($row,0,$name); if ($dir_addr){ ($temp = $num_addr . " $dir_addr" . " $str_addr") }else { ($temp = $num_addr . " $str_addr"); } $worksheet->write($row,1,$temp); $worksheet->write($row,2,$city_addr); $worksheet->write($row,3,$zip_addr); ++$row; } close (INP);
Thanks!!
zzspectrez
In reply to Creating Excel Spreadsheet from text data file by zzspectrez
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |