Perl uses Win32 OLE to Operate Excel

Source: Internet
Author: User
I recently used Perl (using Win32: Ole) to operate Excel. I didn't find similar content on the Internet, so I had to find some beginner knowledge in a book, and I had a great deal with myself, finally, I am a little confused. Here I will make a backup for your reference. ^_^

#! Usr/bin/perl
Use strict;
Use warnings;
Use Win32: Ole; # import a Win32 Ole package

My $ dir = 'd: // myperl // win32ole // '; # create two new Excel files in this directory
My $ src_name = $ dir. "test1". "/. xls ";
My $ dst_name = $ dir. "Test2". "/. xls"; my $ nowstr; # create an Excel application object, and then we can operate on Excel.
My $ app_xls = Win32: Ole-> New ('excel. application', sub {$ _ [0]-> quit })
Or die "can't install excel01! "; # Open an Excel file. 'true' indicates read-only my $ src_book = $ app_xls-> workbooks-> open ($ src_name, 0, 'true ');
My $ src_sheet = $ src_book-> worksheets (1); # select a worksheet
$ Nowstr = $ src_sheet-> cells (1, 'A')-> {value}; # retrieve data in a cell

Print ($ nowstr );
$ Src_sheet-> cells (1, 'A')-> {value} = "change"; # modify data in a cell

My $ dst_book = $ app_xls-> workbooks-> open ($ dst_name );
My $ dst_sheet = $ dst_book-> worksheets (1 );
$ App_xls-> {displayalerts} = 'false'; # Turn off the Excel prompt, such as whether to save the modification.
# $ Dst_book-> worksheets (1)-> Delete; # delete a worksheet

# Copy a worksheet from an Excel worksheet to another Excel worksheet
$ Src_book-> worksheets (1)-> copy ($ dst_book-> worksheets ('sheet3 '));
$ Dst_book-> Save; # Save the changes
$ App_xls-> {displayalerts} = 'true'; # restore prompt

UNDEF $ src_book;
UNDEF $ dst_book; UNDEF $ app_xls; # disable the opened Excel application

References: 1. perl5 programming core technology-du jingnong (Beijing hope electronics Publishing House) 2000.11 first version 2. Microsoft Excel developers refer to and use OLE to describe Excel operations, for ole-related support, you can find it on Microsoft's msdn. There is also an introduction to Ole in my blog technical discussion directory. Author: smilelance from: http://blog.csdn.net/smilelanceTime:2006.05.23

 

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.