Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Jacob Mahto
Added: Apr 15, 2018 4:32 PM
Modified: Apr 15, 2018 4:35 PM
Views: 11
Tags: no tags
  1. //include the file that loads the PhpSpreadsheet classes
  2. require 'spreadsheet/vendor/autoload.php';
  3.  
  4. //include the classes needed to create and write .xlsx file
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  7.  
  8. //object of the Spreadsheet class to create the excel data
  9. $spreadsheet = new Spreadsheet();
  10.  
  11. //add some data in excel cells
  12. $spreadsheet->setActiveSheetIndex(0)
  13.  ->setCellValue('A1', 'Domain')
  14.  ->setCellValue('B1', 'Category')
  15.  ->setCellValue('C1', 'Nr. Pages');
  16.  
  17.  
  18. $spreadsheet->setActiveSheetIndex(0)
  19.  ->setCellValue('A2', 'CoursesWeb.net')
  20.  ->setCellValue('B2', 'Web Development')
  21.  ->setCellValue('C2', '4000');
  22.  
  23. $spreadsheet->setActiveSheetIndex(0)
  24.  ->setCellValue('A3', 'MarPlo.net')
  25.  ->setCellValue('B3', 'Courses & Games')
  26.  ->setCellValue('C3', '15000');
  27.  
  28. //set style for A1,B1,C1 cells
  29. $cell_st =[
  30.  'font' =>['bold' => true],
  31.  'alignment' =>['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
  32.  'borders'=>['bottom' =>['style'=> \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM]]
  33. ];
  34. $spreadsheet->getActiveSheet()->getStyle('A1:C1')->applyFromArray($cell_st);
  35.  
  36. //set columns width
  37. $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(16);
  38. $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(18);
  39.  
  40. $spreadsheet->getActiveSheet()->setTitle('Simple'); //set a title for Worksheet
  41.  
  42. //make object of the Xlsx class to save the excel file
  43. $writer = new Xlsx($spreadsheet);
  44. $fxls ='excel-file_1.xlsx';
  45. $writer->save($fxls);