Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:22:13+00:00 2026-06-13T09:22:13+00:00

I need to Export mysql table into Excel Format. I Have tried but still

  • 0

I need to Export mysql table into Excel Format. I Have tried but still I couldn’t get the correct format. can anyone please help me to fix this. I have attached my php function below.

<body>
<?php

function export_excel_csv()
{
    $conn = mysql_connect("localhost","itsup739_uaquiz","itsup739_uaquiz");
    $db = mysql_select_db("itsup739_uaquizDB",$conn);

    $sql = "SELECT * FROM results";
    $rec = mysql_query($sql) or die (mysql_error());

    $num_fields = mysql_num_fields($rec);

    for($i = 0; $i < $num_fields; $i++ )
    {
        $header .= mysql_field_name($rec,$i)."\\t";
    }

    while($row = mysql_fetch_row($rec))
    {
        $line = '';
        foreach($row as $value)
        {                                           
            if((!isset($value)) || ($value == ""))
            {
                $value = "\\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\\t";
            }
            $line .= $value;
        }
        $data .= trim( $line ) . "\\n";
    }

    $data = str_replace("\\r" , "" , $data);

    if ($data == "")
    {
        $data = "\\n No Record Found!\n";                       
    }

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=reports.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\\n$data";
    //echo $header;
}
export_excel_csv();
?>

Please Follow the link to see the output.

http://itsupportsrilanka.com/uaquiz/admin/export.php

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:22:14+00:00Added an answer on June 13, 2026 at 9:22 am

    It’s actually really really easy, to export to excel.

    Just create your content as normal HTML table like this:

    $contents = '<table>
      <tr>
        <td>Row 1</td>
        <td>Row 2</td>
      </tr>
      <tr>
        <td>bLAH BLAH</Td>
        <td>Blah Blah</td>
      </tr>
    </table>';
    

    Now set the headers as excel document and output your table:

    $filename = 'my_excel_file.xls';
    header( 'Content-type: application/ms-excel' );
    header( 'Content-Disposition: attachment; filename=' . $filename );
    
    echo $contents;
    

    That’s pretty much it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have around 150 MySQL databases, I need to export 1 table from each
MYSQL Database: I have a table of data that I need to put into
I have done a script in PHP to export a MySQL table into a
how can I export a SQL Server table to Mysql ? I guess I
How can I export the contents of a mysql table to xml using php?
I need to export some data from SAS to CSV, so that I can
I need to export a gridview to excel, I put the return html code
I made a dll in c# but i need to export the functions. Is
I have a simple class for use on JNI, which i need to export
I have some data in one of my mysql table stored as utf8. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.