i am trying to use Spreadsheet::WriteExcel. But how do I break it down and put it in rows? How do i create a table and also how can i link the data from two different queries to enter all values in one table?
Sorry for my ignorance but i am new at perl and dont have much experience with it.
You can create a new excel file using the module and write on it.
Actually you can create a file an import it to excel..
use Spreadsheet::WriteExcel;
$output_file="myfile.xls";
my $workbook = Spreadsheet::WriteExcel->new($output_file);
die "Problems creating new Excel File: $! "unless defined $workbook;
my $worksheet = $workbook->add_worksheet();
open INPUT,"/m/home/dmazzini/myresult.log" or die "Couldn't open file: $!";
$worksheet->write($.-1,0,[split]) while <INPUT>;
As an alternative to using Perl, Excel might have a facility similar to the more advanced OpenOffice, where you can save an SQL query and run it any time, importing the data into a spreadsheet with drag-n-drop.
What I did was stored the result from the query straight into a hash of array and then printed it in excel using WriteExcel.
foreach $record (@output) {
But this method, I have to run each query and then separately store the result in another hash and write it out again, Hence the code becomes very repetitive.
Also, the result of my first query looks something like this
and then using spreadheet::writeexcel module to convert it to excel:
#!/opt/nokianms/bin/perl
# From csv to excel
use Spreadsheet::WriteExcel;
open (CSVFILE, "myoutput.csv") or die "myoutput.csv: $!";
my $excel = Spreadsheet::WriteExcel->new("myoutput.xls");
my $worksheet = $excel->add_worksheet();
my $row = 0;
my $col;
while (<CSVFILE>) {
chomp;
my @Fld = split('\,', $_);
$col = 0;
foreach my $token (@Fld) {
$worksheet->write($row, $col, $token);
$col++;
}
$row++;
}
ok so how would i import my data from 3 MySQL queries into a csv file? Sorry, I don't know how to do that.
Would i run query 1
append to myoutput.csv file
query 2
append again?
query 3
append again?
the problem is that they are three separate queries as opposed to one. SO initially I was putting them in three different worksheets, but now i want to be able to put them all in one worksheet in One table.
So the problem is that you need to group by A.B,C or D etc. You could create your own structure (hash of arrays where key is the group and values the other variables) where you can group the result.
Other solution:
I guess that you could use SQL and make a relation between tables. It seems to be that group is the primary key. And then it will produce the output already grouped.
OK so when i run the query, how do i get the results in a csv file?
I will try and group everything in One table.
While browsing cpan, i also found a module called, Spreadsheet::WriteExcel::FromDB
This takes the entire table and prints it in excel. You may choose to ignore columns or put where conditions.
Its pretty useful.
I can get link tables through mysql and get everything into one table and print it to excel using Spreadsheet::WriteExcel::FromDB
It becomes very straightforward. My only problem now is that it would repeat the data for some of the columns because of the linking. So i get results like that
A B C D E F
hockey 16 5 2 /home 69
hockey 16 5 2 /apps 38
javafea 59 10 14 / 24
javafea 59 10 14 /var 16
javahab 9 1 3 / 42
javahab 9 1 3 /var 12
How do i get rid of repetitive results in excel using perl?
Any ideas?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.