Welcome Guest, Not a member yet? Register   Sign In
PHP export to Excel
#1

[eluser]Unknown[/eluser]
How to export to Excel in CodeIgniter:
I was try as
$this->load->plugin('to_excel');

in plugin file
if ($query->num_rows() == 0) {
echo '<p>The table appears to have no data.</p>';
} else {
/*(foreach ($fields as $field) {
$headers .= $field->name . "\t";
}*/

for($i=0;$i<count($fields);$i++)
{
$headers .= $fields[$i]. "\t";
}
$headers .= "\n";
foreach ($query->result_array() as $row) {

$line = '';
foreach($row as $value) {

if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfilename.xls");
echo "$headers\n$data";


It run properly but click to save button
give error as=>

The file you are trying to open , 'yourfilename-14.xls' is in differrnt format than specifued by file extension. Verify taht the file not corrupted and is form a trusted source before open the file.Do you want open file now?

but open file is ok?
any one help me for this error. or suggest how to export excel sheet
#2

[eluser]Seb[/eluser]
Well the thing is you're outputting tabbed separator value, not a real excel spreadsheet.

Therefore Excel warns you that you're not opening the correct file format. You should save your file as ".txt".
#3

[eluser]InsiteFX[/eluser]
Code:
$filename = "my_excel_file.xls";

//prepare to give the user a Save/Open dialog...
header ("Content-type: application/octet-stream");
header ("Content-Disposition: attachment; filename=".$filename);

//setting the cache expiration to 30 seconds ahead of current time. an IE 8 issue when opening the data directly in the browser without first saving it

to a file
$expiredate = time() + 30;
$expireheader = "Expires: ".gmdate("D, d M Y G:i:s",$expiredate)." GMT";
header ($expireheader);

//output the contents
echo $contents;
exit;

InsiteFX
#4

[eluser]vitoco[/eluser]
the format file that you wanna output isn't XLS, it's CSV ( comma or tab separated )

Code:
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
    header ("Cache-Control: no-cache, must-revalidate");
    header ("Pragma: no-cache");
    header ("Content-type:application/vnd.ms-csv");
    header ("Content-Disposition: attachment; filename=CSV_FILENAME.csv" );

Saludos
#5

[eluser]Junaid Atique[/eluser]
This is because you have wrong format. Please correct your format. user html tables to format this or use vitoco method to export this in csv...




Theme © iAndrew 2016 - Forum software by © MyBB