• Set column name and column width using PHPExcel in codeigniter

    //load our new PHPExcel library
    $this->load->library('excel');
    //activate worksheet number 1
    $this->excel->setActiveSheetIndex(0);
    //name the worksheet
    $this->excel->getActiveSheet()->setTitle('Users list');

    $this->excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
    $this->excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
    $this->excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
    $this->excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
    $this->excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);


    $this->excel->getActiveSheet()->getStyle("A1:E1")->applyFromArray(array("font" => array("bold" => true)));

    $this->excel->setActiveSheetIndex(0)->setCellValue('A1', 'ID');
    $this->excel->setActiveSheetIndex(0)->setCellValue('B1', 'FIRST NAME');
    $this->excel->setActiveSheetIndex(0)->setCellValue('C1', 'LAST NAME');
    $this->excel->setActiveSheetIndex(0)->setCellValue('D1', 'EMAIL ADDRESS');
    $this->excel->setActiveSheetIndex(0)->setCellValue('E1', 'TIME');

    // get all users in array formate
    $this->excel->getActiveSheet()->fromArray($users, null, 'A2');

    // read data to active sheet
    $this->excel->getActiveSheet()->fromArray($users);

    $filename='just_some_random_name.xlsx'; //save our workbook as this file name

    header('Content-Type: application/vnd.ms-excel'); //mime type

    header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name

    header('Cache-Control: max-age=0'); //no cache

    //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
    //if you want to save it as .XLSX Excel 2007 format

    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');

    //force user to download the Excel file without writing it to server's HD
            ob_end_clean();
    $objWriter->save('php://output');
  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari