2

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

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.