469,343 Members | 5,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How to export form data to CSV with perl-cgi ?

147 100+
Hello,

I currently have an HTML table with data in it. The data comes from SQL2000. I want to take my HTML table and allow users to hit an "Export to CSV" button which will allow them to download the data in the table to a file.I am unfamiliar with this, and haven't done much file writing/reading stuff in Perl. I've searched the web and have been unable to find a good solution/example for this problem. Help is appreciated! thanks...give me coding on this
Jan 10 '11 #1
8 5157
numberwhun
3,503 Expert Mod 2GB
I would certainly search Google to find information on reading/writing files. Its not that bad, really.

You may also want to investigate the use of modules to help with your solution writing. If you get stuck, post your questions here and we will help you.

Regards,

Jeff
Jan 10 '11 #2
chorny
80 Expert
Is HTML table written with Perl code? If yes, just modify it to write in different format.

Otherwise HTML::TableExtract from CPAN may help you.

P.S. Consider reading "Learning Perl" (no earlier than 4th edition) or "Beginning Perl".
Jan 11 '11 #3
santhanalakshmi
147 100+
Hi,
I don't know, how to start? For CSV export on client machine, there is need to use DBI::CVS.

My Perl output is in table format(used HTML also, datas coming from SQL 2000). I need this output in my client machine as CSV. Your Help is in need. Thanks in advance.
Jan 11 '11 #4
santhanalakshmi
147 100+
Hi,
Thanks for your reply. Yes, it is HTML Table. Hi,
I don't know, how to start? For CSV export on client machine, there is need to use DBI::CVS.

My Perl output is in table format(used HTML also, datas coming from SQL 2000). I need this output in my client machine as CSV.
Jan 11 '11 #5
numberwhun
3,503 Expert Mod 2GB
Ok, so the data was pulled from the database prior to being put into table format. You should be able to take that data and also output it to CSV format.

If there is already a DBD module used to connect to the SQL2000 database and pull the data, I don't know what the implications would be of attempting to use a second DBD module. It may possibly cause a conflict.

Instead, you may want to look into using something like the Class::CSV module to write your CSV file instead. I have never used it, but just searched for csv on search.cpan.org and found it.

If I were you, I would also look into the Learning Perl book that @chorny suggested. IMHO, it is one of the better books out there as a beginning Perl tutorial.

If you want further help with any code, it will be of great help if you could paste your code here so we can see it and help you with it.

Regards,

Jeff
Jan 11 '11 #6
santhanalakshmi
147 100+
Hi,
Thanks. Please see my output. Currently i am getting this output in my form:

sno name location cgpa
1 sss chennai 8.9
2 aaa bombay 7.2


My coding:
1.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4.  
2. Opening HTML Tag
3. Opening Table Property (HTML)
4. Connecting to my database:
using my
Expand|Select|Wrap|Line Numbers
  1. sth->connect(DBI:dns:loginname:password)
5. Picking up the data from my database.
6. Doing some operation here
7. Submit Button
8. Closing Table Tag (is in HTML)
9. Closing HTML.

By clicking the Submit Button, i am getting the following output as shown above in all my client machine.

I need one more button to generate this output in CSV file(this should work globally all my client machines). Please do a favour or give me a solution to proceed this. thanks
Jan 11 '11 #7
numberwhun
3,503 Expert Mod 2GB
Please know that when I said, paste your code in, I was referring to the actual code, not the code walk through. Do you actually have any code written or are you not really able to provide it?
Jan 11 '11 #8
santhanalakshmi
147 100+
Expand|Select|Wrap|Line Numbers
  1. #!c:/perl/bin/perl
  2. use CGI qw(:all);
  3. $pdeptcode=uc(param('deptcode'));
  4.  
  5. $pperiod=uc(param('period'));
  6.  
  7. $pprogramme=uc(param('prg'));
  8.  
  9.  
  10. print "Content-type: text/html\n\n";
  11. print "<body bgcolor=\"#ffcccc\">";
  12.  
  13. use DBI;
  14. my $dbh = DBI->connect("DBI:ODBC:aaaa","bbb","cccc") or die "Can not connect: $DBI::errstr\n"; 
  15.  
  16. my $sth;
  17.  
  18. print "<center><table border=1></center>";
  19. print "<tr><th>SlNo</th><th>Roll Number</th><th>Name</th><th>Branch Name</th></tr>";
  20.  
  21.     if (($pperiod eq 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme ne 'ALL'))
  22.     {
  23.                $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.deptcode=? and a.programme=? and  a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  24.              $sth->execute($pdeptcode,$pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
  25.       }
  26.  
  27.       if (($pperiod ne 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme ne 'ALL'))
  28.     {
  29.  
  30.                $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.semester=? and a.programme=? and  a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  31.              $sth->execute($pperiod,$pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
  32.       }
  33.        if (($pperiod ne 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme eq 'ALL'))
  34.     {
  35.  
  36.                $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.semester=? and a.deptcode=? and  a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  37.              $sth->execute($pperiod,$pdeptcode) or die "Cant execute SQL: $DBI::errstr\n";
  38.       }
  39.  
  40.  
  41.        if (($pperiod eq 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme ne 'ALL'))
  42.     {
  43.  
  44.                $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.programme=? and  a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  45.              $sth->execute($pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
  46.       }
  47.  
  48.  
  49.       if (($pperiod eq 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme eq 'ALL'))
  50.     {
  51.  
  52.                $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.deptcode=? and  a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  53.              $sth->execute($pdeptcode) or die "Cant execute SQL: $DBI::errstr\n";
  54.       }
  55.  
  56.  
  57.        if (($pperiod ne 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme eq 'ALL'))
  58.     {
  59.  
  60.                $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.semester=? and  a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  61.              $sth->execute($pperiod) or die "Cant execute SQL: $DBI::errstr\n";
  62.       }
  63.  
  64.  
  65.        if (($pperiod eq 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme eq 'ALL'))
  66.     {
  67.             $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  68.              $sth->execute or die "Cant execute SQL: $DBI::errstr\n";
  69.       }
  70.  
  71.        if (($pperiod ne 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme ne 'ALL'))
  72.     {
  73.  
  74.             $sth=$dbh->prepare("select a.rollno,a.name,b.branname from stuacmst a,brnchmst b where a.semester=? and a.deptcode=? and a.programme=? and  a.studstat in ('C','F') and a.brancode=b.brancode order by a.rollno");
  75.              $sth->execute($pperiod,$pdeptcode,$pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
  76.       }
  77.  
  78.     while ( @row = $sth->fetchrow_array())
  79.     {
  80.           $j=$j+1;
  81.           print "<tr><td>$j</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>";
  82.     }
  83.  
  84. print "</table>";
  85. print "<br>";
  86. print "</html>";
  87.  
  88.  
Hi, please see my coding. Thanks for your effort.
Jan 12 '11 #9

Post your reply

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

Similar topics

3 posts views Thread by DMina | last post: by
1 post views Thread by Mindaugas | last post: by
2 posts views Thread by Sashi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.