Error exporting to excel in PHP - php

When I execute this:
<?php
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
$flag = false;
foreach($data as $row) {
if(!$flag) {
echo implode("\t", array_keys($row)) . "\n";
$flag = true;
}
array_walk($row, __NAMESPACE__ . '\cleanData');
echo implode("\t", array_values($row)) . "\n";
}
exit;
I got error like below when am opening the downloaded .xls file.
The file You are trying to open Facemappsoft.xls is in a different format than file extension
Can any one help me to resolve this right now?

You are generating a tab-separated values file and giving it a Excel 97-2003 compatible file extension (*.xls).
Excel is complaining about this mismatch when opening the file.
To fix the issue, you will need to give the file an appropriate extension (*.tsv) and update the Content-Type header to one properly describing the file format, such as:
Content-Type: text/tab-separated-values

Related

Multiple Error Messages after exporting excel from php

Error Message while opening the file
Second error message when trying to update the file...
Code in PHP
<?php
$filename = "test.xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
echo implode("\t", ["Username", "Order Number"]) . "\n";
echo implode("\t", ["wswswsw#s.com", "1234454542122232"]) . "\n";
exit();
?>
Am I missing anything?
This code may be able to assist you in finding a solution to your issue. If it doesn't work as expected, there may be other options that you can try.
<?php
$filename = "test.xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
// Generate the data for the file
$data = [
["Username", "Order Number"],
["wswswsw#s.com", "1234454542122232"]
];
// Use a loop to generate the rows of the file
foreach ($data as $row) {
// Use implode() to concatenate the values in the row into a single string
echo implode("\t", $row) . "\n";
}
exit();
?>

Getting warning while opening Excel file after writing a table to it using PHP

I have the following code which writes a table to Excel file. Once The file gets downloaded but while opening it, getting warning
The file format and Extension of file.xls do not match. The file could
be corrected or unsafe. Unless you trust its source don't open it.
How to resolve this issue?
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=$file_name".date("d_m_Y_H_i_s").".xls");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
ob_clean();
// $res in next line will have a table which I get after processing.
$row = $res->getRowAssoc();
$heading = false;
while ($row != null)
{
if(!$heading) {
for ($ii = 0; $ii < $res->getColumnCount(); $ii++)
{
$field_name = $res->getColumnName($ii);
$field_names[$ii] = $util->get_mls_text($field_name);
}
echo implode("\t", $field_names) . "\r\n";
}
$heading = true;
echo implode("\t", $row) . "\r\n";
$row = $res->getRowAssoc();
}

instead of generating .xls file i am getting .php file on server side

I'm using following headers to generate excel file from database MySQL using PHP
header("Content-type: application/octet-stream");
header("Content-Type: application/vnd-ms-excel");
header("Content-disposition: attachments;filename=xxx.xls");
in localhost the output is xxx.xls which is correct file but in the server side i am getting xxx.php file (i am using a cpanel account for ftp)
guys please help me with a solution
simply use
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
Simply you can use the following php code.
You have to give an array with key value pair
$data = array("Name"=> "foo", "age" => 25);
$filename = "My File Name" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
foreach($data as $row) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\n";
$flag = true;
}
foreach ($row as $value){
echo $value;
echo "\t";
}
echo "\n";
}
exit;

php download file: header()

I need some eduction please.
At the end of each month, I want to download some data from my webserver to my local PC.
So, I've written a little script for that, which selects the data from the DB.
Next, I want to download it.
I've tried this:
$file=$month . '.txt';
$handle=fopen($file, "w");
header("Content-Type: application/text");
header("Content-Disposition: attachment, filename=" . $month . '.txt');
while ($row=mysql_fetch_array($res))
{
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
If I run this, then the file is created, but my file doesn't contain the data that I want. Instead I get a dump from the HTML-file in my browser..
What am I doing wrong..
Thanks,
Xpoes
Below script will help you download the file created
//Below is where you create particular month's text file
$file=$month . '.txt';
$handle=fopen($file, "w");
while ($row=mysql_fetch_array($res)){
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
//Now the file is ready with data from database
//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;
Your current code does not output a file, it just sends headers.
in order for your script to work add the following code after your fclose statement.
$data = file_get_contents($file);
echo $data;

PHP header , csv downloadable error in generating output (unknown new lines character is appending with output), zend

Using following code user downloads information (in csv format) from database. But while getting output lots of rows are blank , i.e. in my csv there will be two rows. But while getting csv after first row 2 blank rows will be printed.
public generatecsvAction()
{
$answermodel=new Model_DbTable_Answers();
header("Content-type: application/octet-stream");
//header('Content-Type: text/csv; charset=utf-8'); // tried this too but still same output
header("Content-Disposition: attachment; filename=exportevent.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "First_Name,Last_Name,Roll_No1,User_name,col1,col2,col3..... \n"; // checked without this \n
foreach($testarray as $i)
{
$ans = $answermodel->getanswers1($a,$i);
if(count($ans)==10) // if user answered all the qustions ,
{
//echo "1,";
foreach($ans as $value)
{
$res=$answermsamodel->findansweroption($value);
$res=$res.",";
$s=$s.$res;
}
echo $s;
break;
}
}
}
Most likely, $answermsamodel->findansweroption($value) returns a value with line breaks in it.

Categories