1

I want to write an array into an Excel sheet: Can someone help me? How can I write this array in excel sheet?

My array:

Array
(
[1] => Array
    (
        [1] => age
        [2] => name
        [3] => gender
    )

[2] => Array
    (
        [1] => 12
        [2] => alexander
        [3] => male

    )

[3] => Array
    (
        [1] => 18
        [2] => shine
        [3] => female
    )

)

I want this output:

enter image description here

I want to write this array in excel sheet and, after writing the array, to download file as an Excel spreadsheet (xlsx).

caitlin
  • 2,769
  • 4
  • 29
  • 65
shan
  • 79
  • 1
  • 7
  • 1
    possible duplicate of http://stackoverflow.com/questions/3968973/how-can-i-write-data-into-an-excel-using-php – rahul Feb 18 '16 at 10:00

3 Answers3

1

Hi you can store the array as csv file or use the one of Excel php library to create the excel file you can check this library : PHPExcel

0

I've tested this code on my dev server and it should work perfectly:

  1. Get the PHPExcel library and put it on your server.
  2. Use this code:

    // Include the PHPExcel libraries
    require_once "PHPExcel/PHPExcel.php"; // Or whatver the path to your PHPExcel library is
    require_once "PHPExcel/PHPExcel/IOFactory.php";
    
    // Set up your input data array
    $data = array(
        array("age", "name", "gender"),
        array("12", "alexander", "male"),
        array("18", "shine", "female"),
    );
    
    // Create the PHPExcel object
    $xl = new PHPExcel();
    
    // Write the data into the new spreadsheet starting at cell A1
    $xl->getActiveSheet()->fromArray($data, NULL, 'A1');
    
    // Create a spreadsheet writer, in this case for Excel 2007 (xlsx)
    $objWriter = PHPExcel_IOFactory::createWriter($xl, 'Excel2007');
    
    // Set the appropriate headers for the download:
    header("Content-type: application/octet-stream");
    
    // Name the file
    header("Content-Disposition: attachment; filename=Export.xlsx");
    $objWriter->save('php://output');
    // We're finished!
    die; 
    

Alternately, if you want to do something different with the file you've just generated, you can replace everything after $objWriter with something like this:

ob_start();
$objWriter->save('php://output');
$fileContents = ob_get_clean();
caitlin
  • 2,769
  • 4
  • 29
  • 65
0

I understanding that you want to generate .XLS file. right? The Following code will do.

require_once "PHPExcel/PHPExcel.php"; //the path to your PHPExcel library 
require_once "PHPExcel/PHPExcel/IOFactory.php";

    // Set up your input data array
    $data = array(
        array("age", "name", "gender"),
        array("12", "alexander", "male"),
        array("18", "shine", "female"),
    );
$name ="sheet name";
$objPHPExcel = new PHPExcel();
$objWriter  = array();
$objWorkSheet = $objPHPExcel->createSheet();            
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle($name);
$objPHPExcel->getActiveSheet()->fromArray($data);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=\""filename.xls");
header('Cache-Control: max-age=0');
ob_get_clean();
$objWriter->save('php://output');
ob_end_flush();
exit;