PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 4, 2022

[FIXED] How to insert data in excel Spreadsheet using php Excel

 October 04, 2022     excel, insert, php, phpexcel     No comments   

Issue

how to insert data in excel spreadsheet using phpExcel?

please help me.


Solution

This is assuming all the libraries loaded and imported into the page

function report($result){
//result is the data to be filled


    $ea = new \PHPExcel();
    $ea->getProperties()
        ->setCreator('YOURNAME')
        ->setTitle('PHPExcel');

    $ews = $ea->getSheet(0);

    $ews->setCellValue('a1', 'ID'); // Sets cell 'a1' to value 'ID
    $ews->setCellValue('b1', 'first Name');
    $ews->setCellValue('c1', 'Last Name');



//this is to set header colour
    $header = 'a1:c1';
    $ews->getStyle($header)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('0000ffe6');
    $style = array(
        'font' => array('bold' => true,),
        'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,),
    );
    $ews->getStyle($header)->applyFromArray($style);

    $ews->fromArray($result, '-', 'A2');


//this is to autosize columns to fit data
    for ($col = ord('a'); $col <= ord('c'); $col++)
    {
        $ews->getColumnDimension(chr($col))->setAutoSize(true);
    }


// Redirect output to a clients web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="report.xlsx"');
    header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');
//
//// If you're serving to IE over SSL, then the following may be needed
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0

    $objWriter = PHPExcel_IOFactory::createWriter($ea, 'Excel2007');
    $objWriter->save('php://output');
    exit;

}


Answered By - glenn ferns
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing