Append two sql tables and export them as Excel document php - php

I have 2 tables in the database deliverydata and deliverydata1. I want to append them and export the result as a excel sheet. Here is my sql to excel export script for deliverydata table. What code should I add to append them and export the output as excel sheet. Both the tables have the same number of columns with the same name.
Here is my code :
<?php
include 'db.php';
$SQL = "SELECT WayFrom,WayTo,ConsignorName,ConsigneeName,PODStatus from deliverydata";
$header = '';
$result ='';
$exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $exportData );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $exportData , $i ) . "\t";
}
while( $row = mysql_fetch_row( $exportData ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$result .= trim( $line ) . "\n";
}
$result = str_replace( "\r" , "" , $result );
if ( $result == "" )
{
$result = "\nNo Record(s) Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$result";
?>

Thanks #iainn for your suggestion. It worked :)
#rafael Yes I'm using phpexcel library
#parfait the current code was working properly but I wanted to append two tables

Related

Change Export File Type

I have this script that allows me to save a mysql database table as a excel spreadsheet from a php webpage. But it needs to be saved as a PDF. I have looked around and cant find any fix to my code.
<?php
include "db.php";
$SQL = "Select Name, Email, Department, Cellphone, Extension from contacts";
$header = '';
$result ='';
$exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $exportData );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $exportData , $i ) . "\t";
}
while( $row = mysql_fetch_row( $exportData ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$result .= trim( $line ) . "\n";
}
$result = str_replace( "\r" , "" , $result );
if ( $result == "" )
{
$result = "\nNo Record(s) Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Telephone List.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$result";
?>

Export to csv after printing table in mysql

Been trying to figure this out since a hell of a lot of hours, but without result. A user can select some filters in a form, which result in a unique table, printed to the screen.
Then I want a button to export this specific table to a csv file, stored on the users computer.
if(isset($_POST["askReport"])){
//all the variables
//...
$myQuery = ""SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD"";
$result = $mysqli->query($myQuery);
if($result->num_rows >0){
print '<table><tr><th>.....'; //and all the headers
print '<td>'.$row["user"].'</td>';
..... //and all the other columns and end of row
}
So far, so good, I get a nice table with the results that I want. But then comes the tricky part - how do I get the button to download this table? Here's what I tried: (this code is stil in the isset($_POST["askReport"]) if statement)
// DOWNLOAD TABEL
print '<form method="POST"><button name=download>download</button></form>';
if(isset($_POST["download"])){
$select = "SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=download .xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
};
}
This doesn't give any errors. It just reloads the page. So I can't figure out why it isn't working. I thank you for your help.
PARTIAL SOLUTION
The following code (your code, just with the MySQL commented out for testing purposes) works fine. I'm running Apache/2.4.9 (Unix) PHP/5.5.14
The thing that made the downloading work was when I put the <?php and ?> tags in. Try this code and let me know if it works.
<?php
// DOWNLOAD TABEL
print '<form method="POST"><button name=download>download</button></form>';
if(isset($_POST["download"])){
$select = "SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD";
/* $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
*/
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
$header="abc,123";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=download .xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "$header\n$data";
};
?>

Styling export XLS using PHP

How do i make the columns wider and the headers different other than the column name of the table in mysql? and how do i make it have a larger font? I am new to exporting excel files i have no idea how to do it, i've tried researching about it but i didn't understand it either. thank you in advance for your help.
Here is my export.php
<?php
require 'config.php';
$SQL = "SELECT prod_brand, prod_name, prod_category, prod_price, prod_desc, prod_quantity from inventory";
$header = '';
$result ='';
$exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $exportData );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $exportData , $i ) . "\t";
}
while( $row = mysql_fetch_row( $exportData ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$result .= trim( $line ) . "\n";
}
$result = str_replace( "\r" , "" , $result );
if ( $result == "" )
{
$result = "\nNo Record(s) Found!\n";
}
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename=InventoryExport'.date('m-d-Y H:i:s').'.xls');
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$result";
?>
See the PHP code sample from this link, where the headers names are custom and the rows are extracted from a database:
http://www.easyxls.com/manual/basics/export-list-to-excel.html
To increase the font size of the header use this code:
// Create an instance of the object used to format the cells
$xlsAutoFormat = new COM("EasyXLS.ExcelAutoFormat");
//Set the style of the header
$xlsHeaderStyle = new COM("EasyXLS.ExcelStyle");
$xlsHeaderStyle->setFontSize(18);
$xlsAutoFormat->setHeaderRowStyle($xlsHeaderStyle);

php mysql export to excel multiple sheets [duplicate]

This question already has answers here:
How to generate an Excel document with multiple worksheets from PHP?
(4 answers)
Closed 9 years ago.
I have several queries need to be exported to one file with one sheet (with query name) per query. Here is code for one;
$select = "SELECT * from data";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\nNo Data!\n";
}
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Test.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
exit();
How can i use this code with multiple queries to multiple sheets?
Suggest that you look at some of the PHP libraries that can actually write real Excel BIFF or OfficeOpenXML files rather than simply using a csv renamed as .xls
In addition to my own PHPExcel, you can find a list of alternatives here

Error exporting MySQL query to CSV

I am trying to export data to a cdv/xls file using PHP. I keep getting the following error:
*"Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in"*
Here is my code:
<?php
//Connect the the database
require('../mysql_connect.php') ;
$select = "SELECT * FROM customers WHERE email = 'info#example.com'";
$export = mysql_query ( $select, $dbc ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
Any help is greatly appreciated!
I was using mysqli and not mysql in my mysql_connect.php include. Thanks for the help!

Categories