www.webdeveloper.com
Results 1 to 2 of 2

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

  1. #1
    Join Date
    Jul 2014
    Posts
    12

    Question 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.

    PHP Code:
    <?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";
    }
    ?>

  2. #2
    Join Date
    Jul 2014
    Posts
    12
    i've found a code for split to excel sheet.now how i integrate mysql table selection for this code?

    PHP 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');
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles