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

  • Home
  • SEARCH
  • 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 8512661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:20:33+00:00 2026-06-11T04:20:33+00:00

I am currently exporting tenant data from my MySql database into an Excel spreadsheet,

  • 0

I am currently exporting tenant data from my MySql database into an Excel spreadsheet, using the code below.

<?php
    require_once('../../../includes/config.inc.php'); 

    require_once(MYSQL);

    $query = "SELECT * FROM tenant";        //Define the SQL query

    if ($result = $dbc->query($query))
    {
        $file_type = "vnd.ms-excel";
        $file_ending = "xls";

        header("Content-Type: application/$file_type");
        header("Content-Disposition: attachment; filename=tenant_data.$file_ending");
        header("Pragma: no-cache");
        header("Expires: 0");

        $seperator = "\t";  //define separator (defines columns in excel)

        $field_count = $result->field_count;

        for ($for_count = 1; $for_count < $field_count; $for_count++)   //start of printing column names as names of MySQL fields
        {
            $result->field_seek($for_count);

            $field_info = $result->fetch_field();

            $field_name = $field_info->name;

            $field_name_title = str_replace("_", " ", "$field_name");

            $field_array[] = $field_name;

            echo $field_name_title . $seperator; 
        }

        $result->field_seek(0);

        print("\n");        //end of printing column names

        while($row = $result->fetch_assoc())    //start while loop to get data
        {
            $schema_insert = "";

            for ($col_count = 0; $col_count < $field_count - 1; $col_count++)
            {
                $current_field = $field_array[$col_count];

                if ($row[$current_field] != "")
                {
                    $schema_insert .= "$row[$current_field]" . $seperator;
                }
                else
                {
                    $schema_insert .= "" . $seperator;
                } 
            }

            $schema_insert = str_replace($seperator . "$", "", $schema_insert);

            $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);            //Replaces \n or \r with a space

            $schema_insert .= "\t";

            print(trim($schema_insert));

            print "\n";
        }
    }
?>

The code currently works, however I have two problems:

1) I want the columns in the spreadsheet to autosize to the size of the content on creation.

2) In the data, there is a Phone field, where numbers tend to start with 0. As a result, the 0 tends to get missed off in the database, and I think the column needs specifying as a String.

Does anyone know how to do either of these things? I don’t want to use PHPExcel as it seems to be a massive overcomplication when this is all I want to do.

  • 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-11T04:20:35+00:00Added an answer on June 11, 2026 at 4:20 am

    In the absence of any answer, I eventually decided just to use the PHPExcel class.

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

Sidebar

Related Questions

I am currently exporting data from php to excel using the code as below:
I am currently exporting data from an excel import using excel interop. I've been
I'm currently exporting a database table with huge data (100000+ records) into an xml
Hi I am exporting my mysql data in excel format currently I have 14k+
Currently our application exporting large data to office 2003 xml spreadsheet format in server.
I currently have a sproc that is exporting data to a csv file using
I am currently exporting to Excel using the old HTML trick, where I set
I am currently exporting data from App Engine (to csv files) so that a
I am currently exporting Drupal data to an external source (XML) programmatically. However, I
Currently I am facing the tedious problem of exporting complete GridView data to 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.