I am trying to create a mysql query loop to implement it in the xlsxwriter php excel exporter but i am having hard time making it work, it generates the excel sheet but it says it is corrupted and not working.
This is the original file source code:
<?php
include_once("xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$rows = array(
array('test','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','=B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
$writer = new XLSXWriter();
$writer->setAuthor('Some Author');
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
$writer->writeToStdOut();
//$writer->writeToFile('example.xlsx');
//echo $writer->writeToString();
exit(0);
And this is the code i have created, and you will notice that in the "WriteSheetRow" line, i have made some dummy data, just to check if it is working, and it is not!:
<?php
include "session.php";
include_once("files/excel/xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$writer = new XLSXWriter();
//define column headers
$headers = array('Product Id' => 'integer', 'Price' => 'price', 'Sale Price' => 'price', 'Sales Count' => 'integer', 'Sale Date' => 'string');
//write headers
$writer->writeSheetHeader('Sheet1', $headers);
$sql = "SELECT * FROM customeracct WHERE tourid=1005 AND status=1";
$result = $db->query($sql);
if ($result->num_rows > 0) { while($customeracct = $result->fetch_assoc()) {
$contactid = $customeracct["contactid"];
$contactsql = "SELECT * FROM contacts WHERE id=$contactid";
$contactresult = $db->query($contactsql);
$contactcustomer = $contactresult->fetch_assoc();
//write rows to sheet1
$writer->writeSheetRow('Sheet1',array('test','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'));
}}
$writer->setAuthor('Good Shepherd Travels');
exit(0);
Can you please help me check if it is correct, or if there is a better way to do it? I tried to make the loop inside the array, but it doesn't work to have a php conditional code inside an array.
Thank you.