'exporting php data into excel in text data type

I want to export my php web page to excel.

For that, I have found the following code which works but it has an issue.

Values in a column start with something like '00003421' in php but when it is exported in excel, it shows only '3421' in the cell. It ignores the zero values. Moreover, I want them in text data type.

How can I export the data in plain text format as it is (including zeroes)?

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=export.xls");
header("Content-Transfer-Encoding: BINARY");

It's tabular data with 4 columns and around 20,000 rows.



Solution 1:[1]

The 0 is just hide if your data type of the cell is not text, select all the cells in excel, and set the data type to text.

Or try add a single quote to every value in php, '00003421' to '\'00003421', excel will know that this is text data type if the number begin with a single quote.

Solution 2:[2]

I have solved this by the below way.

You can try this

<?php
$number='00003421';
echo "<td>=\"$number\"</td>";
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xdazz
Solution 2 Siva