hi guys,
i've got the code below that extract cell value from an excel sheet
but any idea how i continue to output it to a text file?
i'm not too sure how to do it, thanks -
use strict;
-
use Win32::OLE qw(in with);
-
use Win32::OLE::Const 'Microsoft Excel';
-
$Win32::OLE::Warn = 3; # die on errors...
-
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
-
|| Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel
-
# application or open new
-
my $Book = $Excel->Workbooks->Open("C:\\DOCUMENTS\\test.xls"); # open Excel file
-
my $Sheet = $Book->Worksheets(1); # select worksheet number 1
-
$last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
-
my $array = $Sheet->Range("A4:B$last_row")->{'Value'}; # get the contents
-
$Book->Close;
-
-
16 4604
It has returned two dimensional array i.e an array with references to arrays.
so the value to be get from the array reference -
use strict;
-
use Win32::OLE qw(in with);
-
use Win32::OLE::Const 'Microsoft Excel';
-
$Win32::OLE::Warn = 3; # die on errors...
-
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
-
# get already active Excel
-
my $Book = $Excel->Workbooks->Open("E:\\perl-scripts\\thescripts\\test.xls"); # open Excel file
-
my $Sheet = $Book->Worksheets(1); # select worksheet number 1
-
my $last_row = $Sheet->UsedRange->Find({What => "*", SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row};
-
my $array = $Sheet->Range("A4:B$last_row")->{'Value'}; # get the contents
-
$Book->Close;
-
-
foreach my $cell_ref (@$array) {
-
foreach my $value (@$cell_ref) {
-
print "$value\t";
-
}
-
print "\n";
-
}
-
-Manimaran
hm..i think there's a problem with the code
u can pass it into an array and print out the values,
but it's not printing to a text file
any suggestion how i can print the values to a text file?
hm..i think there's a problem with the code
u can pass it into an array and print out the values,
but it's not printing to a text file
any suggestion how i can print the values to a text file?
If you want to output to a file, then you have to first open a file and get a file handle assigned, then use the file handle to point the print statement to the file for output.
Here is an example: -
# open a file for writing to and assign a file handle
-
open(FILE, ">file.txt");
-
-
-
# Print out to the newly opened file
-
print FILE ("Some text\n");
-
Regards,
Jeff
hey guys thanks for the help got it printed out already but i need to do a few more things, tried a lot ways still can't get it
my excel file for example has data in its respective cell like:
1 4
2 5
3 6
when i print it to a text file it becomes:
1
4
2
5
3
6
any idea if i can change the print out format in text file to:
1 4
2 5
3 6
i tried putting 123 in one array and 456 in another array but could not work it out
below's my most recent code -
-
-
-
use Win32::OLE;
-
use Win32::OLE::Const 'Microsoft Excel';
-
-
# use existing instance if Excel is already running
-
eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
-
die "Excel not installed" if $@;
-
unless (defined $ex) {
-
$ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
-
or die "Oops, cannot start Excel";
-
}
-
-
####deducing if $save_file_name exist or not######
-
-
-
$book = $ex->Workbooks->Open("C:\\Documents and Settings\\clong\\Desktop\\Convert to Text file\\Quarantined_IMSIHLR2_20070611_Completed1.xls");
-
$sheet = $book->Worksheets(1);
-
-
$data = $sheet -> Range("A4:B2873") -> {Value};
-
-
-
open(FILE, ">file.txt");
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
-
print FILE ("$value \n");
-
}
-
}
-
Change this block of code: -
open(FILE, ">file.txt");
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
print FILE ("$value \n");
-
}
-
}
-
to: -
open(FILE, ">file.txt");
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
print FILE ("$value \t");
-
}
-
print FILE "\n";
-
}
-
thanks for the codes
ok tried soemthing too..
i wanted another variable added to the text file:
1 4 DELETED
2 5 DELETED
3 6 DELETED
i amended my code to : -
-
-
open(FILE, ">file.txt");
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
-
print FILE ("$value \t","DELETED");
-
-
-
}
-
-
print FILE "\n";
-
}
-
and it became :
1 DELETED4 DELETED
2 DELETED5 DELETED
3 DELETED 6 DELETED
any idea where i went wrong?
opps
ok found it out
just replaced it as below -
-
open(FILE, ">file.txt");
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
-
print FILE ("$value");
-
print FILE" \t";
-
-
-
}
-
print FILE ("DELETED");
-
print FILE "\n";
-
}
-
-
Hate to be redundant or insignificant in my response, yet here is my take.
Based on my limited understanding and personal roadblocks.
1) Reading/Looping Through an XLS file to extract data with perl is a $bear = killer koala
2) My path of least resistance to overcome said $bear, has been to require the XLS file to be converted (via save as) to a CSV format.
O.K -- I too am almost sick at the lame a$$ed effort to be cute with $variables
couldn't resist...
knowing enoughing to be dangerous and not much more
Regards,
hutch
:-)
hi guys, i made some modification to my script above but quite puzzled again
well this time i got 2 sets of array
but i need to print them out in the same manner again like from excel it has:
1 4 DELETED
2 5 DELETED
3 6 DELETED
right the problem is the number are defined as cells from A1 to B3 as one array
and the deleted are taken from another cell say T1 to T3 in another array
so i got print out 2 arrays of info into text file in the manner:
1 4 DELETED
2 5 DELETED
3 6 DELETED
below is my code which i think is wrong when i tried to attempt to print the 2nd array out -
use Win32::OLE;
-
use Win32::OLE::Const 'Microsoft Excel';
-
-
# use existing instance if Excel is already running
-
eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
-
die "Excel not installed" if $@;
-
unless (defined $ex) {
-
$ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
-
or die "Oops, cannot start Excel";
-
}
-
-
-
$book = $ex->Workbooks->Open("C:\\Documents and Settings\\clong\\Desktop\\Convert to Text file\\@xlsFiles");
-
$sheet = $book->Worksheets(1);
-
-
-
$last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
-
$last_row = $last_row-14;
-
$total_run = $sheet -> Range("Z$last_row") -> {Value};
-
$last_row = $last_row-1;
-
$data = $sheet -> Range("A4:B$last_row") -> {Value};
-
$last_row = $last_row+4;
-
$deleted = $sheet -> Range("Z$last_row") -> {Value};
-
$last_row = $last_row-4;
-
$output = $sheet -> Range("T4:T$last_row") -> {Value};
-
-
-
open(FILE, ">file.txt");
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
-
print FILE ("$value \t");
-
-
}
-
foreach my $cell_ref (@$output) {
-
foreach my $value (@$cell_ref){
-
print FILE ("$value");}
-
}
-
print FILE ("\n");
-
-
}
-
print FILE ("TOTAL RUN: $total_run \n");
-
print FILE ("TOTAL DELETED: $deleted\n");
-
print FILE ("ROWS SELECTED: $total_run\n");
-
print FILE ("$last_row");
-
-
alright found out something...if i change the code as shown
i just need to know how to put $output into an array so it prints out but it must be the same format as i've describe as above, thanks
any suggestion? -
-
-
##start of conversion of excel to text file
-
-
-
use Win32::OLE;
-
use Win32::OLE::Const 'Microsoft Excel';
-
-
# use existing instance if Excel is already running
-
eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
-
die "Excel not installed" if $@;
-
unless (defined $ex) {
-
$ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
-
or die "Oops, cannot start Excel";
-
}
-
-
-
$book = $ex->Workbooks->Open("C:\\Documents and Settings\\clong\\Desktop\\Convert to Text file\\@xlsFiles");
-
$sheet = $book->Worksheets(1);
-
-
-
$last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
-
$last_row = $last_row-14;
-
$total_run = $sheet -> Range("Z$last_row") -> {Value};
-
$last_row = $last_row-1;
-
$data = $sheet -> Range("A4:B$last_row") -> {Value};
-
$output = $sheet -> Range("T4:T$last_row") -> {Value};
-
$last_row = $last_row+14;
-
$deleted = $sheet -> Range("Z$last_row") -> {Value};
-
-
-
-
open(FILE, ">file.txt");
-
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
-
-
print FILE ("$value \t");
-
}
-
print FILE ("$output");
-
print FILE ("\n");
-
-
}
-
-
-
-
-
print FILE ("TOTAL RUN: $total_run \n");
-
print FILE ("TOTAL DELETED: $deleted\n");
-
print FILE ("ROWS SELECTED: $total_run\n");
-
hm...any one has suggestions how i can print the 2nd array out properly?
i tried using push @array, value
and i got things like
1 4 deleted deleted deleted
2 5 deleted deleted deleted
3 6 deleted deleted deleted
but i just need one deleted
1 4 deleted
2 5 deleted
3 6 deleted
i tried accomplishing this using a counter below:
[code=perl]
hm...any one has suggestions how i can print the 2nd array out properly?
i tried using push @array, value
and i got things like
1 4 deleted deleted deleted
2 5 deleted deleted deleted
3 6 deleted deleted deleted
but i just need one deleted
1 4 deleted
2 5 deleted
3 6 deleted
i tried accomplishing this using a counter below: -
$last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
-
$last_row = $last_row-14;
-
$total_run = $sheet -> Range("Z$last_row") -> {Value};
-
$last_row = $last_row-1;
-
$data = $sheet -> Range("A4:B$last_row") -> {Value};
-
$last_row = $last_row+14;
-
$deleted = $sheet -> Range("Z$last_row") -> {Value};
-
-
-
-
open(FILE, ">file.txt");
-
-
foreach my $cell_ref (@$data) {
-
foreach my $value (@$cell_ref){
-
-
-
print FILE ("$value \t");
-
}
-
$output = $sheet -> Range("T$row") -> {Value};
-
print FILE ("$output");
-
print FILE ("\n");
-
$row++;
-
}
-
but the algorithm is a little slow..so i'm thinking of using arrays
but how can i get the 2nd array printed out correctly
pls help..thanks
i hope u all can see this thread...seems like it's not posted under new
well take yr time
thanks for the help in advance
ok i figured out found it somewhere
basically it's to print arrays into columns
so say @array1, @array2, @array3 into column
the below code works,
but can someone explain to me the principle behind this code how it works? -
-
while(@array1 || @array2 || @array3) {
-
printf FILE ("%-10s %-10s %-10s\n",
-
shift(@array1) || ' ',
-
shift(@array2) || ' ',
-
shift(@array3) || ' ',
-
);
-
}
ok i figured out found it somewhere
basically it's to print arrays into columns
so say @array1, @array2, @array3 into column
the below code works,
but can someone explain to me the principle behind this code how it works? -
-
while(@array1 || @array2 || @array3) {
-
printf FILE ("%-10s %-10s %-10s\n",
-
shift(@array1) || ' ',
-
shift(@array2) || ' ',
-
shift(@array3) || ' ',
-
);
-
}
The logic of this is code is like this:
- The code block in while loop as long as any of the three arrays exist i.e. if the arrays are of unequal size, the loop iterates until the last element in the largest array is processed.
- The printf() statement will format the output. In this case, a ten characters' space is left between the first characters of consecutive words.
- The three parameters to be printed are the elements of the 3 arrays.
- shift() statement keeps on removing the first element from the array. If any of the arrays is emptied, the shift() fails and the space character after OR(||) will be printed.
i see..ok
thanks for the enlightenment
cheers, :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ep |
last post by:
I need to put some text (and numbers) in Word and Excel. How do I put
that into them? Also how can I use the "hot keys" to navigate in
each.
Thanks
Ep
|
by: microsoft.news.com |
last post by:
I'm creating an Excel spreadsheet template that I want to populate from a
text file. I need to put certain data items in certain cells in the excel
spreadsheet. I need to populate it from code.
...
|
by: Mamatha |
last post by:
Hi
I want to export data from textfile to Excel sheet in
VB.NET application.When i copy the text file into Excel
sheet the data of text file are copied to only one cell of
the Sheet.
Now i...
|
by: Mamatha |
last post by:
Hi
When i clicked a button, i want to insert data from
listview in VB.NET to Excel sheet.
If you know the solution either above or below is ok for
me.
I know how to insert from a textfile,but...
|
by: Avi |
last post by:
I need to create a text file that has the data from the 10 tables in
the database. The number of fields in the tables exceeds 255 and so I
cannot make a new table with all the fields and then...
|
by: Justme |
last post by:
Novice programmer needs help with using fgets to read and ignore the
first two lines of a file. I've gone thru the previous posting
regarding fgets, but none of them seems to help my situation.
I...
|
by: kachokaratz |
last post by:
well i have a problem in my project..since the contents of my textfile is too long, i was wondering how to output the textfile in such a way that you can divide the contents to be able to output...
|
by: maryanncanor |
last post by:
Hi I would like to know if there is a macro that can export the data from a query to an output textfile that has NO QUOTES.
If I run the concatenated query Expr1: & & & & & & &
...
|
by: asedt |
last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile.
My problem is that i don't know how to...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |