Jump to content

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


Tje

Recommended Posts

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

<?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 "\r\n";
}
?>


Link to comment
Share on other sites

Currently your code only has one query for extracting the data from one of your tables (regfarmer). To get the data from the second table you'd add a second query and process the results the same way as before

// get data from second table.
$select_table=mysql_query("select * from SECOUND_TABLE_NAME_HERE 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);
}
Edited by Ch0cu3r
Link to comment
Share on other sites

hey, I've found a code for split to excel sheet.now how i integrate mysql table selection for this code?

<?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');
?>
Link to comment
Share on other sites

#moderator It's work fine .but i want to open second table values in new worksheet.like below image

 

hey, I've found a code for split to excel sheet

For what you are trying to do, yes will need to use that PHPExcel class you just found to create separate worksheets for each table. In order for you to that you are first going to need to learn how to use the PHPExcel class. Documentation can be found here

 

Once you have learned how to use that class you should then be able to work out how to merge it with your existing code.

 

Dont mean to be harsh but I/we are not going to write the code for you. If we wrote it for you will not learn anything. If are not bothered about learning and just want the code ASAP then we have a freelance forum, post a job offering there for someone to write the necessary code for you.

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.