I found an PHP function from the internet that can write data into true excel not just csv, and i adopted it into my project to put data from MySQL. Thanks to that site (i forgot the link).
1. download this file excel_function.php.
2. in your php file add this code
<?php
function cleanData($str) {
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
return $str;
}
include('excel_function.php');
$query = "SELECT * FROM tablename";
if ($query){
include ('connect.php'); //connect to database
$queryGenerate = $mysqli->query($query);
if ($queryGenerate){
$num = $queryGenerate->num_rows;
if ($num>0){
$current_date = date('Y-m-d');
$filename ="{$current_date}.xls";
// Send Header
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename={$filename}"); header("Content-Transfer-Encoding: binary ");
xlsBOF();
xlsWriteLabel(0,0,"Gatepass No.");
xlsWriteLabel(0,1,"QTY");
xlsWriteLabel(0,2,"UOM");
xlsWriteLabel(0,3,"Item");
xlsWriteLabel(0,4,"Item");
xlsWriteLabel(0,5,"Date");
$xlsRow = 1;
while($rows=$queryGenerate->fetch_object()){
xlsWriteLabel($xlsRow,0,$rows->gt);
xlsWriteNumber($xlsRow,1,$rows->qty);
xlsWriteLabel($xlsRow,3,$rows->uom);
xlsWriteLabel($xlsRow,4,$rows->description);
xlsWriteLabel($xlsRow,5,$rows->d);
$xlsRow++;
}
xlsEOF();
}
}
$mysqli->close();
}
?>
1. download this file excel_function.php.
2. in your php file add this code
<?php
function cleanData($str) {
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
return $str;
}
include('excel_function.php');
$query = "SELECT * FROM tablename";
if ($query){
include ('connect.php'); //connect to database
$queryGenerate = $mysqli->query($query);
if ($queryGenerate){
$num = $queryGenerate->num_rows;
if ($num>0){
$current_date = date('Y-m-d');
$filename ="{$current_date}.xls";
// Send Header
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename={$filename}"); header("Content-Transfer-Encoding: binary ");
xlsBOF();
xlsWriteLabel(0,0,"Gatepass No.");
xlsWriteLabel(0,1,"QTY");
xlsWriteLabel(0,2,"UOM");
xlsWriteLabel(0,3,"Item");
xlsWriteLabel(0,4,"Item");
xlsWriteLabel(0,5,"Date");
$xlsRow = 1;
while($rows=$queryGenerate->fetch_object()){
xlsWriteLabel($xlsRow,0,$rows->gt);
xlsWriteNumber($xlsRow,1,$rows->qty);
xlsWriteLabel($xlsRow,3,$rows->uom);
xlsWriteLabel($xlsRow,4,$rows->description);
xlsWriteLabel($xlsRow,5,$rows->d);
$xlsRow++;
}
xlsEOF();
}
}
$mysqli->close();
}
?>
Comments
Post a Comment