/    Sign up×
Community /Pin to ProfileBookmark

How to download two mysql tables in same excel sheet using PHP

I have a PHP code to download one mysql table to excel sheet.what i want to do is download 2 tables in to same excel sheet.those tables are should be in different excel worksheet.please help me. this code work fine.it’s download one table.

[code=php]
<?php
ob_start();
session_start();
include(‘dbconnection.php’);
$usr= $_SESSION[‘fname’];

header(‘Content-Type: text/csv’);
header(‘Content-Disposition: attachment;filename=exported-data.csv’);

$select_table=mysql_query(“select * from regfarmer WHERE ffname=’$usr'”);

$rows = mysql_fetch_assoc($select_table);

if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

function getcsv($no_of_field_names)
{
$separate = ”;

foreach ($no_of_field_names as $field_name)
{
if (preg_match(‘/\r|\n|,|”/’, $field_name))
{
$field_name = ” . str_replace(”, $field_name) . ”;
}
echo $separate . $field_name;

$separate = ‘,’;
}

echo “rn”;
}
?>

[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Tje1authorAug 15.2014 — i've found a code for split to excel sheet.now how i integrate mysql table selection for this code?

[code=php]
<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');

// Create a new worksheet, after the default sheet
$objPHPExcel->createSheet();

// Add some data to the second sheet, resembling some different data types
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'More data');

// Rename 2nd sheet
$objPHPExcel->getActiveSheet()->setTitle('Second sheet');

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>

[/code]
×

Success!

Help @Tje1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...