Skip to content

Instantly share code, notes, and snippets.

@dalang
Created May 11, 2013 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalang/5560638 to your computer and use it in GitHub Desktop.
Save dalang/5560638 to your computer and use it in GitHub Desktop.
edit excel file and save as a new file with ParseExcel Package parse crscy contact excel file and output contact info in xml format
#! /usr/bin/perl
use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode; #◊÷∑˚±‡¬Î
open(FILE, '>_ftn.txt');
syswrite(FILE, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n
<list>\n");
my $parser = Spreadsheet::ParseExcel->new();
my $formatter = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map=>"CP936");#…Ë÷√◊÷∑˚±‡¬Î
#my $workbook = $parser->parse('Book.xls');
my $workbook = $parser->parse('contact1.xls', $formatter);#∞¥À˘…Ë÷√µƒ◊÷∑˚±‡¬ÎΩ‚Œˆ
my @array = ("<title>", "</title>","<department>", "</department>","<name>", "</name>", "<abb>", "</abb>", "<cellPhone>", "</cellPhone>","<officePhone>", "</officePhone>", "<email>","</email>", "<picture>", "</picture>", "<managerId>","</managerId>");
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
my $title;
my $name;
my $mid = 0;
for my $row ( $row_min .. $row_max ) {
syswrite(FILE, "\n<employee>\n");
syswrite(FILE, "<id>".$row."</id>\n");
syswrite(FILE, $array[0].$array[1]."\n");
#syswrite(FILE, $array[2]."\n".$array[3]."\n");
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
if ($col > 0) {
if ($col == 1) {
my $tmp = $worksheet->get_cell($row, $col)->value();
if (length($tmp)) {
$title = $tmp;
$mid++;
} else {
}
}
my $tmp = $cell->value();
if ($col == 2) {
$name = $tmp;
}
if (length($tmp) == 0) {
$tmp = $title;
}
if ($col < 6) {
syswrite(FILE, $array[$col * 2].$tmp.$array[$col * 2 + 1]."\n");
}
print $col, " ", $tmp," ";
if ($col == 6) {
syswrite(FILE, $array[-6].$name.'@crscy.com.cn'.$array[-5]."\n");
$tmp =~s/^[\s ]+//g;
if ($tmp eq 'M') {
syswrite(FILE, $array[-4]."no_avatar_m.jpg".$array[-3]."\n");
} else {
syswrite(FILE, $array[-4]."no_avatar_f.jpg".$array[-3]."\n");
}
last;
} }
#print $title;
}
print "\n";
syswrite(FILE, $array[-2].$mid.$array[-1]."\n");
syswrite(FILE, "</employee>\n");
}
}
syswrite(FILE, "\n</list>");
close(FILE);
<>;
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
my $excel_file_name = $ARGV[0];
my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $workbook_orig = $parser->Parse($excel_file_name);
# We will edit column 7 of the first worksheet.
my $worksheet = $workbook_orig->worksheet(0);
my $TEST_COL = 8;
my $EDIT_COL = 7;
my %hash;
my ($row_min, $row_max) = $worksheet->row_range();
for my $r ($row_min .. $row_max){
my $cell = $worksheet->get_cell($r, $TEST_COL);
unless (defined $cell){
next; # Modify as needed to handle blank cells.
}
my $val = $cell->value;
print $val;
if (exists $hash{$val})
{
$worksheet->AddCell( $r, $EDIT_COL, 0, $cell->{FormatNo} );
} else {
$hash{$val} = 1;
$worksheet->AddCell( $r, $EDIT_COL, $val, $cell->{FormatNo} );
}
}
# You can save the modifications to the same file, but when
# you are learning, it's safer to write to a different file.
$excel_file_name =~ s/\.xls$/_new.xls/;
$workbook_orig->SaveAs($excel_file_name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment