473,378 Members | 1,438 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,378 software developers and data experts.

MySQL database to Excel spreadsheet report

Hi everybody.

I am new in PERL language and working in it since little hours, actually i am still a trainee and i need help please.

In fact, i need a script in PERL that enables me to retrieve data from a table from the database in MySQL and write its contents in an excel sheet file as an excel repot.

I have done a script but it is still in draft cause it's not working.

Can somebody help me please.

Thanks a lot.
Jul 31 '07 #1
5 6119
numberwhun
3,509 Expert Mod 2GB
Well then, Welcome to Perl!!!

Please note, when you post for assistance, be sure to always post the codd that you have tried thus far. This is not a script writing service. We are happy to help you tweak and debug your code, but won't do all of the legwork for you.

I can throw this bit of information your way though. To interact with a database, you will want to use the DBI module. Also, for writing to/creating an Excel spreadsheet, I believe you will want to read up on the Spreadsheet::SimpleExcel module. I don't have experience with that last one, but the web page is quite detailed.

Regards,

Jeff
Jul 31 '07 #2
Hi again everybody, according to my first request of help, i am puttig the code that i have done until now. Actually this is the forst code i do in Perl so i suppose there could be several faults.

I was supposed to retreive the data from a MySQL database table. this data should be viewed in an excel sheet report.

This is what i have done until now and i am blocked, i really need help please;

//connect to MySQL database

Expand|Select|Wrap|Line Numbers
  1. !/usr/bin/perl -w
  2. use strict;
  3. use DBI;
  4. use Spreadsheet::SimpleExcel;
  5.  
  6. print "Available Database Drivers:\n\n";
  7. print join("\n", DBI->availableDrivers()), "\n\n";
  8.  
  9. my %attr => (RaiseError => 0);
  10. my $dbh = DBI -> connect("DBI:mysql:dbname=bugs:localhost", "root", "root")
  11.     or die("Error: $DBI::errstr");
  12.  
  13. my $excel_file = "C:/Perl/Bugs/reports";
  14.  
  15. //-e checks to see if the file exists
  16.  
  17. if (-e "$excel_file") {
  18.     #open Excel file
  19.     my $Book = $Excel->Workbooks->Open("$excel_file");
  20.  
  21. } else {
  22.  
  23.     //paste code here to create a new file
  24.     #create a new instance
  25.     my $excel = Spreadsheet::SimpleExcel->new();
  26.     $excel->{Visible}=1;
  27.     $excel->{SheetsInNewWorkBook} = 1;
  28.     $workbook = $excel->Workbooks->Add();
  29.     $worksheet = $Workbook->Worksheets(1);
  30.     $worksheet->{Name} = "Report";
  31.     my $sth = $dbh->prepare("select * from bugs");
  32.     $sth->execute() or die $dbh ->errstr;
  33.     while ($sth->fetch){
  34.         Print "\t \n";
  35.     }
  36.     $sth->finish();
  37.  
  38.     #sort data of worksheet - ASC or DESC
  39.     $excel->sort_data("Name of worksheet",0, "ASC");
  40.  
  41.     #create the spreadsheet
  42.     $excel->output();
  43.  
  44.     #print sheet-names
  45.     print join(", ", $excel->sheets()), "\n";
  46.  
  47.     #get the result as a string
  48.     my $spreadsheet = $excel->output_as_string();
  49.  
  50.     #print result into a file and handle error
  51.     $excel->output_to_file("report.xls") or die $excel->errstr();
  52.     $workbook->SaveAs($excel_file);
  53.     $dbh->disconnect();
  54.  
  55.  
Aug 2 '07 #3
Hi again everybody, according to my first request of help, i am putting the code that i have done until now. Actually this is the first code i do in Perl so please excuse me for my faults.

I was supposed to retreive the data from a MySQL database table. this data should be viewed in an excel sheet report.

This is what i have done until now and i am blocked, i really need help please;

Expand|Select|Wrap|Line Numbers
  1.  
  2. # //connect to MySQL database
  3.  
  4. #!/usr/bin/perl -w
  5. use strict;
  6. use DBI;
  7. use Spreadsheet::SimpleExcel;
  8.  
  9. print "Available Database Drivers:\n\n";
  10. print join( "\n", DBI->availableDrivers() ), "\n\n";
  11.  
  12. my %attr => ( RaiseError => 0 );
  13. my $dbh = DBI->connect( "DBI:mysql:dbname=bugs:localhost", "root", "root" )
  14.   or die("Error: $DBI::errstr");
  15.  
  16. my $excel_file = "C:/Perl/Bugs/reports";
  17.  
  18. # //-e checks to see if the file exists
  19.  
  20. if ( -e "$excel_file" ) {
  21.  
  22.     #open Excel file
  23.     my $Book = $Excel->Workbooks->Open("$excel_file");
  24.  
  25. }
  26. else {
  27.  
  28.     #  //paste code here to create a new file
  29.     #create a new instance
  30.     my $excel = Spreadsheet::SimpleExcel->new();
  31.     $excel->{Visible}             = 1;
  32.     $excel->{SheetsInNewWorkBook} = 1;
  33.     $workbook                     = $excel->Workbooks->Add();
  34.     $worksheet                    = $Workbook->Worksheets(1);
  35.     $worksheet->{Name}            = "Report";
  36.     my $sth = $dbh->prepare("select * from bugs");
  37.     $sth->execute() or die $dbh->errstr;
  38.  
  39.     while ( $sth->fetch ) {
  40.         Print "\t \n";
  41.     }
  42.     $sth->finish();
  43.  
  44.     #sort data of worksheet - ASC or DESC
  45.     $excel->sort_data( "Name of worksheet", 0, "ASC" );
  46.  
  47.     #create the spreadsheet
  48.     $excel->output();
  49.  
  50.     #print sheet-names
  51.     print join( ", ", $excel->sheets() ), "\n";
  52.  
  53.     #get the result as a string
  54.     my $spreadsheet = $excel->output_as_string();
  55.  
  56.     #print result into a file and handle error
  57.     $excel->output_to_file("report.xls") or die $excel->errstr();
  58.     $workbook->SaveAs($excel_file);
  59.     $dbh->disconnect();
  60. }
  61.  
Aug 2 '07 #4
numberwhun
3,509 Expert Mod 2GB
Hi again everybody, according to my first request of help, i am putting the code that i have done until now. Actually this is the first code i do in Perl so please excuse me for my faults.

I was supposed to retreive the data from a MySQL database table. this data should be viewed in an excel sheet report.

This is what i have done until now and i am blocked, i really need help please;

Expand|Select|Wrap|Line Numbers
  1.  
  2. # //connect to MySQL database
  3.  
  4. #!/usr/bin/perl -w
  5. use strict;
  6. use DBI;
  7. use Spreadsheet::SimpleExcel;
  8.  
  9. print "Available Database Drivers:\n\n";
  10. print join( "\n", DBI->availableDrivers() ), "\n\n";
  11.  
  12. my %attr => ( RaiseError => 0 );
  13. my $dbh = DBI->connect( "DBI:mysql:dbname=bugs:localhost", "root", "root" )
  14.   or die("Error: $DBI::errstr");
  15.  
  16. my $excel_file = "C:/Perl/Bugs/reports";
  17.  
  18. # //-e checks to see if the file exists
  19.  
  20. if ( -e "$excel_file" ) {
  21.  
  22.     #open Excel file
  23.     my $Book = $Excel->Workbooks->Open("$excel_file");
  24.  
  25. }
  26. else {
  27.  
  28.     #  //paste code here to create a new file
  29.     #create a new instance
  30.     my $excel = Spreadsheet::SimpleExcel->new();
  31.     $excel->{Visible}             = 1;
  32.     $excel->{SheetsInNewWorkBook} = 1;
  33.     $workbook                     = $excel->Workbooks->Add();
  34.     $worksheet                    = $Workbook->Worksheets(1);
  35.     $worksheet->{Name}            = "Report";
  36.     my $sth = $dbh->prepare("select * from bugs");
  37.     $sth->execute() or die $dbh->errstr;
  38.  
  39.     while ( $sth->fetch ) {
  40.         Print "\t \n";
  41.     }
  42.     $sth->finish();
  43.  
  44.     #sort data of worksheet - ASC or DESC
  45.     $excel->sort_data( "Name of worksheet", 0, "ASC" );
  46.  
  47.     #create the spreadsheet
  48.     $excel->output();
  49.  
  50.     #print sheet-names
  51.     print join( ", ", $excel->sheets() ), "\n";
  52.  
  53.     #get the result as a string
  54.     my $spreadsheet = $excel->output_as_string();
  55.  
  56.     #print result into a file and handle error
  57.     $excel->output_to_file("report.xls") or die $excel->errstr();
  58.     $workbook->SaveAs($excel_file);
  59.     $dbh->disconnect();
  60. }
  61.  
Ok, so have you run this to see if it works? If so, what were the results? Were there any errors? Please provide any information you can.

Regards,

Jeff
Aug 3 '07 #5
archulu
34
hay i am sending sample code. i think its help to u(even now also ur waiting for this)


package test::excel_back;
use DBI;
use Apache::File;
use Apache::Request;
use Spreadsheet::WriteExcel;
use MIME::Types;

my($dbh,$sth,@res);

sub prepare_excel {
my $name= 'archulu';
my $workbook = Spreadsheet::WriteExcel->new("/tmp/rekha.xls");
my $worksheet = $workbook->add_worksheet("$name");
my $format = $workbook->add_format();
my $format1= $workbook->add_format();
my $row=1;

$worksheet->set_column('E:E',16);
$worksheet->set_column('F:F',12);

$format->set_align('center');
$format->set_color('pink');
$format->set_bold();
$format1->set_color('green');
$format1->set_align('center');

$worksheet->write($row,4,"BankId",$format);
$worksheet->write($row,5,"Name",$format);
$worksheet->write($row,6,"place",$format);


$row++;
while(@res = $sth->fetchrow_array)
{
$worksheet->write($row,4,"$res[0]",$format1);
$worksheet->write($row,5,"$res[1]",$format1);
$worksheet->write($row,6,"$res[2]",$format1);
$row++;
}
$workbook->close();
}

sub handler {

my $r=Apache::Request->new(shift);
my $value=$r->param('cb');
$dbh=DBI->connect("dbi:Pg:dbname=emp;host=localhost","archa na","");
$sth=$dbh->prepare("select * from bank");
$sth->execute();

if($value eq 'getExcel')
{
&prepare_excel();
my $fh = Apache::File->new("/tmp/rekha.xls");
$r->send_http_header('application/vnd.ms-excel');
my $fd=$r->send_fd($fh);
return;
}
return OK;
}
1;
Sep 27 '07 #6

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

Similar topics

8
by: Johnny | last post by:
I was looking for information on how to import an excel file or other text file into a MySql database. I have done this with both access and SQL Server and am looking for a way to do this in...
3
by: bawar | last post by:
Hey, I can say I belong to the newbies in this subject. I own my own website with mySQL-databases, but now I have to show one table of those mySQL-databases in an excel worksheet. It is the...
4
by: ctkevin | last post by:
I am an experienced Access user but recently moved an Access database to the web for my employer. We use a host that does not allow the use of data access pages. Is there anyway to pre-build a...
2
by: Anne Sachleben via AccessMonster.com | last post by:
I am using the TransferSpreadsheet function to export a query result to a specific worksheet in an Excel file titled "report". I want the result to be exported to the worksheet titled "facts". ...
6
by: DeniseY | last post by:
I have an Access report that is created on the fly by the user selecting the fields to be included. The Access report comes out fine, but I want it to automatically output to an Excel spreadsheet....
2
by: nofear | last post by:
I used to export my reports as snapshot but now I have to export them to Excel When I export my report to a Excel Spreadsheet the report header and footer are not included Only the data gets...
1
by: Sport Girl | last post by:
Hi everybody , i have the task of developing in Perl a script that retrieves data from 3 tables ( bugs, profiles, products) from a MySQL database called bugs and display them in an excel sheet...
1
by: Master Ken | last post by:
Hi All, I'm very new to C# and ASP and I am asking for some help as I don't really know where to start on this project. Some background info first I run a report each week which outputs data...
2
by: Alan M Dunsmuir | last post by:
A client has sent me a (Windows) Excel Spreadsheet (.xls) file containing a block of data he wants included as a table in a MySQL database in a (Linux-based) PHP/MySQL Web application I'm...
1
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...
0
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.