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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • santhanalakshmi
    New Member
    • May 2009
    • 147

    #1

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

    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
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • chorny
      Recognized Expert New Member
      • Jan 2008
      • 80

      #3
      Is HTML table written with Perl code? If yes, just modify it to write in different format.

      Otherwise HTML::TableExtr act from CPAN may help you.

      P.S. Consider reading "Learning Perl" (no earlier than 4th edition) or "Beginning Perl".

      Comment

      • santhanalakshmi
        New Member
        • May 2009
        • 147

        #4
        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.

        Comment

        • santhanalakshmi
          New Member
          • May 2009
          • 147

          #5
          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.

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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

            Comment

            • santhanalakshmi
              New Member
              • May 2009
              • 147

              #7
              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.
              Code:
              #!/usr/bin/perl
              use warnings;
              use strict;
              2. Opening HTML Tag
              3. Opening Table Property (HTML)
              4. Connecting to my database:
              using my
              Code:
              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
              Last edited by numberwhun; Jan 11 '11, 04:29 PM. Reason: Please use code tags.

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                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?

                Comment

                • santhanalakshmi
                  New Member
                  • May 2009
                  • 147

                  #9
                  Code:
                  #!c:/perl/bin/perl
                  use CGI qw(:all);
                  $pdeptcode=uc(param('deptcode'));
                  
                  $pperiod=uc(param('period'));
                  
                  $pprogramme=uc(param('prg'));
                  
                  
                  print "Content-type: text/html\n\n";
                  print "<body bgcolor=\"#ffcccc\">";
                  
                  use DBI;
                  my $dbh = DBI->connect("DBI:ODBC:aaaa","bbb","cccc") or die "Can not connect: $DBI::errstr\n"; 
                  
                  my $sth;
                  
                  print "<center><table border=1></center>";
                  print "<tr><th>SlNo</th><th>Roll Number</th><th>Name</th><th>Branch Name</th></tr>";
                  
                  	if (($pperiod eq 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme ne 'ALL'))
                  	{
                       	  	$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");
                       		$sth->execute($pdeptcode,$pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
                        }
                  
                        if (($pperiod ne 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme ne 'ALL'))
                  	{
                             
                       	  	$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");
                       		$sth->execute($pperiod,$pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
                        }
                         if (($pperiod ne 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme eq 'ALL'))
                  	{
                             
                       	  	$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");
                       		$sth->execute($pperiod,$pdeptcode) or die "Cant execute SQL: $DBI::errstr\n";
                        }
                  
                  
                         if (($pperiod eq 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme ne 'ALL'))
                  	{
                             
                       	  	$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");
                       		$sth->execute($pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
                        }
                  
                  
                        if (($pperiod eq 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme eq 'ALL'))
                  	{
                             
                       	  	$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");
                       		$sth->execute($pdeptcode) or die "Cant execute SQL: $DBI::errstr\n";
                        }
                  
                  
                         if (($pperiod ne 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme eq 'ALL'))
                  	{
                             
                       	  	$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");
                       		$sth->execute($pperiod) or die "Cant execute SQL: $DBI::errstr\n";
                        }
                  
                  
                         if (($pperiod eq 'ALL') && ($pdeptcode eq 'ALL') && ($pprogramme eq 'ALL'))
                  	{
                              $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");
                       		$sth->execute or die "Cant execute SQL: $DBI::errstr\n";
                        }
                  
                         if (($pperiod ne 'ALL') && ($pdeptcode ne 'ALL') && ($pprogramme ne 'ALL'))
                  	{
                  
                              $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");
                       		$sth->execute($pperiod,$pdeptcode,$pprogramme) or die "Cant execute SQL: $DBI::errstr\n";
                        }
                  
                  	while ( @row = $sth->fetchrow_array())
                  	{
                            $j=$j+1;
                            print "<tr><td>$j</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>";
                  	}
                  
                  print "</table>";
                  print "<br>";
                  print "</html>";
                  Hi, please see my coding. Thanks for your effort.

                  Comment

                  Working...