Perl operations on Excel files

Source: Internet
Author: User
Tags ole

Use strict;
Use CWD;
Use strict 'subs ';
Use Win32: Ole QW (in );
Use Win32: Ole: variant;
$ Win32: Ole: Warn = 3; # Die on errors...

My @ files = ("1.xls"," 2.xls", "3.xls"," 4.xls", "5.xls"," 6.xls ");
Foreach my $ file (@ files)
{
& Editexcel ($ file );
Alarm (1500 );
}

Sub editexcel
{
My $ dir = getcwd;
My $ fullpath = "$ DIR/$ _ [0]";
Print $ fullpath. "/N ";
############################ Initialize ########## #####################
My $ Excel;
# Use existing instance if Excel is already running
Eval
{
Win32: Ole-> getactiveobject ('excel. application ');
};
If ($ @)
{
Print "excel not installed $ @";
}
Unless (defined $ Excel)
{
$ Excel = Win32: Ole-> New ('excel. application', sub {$ _ [0]-> quit;}) or print "cannot start excel .";
}
Alarm (500 );
############################ Current date ######### ######################
My $ sec, my $ min, my $ hour, my $ mday, my $ month, my $ year, my $ wday, my $ yday, my $ isdst;
($ Sec, $ min, $ hour, $ mday, $ month, $ year, $ wday, $ yday, $ isdst) = localtime (Time ());
$ Year + = 1900;
$ Month + = 1;
# Print "$ year-$ month-$ mday ";

# $ Excel-> {visible} = 0; # Show Excel
######################### Workbook ############## ###################
# Open a workbook
My $ book = $ Excel-> workbooks-> open ($ fullpath );
############################ Sheets ########### #######################
My $ sheets = $ book-> worksheets;
############################# Sheet ######### ###########################
# My $ sheet = $ book-> worksheets (1 );
My $ curr_mon = sprintf ("%-02.2d month", $ month );
My $ curr_day = sprintf ("%-02.2d", $ mday );
My $ sheet = $ sheets-> item ($ curr_mon );
# Activate the appointed page
$ Sheet-> activate ();
############################## Set value ######## ############################
# Set yrp
$ Sheet-> cells (2, 1)-> {value} = "yrp ";
$ Sheet-> cells (3, 2)-> {value} = "bug Parsing ";
# Row counts and column counts of cells used
My $ row_counts = $ sheet-> {usedrange}-> {rows}-> {count };
My $ column_counts = $ sheet-> {usedrange}-> {columns}-> {count };
For (my $ Col = 1; $ Col <= $ column_counts; $ Col ++)
{
If ($ sheet-> cells (1, $ col)-> {value} = ~ /^ $ Curr_day $/I)
{
$ Sheet-> cells (3, $ col)-> {value} = 8;
$ Sheet-> cells (4, $ col)-> {value }= 100;
Print "-- Set Value OK !! --/N ";
Last;
}
}
########################## Save ############# ##########################
# Save and exit
$ Book-> Save ();
$ Book-> close ();
UNDEF $ book;
UNDEF $ Excel;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.