473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Win32::OLE Excel - Running Subtotal command

Hi All,

I am trying to create a subtotal in an Excel sheet through Perl script. Some how I am not able to make it work.

Here is what I wrote

Expand|Select|Wrap|Line Numbers
  1. $Sheet->Activate;
  2. $Range = $Sheet->Range("A2:C$Row");
  3. $Range->Select;
  4.  
  5. @TotalList = (2, 3);
  6. $Excel->Selection->Subtotal({GroupBy => 1, Function => xlCount, TotalList => (2)});
  7.  
Any help on this is highly appreciated

Thanks in advance
Dilip
Jul 17 '07 #1
8 5012
miller
1,089 Expert 1GB
Whenever you post code, attempt to make it compilable by the expert reading it. In this instance, you have not stated what perl module you using (Where's your use Module statement?), and you've also not shown how you're creating the $Sheet variable. Yes, some of us might be familiar enough with parsing excel spreadsheets to guess. But don't make us guess.

- Miller
Jul 17 '07 #2
Hi Miller,

Thank you for pointing out my mistake, I accept that and my sincere apologies.

Please find below the compilable code:

Expand|Select|Wrap|Line Numbers
  1. use Win32::OLE;
  2.  
  3. #############################################
  4. # Create Excel instance
  5. #############################################
  6. # Start Excel and make it visible
  7. $Excel = Win32::OLE->new('Excel.Application');
  8. $Excel->{SheetsInNewWorkbook} = 1;
  9. $Excel->{Visible} = 1;
  10.  
  11. #############################################
  12. # Add sheet
  13. #############################################
  14. my $Book = $Excel->Workbooks->Add;
  15. my $Sheet = $Book->Worksheets(1);
  16. $Sheet->{Name} = 'Sample';
  17.  
  18. # Insert column titles
  19. my $Range = $Sheet->Range("A1:C1");
  20. $Range->{Value} = ["One","Two", "Three"];
  21. $Range->Font->{Bold} = 1;
  22.  
  23.  
  24. #############################################
  25. # Generate some sample data
  26. #############################################
  27. for ($Row=2; $Row<6; $Row++) {
  28.     $Range = $Sheet->Range("A$Row:C$Row");
  29.     $Range->{1} = $Row;
  30.     $Range->{2} = $Row;
  31.     $Range->{3} = $Row;
  32. }
  33.  
  34. for (; $Row<25; $Row++) {
  35.     $Range = $Sheet->Range("A$Row:C$Row");
  36.     $Range->{1} = $Row;
  37.     $Range->{2} = $Row;
  38.     $Range->{3} = $Row;
  39. }
  40.  
  41. for ($R=2; $R<13; $R++) {
  42.     $Range = $Sheet->Range("A$Row:C$Row");
  43.     $Range->{1} = $R;
  44.     $Range->{2} = $R;
  45.     $Range->{3} = $R;
  46.     $Row ++;
  47. }
  48.  
  49. $Range = $Sheet->Range("A2:C$Row");
  50. $Range->Sort($Range->Range("A2"));
  51.  
  52. $Sheet->Activate;
  53. $Range = $Sheet->Range("A1:C$Row");
  54. $Range->Select;
  55.  
  56. @TotalList = (2, 3);
  57. $Excel->Selection->Subtotal({GroupBy => 1, Function => xlCount, TotalList => \@TotalList});
  58.  
  59. __END__
  60.  
Thanks & regards
Dilip
Jul 18 '07 #3
miller
1,089 Expert 1GB
Unfortunately, you have surpassed my knowledge with regard to this specific knowledge. I very much prefer the other major package for creating excel files.

cpan Spreadsheet::WriteExcel

- Miller
Jul 18 '07 #4
Thank you for quick reply, but my site does not have that package installed and I do not have permissions to install it.

Dilip
Jul 18 '07 #5
miller
1,089 Expert 1GB
Well, here's a question for you. Normally, whenever I want to know about a module's functionality, I simply go straight to cpan:

cpan Win32::OLE

In this instance though, that is not all that illuminating. The problem is that in order to document the accepted functionality, it seems like they might have to document all the functionality of Excel itself. I can't imagine that is something that they would choose to do.

So my question to you is, where do you learn how to use this module? All the books about it are very much outdated. The Randal Schwartz book is back from 1997.

- Miller
Jul 18 '07 #6
It is an interesting question. In fact, i am relatively new to PERL and did not have any formal training on PERL. But I am reasonably good in Visual Basic and I used to do these using VBA. And my current assignment requires me to do the above mentioned manipulation using PERL. To get this done, I managed to find some sample PERL scripts that manipulates Excel data. And from there onwards, I was just trying to convert the VBA code into PERL. Since the above code did not throw any compilation or run-time errors, I concluded that the OLE module does support that function, but probably there is something wrong in the invocation.

Once again, thank you very much for your efforts towards this.
Jul 19 '07 #7
miller
1,089 Expert 1GB
Ahh. Well, if you have experience with VisualBasic, then you should be ahead of the curve in knowing how to use this functionality. Unfortunately, to be able to help you with the perl aspect of the problem, it takes more knowledge than I have of the VB side.

If you're looking for a quick introduction to perl, this resource is pretty good:

Beginning Perl

And as always, knowing perldoc is very important.

http://perldoc.perl.org/

- Miller
Jul 19 '07 #8
No worries Miller.

Thanks for the links (Beginning Perl) and thank you very much for your time and efforts.

Catch you later with some other PERL query

Dilip
Jul 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: M. David Allen | last post by:
Hello, I've been using ActiveState's ActivePerl to generate Excel spreadsheets using the Win32::OLE module. The rudimentary examples that are out there on the web show the way to set up...
2
by: usenet | last post by:
Problem installing Win32::OLE under Cygwin. I have searched the web and found little documentation on installation procedure. Please help. I'd like to get Win32::OLE working under cygwin. ...
3
by: sandycat05 | last post by:
Hello all, I am a new Perl programmer. Below is the beginnings of a code that I am using to manipulate an Excel spreadsheet via Perl using win32::OLE. However, what I'd like to do is the following:...
2
by: medak | last post by:
Hi, I am trying to automate internet explorer using Active Perl’s Win32::OLE Module I know that WithEvents(OBJECT]method enables and disables the firing of events by the specified OBJECT. And...
2
by: momukhtar | last post by:
I am using Win32::OLE to write a perl script which opens an excel file. That excel file is password protected and everytime i run that script dialog box pops up and I have to click on Read-Only then...
8
by: rssd | last post by:
can somebody help me. I'm trying to read some excel files but i'm always getting this error No type library matching "Microsoft Excel" found at D:\Genes_datasets\exp.pl line 4 Win32::OLE(0.16):...
1
by: mohanprasadgutta | last post by:
Hi, I need help to open a password protected excel file in perl using Win32:OLE. when I tried to open file in normal way at the time of program execution it is prompting me to enter password. I am...
1
by: Tension | last post by:
Hi, I have written a development automation tool that needs to be distributed to 20-odd developers using Windows XP workstations. Is there a free Perl distribution that includes "Win32::OLE" by...
6
by: poolboi | last post by:
hi all, i've got the following program that needs yr help: use Win32::OLE; # use existing instance if Excel is already running eval {$ex =...
1
by: user1357 | last post by:
Hi all, i am trying to run the following code use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel';
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.