How can i convert and download database table to excel?

Discussion in 'PHP' started by HussainMHB, Apr 29, 2013.

  1. #1
    I'm need of downloading my table to excel at the end of every single month. How could i do that? Please let me know that with some graspable example or link. I'll so grateful.
     
    HussainMHB, Apr 29, 2013 IP
  2. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #2
    CSV (comma seperated values) format is an option, You can import this format in excel or any spreadsheet application.

    <?php
     
    $data[] = 'id,first_name,last_name';
     
    $result = mysql_query('SELECT * FROM table WHERE 1=1');
    while ($row = mysql_fetch_assoc($result))
        $data[] = $row['id'] . ',' . $row['first_name'] . ',' . $row['last_name'];
     
    print implode($data, "\r\n");
     
    ?>
    PHP:
     
    Hamidsam, Apr 29, 2013 IP
  3. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    Thanks man. I can understand what you're trying to say. But how can i download that? I need to put just one link in my project after clicking, that will download the respective table into csv.
     
    HussainMHB, Apr 29, 2013 IP
  4. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #4
    Put this at the beginning of the code:
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename=example.csv');
    header('Pragma: no-cache');
    PHP:
     
    Hamidsam, Apr 29, 2013 IP
  5. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #5
    How can we connect the former coding with the current header coding
    it seems to be no connection with both of them.
    Sorry i actually can't grasp it. please let me know.
     
    HussainMHB, Apr 29, 2013 IP
  6. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    No need man further information.
    Thanks very much
    I took a trial with this. It works great.
    Again thank you very much...
     
    HussainMHB, Apr 29, 2013 IP
  7. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #7
    I've got an another problem.
    When i click download button, the html coding are also downloaded within that excel sheet if i use html tags in that php page.

    What should i do now to get rid of html tags in excel?
    Thank. Hussain
     
    HussainMHB, May 1, 2013 IP
  8. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #8
    Put the above code at the beginning of your file (before printing anything in page). After printing the csv results end up the process using "exit" or "die()":
    
    exit;
    
    PHP:
     
    Hamidsam, May 1, 2013 IP
  9. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #9
    Thanks
    I've one function in which i wrote the csv coding. But before csv coding i also need to write some html coding here.
    <?php ......
    public function excel()
    {
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename=example.csv');
    header('Pragma: no-cache');
    }
    ........?>
    this function is called when my all process are finished
    and then all details are stored in excel
    i took a trial here all details are stored but in addition the tags that i used in that php page are also displayed
    in that excel sheet?
    What should i do now?
     
    HussainMHB, May 1, 2013 IP
  10. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #10
    In this case you need to put the csv exporting codes to another file like "export.php", and redirect the main file to export.php when processing and storing is done.

    PHP does not allow change headers when anything is printed in the result.
     
    Hamidsam, May 1, 2013 IP
  11. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #11
    So noway for this problem rather than separating the csv coding into a new file.
    Ok dude... Thank you very much.
     
    HussainMHB, May 2, 2013 IP
  12. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #12
    Hi man
    Another simple doubt
    Can i apply some colors to respective cell that i want to fill
    if its feasible, then please let me know...
     
    HussainMHB, May 4, 2013 IP
  13. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #13
    Anyone out here to help me?
    Please tell me how to apply colors when i download excel sheet from table.
    Thanks.
     
    HussainMHB, May 8, 2013 IP
  14. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #14
    CSV format does not support any text formating.
    You need a library to export output as excel spreadsheet. Try PHPExcel.
     
    Hamidsam, May 8, 2013 IP
  15. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #15
    You mean i just remove the csv coding and try to output my table as properly excel and also apply color to respective cells that we wanted.
     
    HussainMHB, May 8, 2013 IP
  16. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #16
    If you want have text formatting in the output, yes.
    CSV just show rows and cells, no more.
     
    Hamidsam, May 8, 2013 IP
  17. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #17
    header('Content-Type: application/xls');
    header('Content-Disposition: attachment; filename=example.xls');
    header('Pragma: no-cache');
    The keyword xls in first and second line output our details as excel sheet?
    Thanks
     
    HussainMHB, May 8, 2013 IP
  18. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #18
    I think PHPExcel will do this.

    Anyway:
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
    header("Cache-Control: max-age=0");
    PHP:
     
    Hamidsam, May 8, 2013 IP
  19. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #19
    Ok. I'll try this method and see how it shows.
    Thanks friend.
     
    HussainMHB, May 8, 2013 IP
  20. E_books

    E_books Banned

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #20
    If you are using phpmyadmin you will see that its allowing you to export to csv file and it can be used in excel directly
     
    E_books, May 10, 2013 IP
    Pro Designz likes this.