Tje Posted August 14, 2014 Share Posted August 14, 2014 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"; } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 14, 2014 Share Posted August 14, 2014 (edited) 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 August 14, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Tje Posted August 15, 2014 Author Share Posted August 15, 2014 #moderator It's work fine .but i want to open second table values in new worksheet.like below image Quote Link to comment Share on other sites More sharing options...
Tje Posted August 15, 2014 Author Share Posted August 15, 2014 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'); ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 15, 2014 Share Posted August 15, 2014 (edited) #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 August 15, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.