PHP excel class
The main idea of the class is to provide an easy way to read and create excel files from arrays of data.
NOTE: This class requires PHP Excel Library
NOTE: The library requires:
ZipArchive. Instalation: "sudo apt-get install php7.0-zip". Additional info:
https://stackoverflow.com/questions/25176999/phpexcel-ziparchive-not-found
PHP xml. Instalation: "sudo apt-get install php7.0-xml".
NOTE: The modules described above require reload or restart of the Apache (or other) server. Reload "/etc/init.d/apache2 reload".
Example of excel file creation:
$excel_data = array(
array('Column A row 1', 'Column B row 1',),
array('Column A row 2', 'Column B row 2',),
);
samis_excel::save_excel_file($excel_data, '/tmp/test.xlsx');
Example of excel file reading:
$excel_data = samis_excel::get_standart_excel_file_data('/tmp/test.xlsx');
Example of excel file download:
samis_excel::download_excel_file($excel_data, 'test.xlsx');
And the excel class itself:
<?php
/**
* The main idea of the class is to provide an easy way to read and create excel files from arrays of data.
* NOTE: This class requires PHP Excel Library
* NOTE: The library requires:
* ZipArchive. Instalation: "sudo apt-get install php7.0-zip". Additional info: https://stackoverflow.com/questions/25176999/phpexcel-ziparchive-not-found
* PHP xml. Instalation: "sudo apt-get install php7.0-xml".
* NOTE: The modules described above require reload or restart of the Apache (or other) server. Reload "/etc/init.d/apache2 reload".
* @package Sami's excel class
* @version $Id: samis_excel.class.php v.1.0 2017-07-03 17:48:00 $
* @author Samuil Banti
* @copyright (C) 2017 - Samuil Banti
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
class samis_excel {
/**
* Gets the data from all sheets for excel file with standart tables structure.
* @param string $excel_file - the path to the excel file.
* @return object $file_data - the data extracted from all shields.
*/
public static function get_standart_excel_file_data($excel_file)
{
$objPHPExcel = PHPExcel_IOFactory::load($excel_file);
$countSheets = $objPHPExcel->getSheetCount();
$file_data = new stdClass;
for($sheet_id = 0; $sheet_id <= ($countSheets-1); ++$sheet_id) {
$objWorksheet = $objPHPExcel->setActiveSheetIndex($sheet_id);
$sheet_rows = self::get_sheet_rows($objWorksheet);
$sheet_name = $objWorksheet->getTitle();
$file_data->{$sheet_name} = $sheet_rows;
}
return $file_data;
}
/**
* Gets the data from specific sheet.
* @param object $objWorksheet - the phpExcel library object of the sheet.
* @return array $rows - the rows of the sheet.
*/
private static function get_sheet_rows($objWorksheet)
{
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$rows = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
if(!array_key_exists($row, $rows)) {
$rows[$row] = array();
}
$rows[$row][$col] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
return $rows;
}
/**
* Create excell file from array or object of arrays or objects.
* WARNING: Thisfunction will not work properly for more than 26 columns!!!
* @param array $data - The content of the excel file.
* @param string $title - The excel title.
* @param string $subject - The excel subject.
* @param string $description - The excel description.
* @param string $keywords - The excel keywords.
* @param string $creator - The excel creator.
*/
protected static function create_excell_file($data, $title = 'N/A', $subject = 'N/A', $description = 'N/A', $keywords = 'N/A', $creator = 'N/A')
{
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator($creator)
->setLastModifiedBy($creator)
->setTitle($title)
->setSubject($subject)
->setDescription($description)
->setKeywords($keywords);
$alphabet = str_split('ABCDEFGHIJKLMNPQRSTUVWXYZ');
$i = 1;
foreach($data as $row) {
$row = (array)$row;
$row = array_values($row);
foreach($row as $kk => $cell_value) {
$cell_name = $alphabet[$kk].$i;
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cell_name, $cell_value);
}
++$i;
}
return $objPHPExcel;
}
/**
* Saves an excel file in a given location.
* @param array $data - The content of the excel file.
* @param string $file_name - The path for the file to be saved.
* @param string $title - The excel title.
* @param string $subject - The excel subject.
* @param string $description - The excel description.
* @param string $keywords - The excel keywords.
* @param string $creator - The excel creator.
*/
public static function save_excel_file($data, $file_name, $title = 'N/A', $subject = 'N/A', $description = 'N/A', $keywords = 'N/A', $creator = 'N/A')
{
$objPHPExcel = self::create_excell_file($data, $title = 'N/A', $subject = 'N/A', $description = 'N/A', $keywords = 'N/A', $creator = 'N/A');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($file_name);
}
/**
* Downloads an excel file.
* @param array $data - The content of the excel file.
* @param string $file_name - The path for the file to be saved.
* @param string $title - The excel title.
* @param string $subject - The excel subject.
* @param string $description - The excel description.
* @param string $keywords - The excel keywords.
* @param string $creator - The excel creator.
*/
public static function download_excel_file($data, $file_name, $title = 'N/A', $subject = 'N/A', $description = 'N/A', $keywords = 'N/A', $creator = 'N/A')
{
$objPHPExcel = self::create_excell_file($data, $title = 'N/A', $subject = 'N/A', $description = 'N/A', $keywords = 'N/A', $creator = 'N/A');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //Excel5 //2003
//header('Content-Type: application/vnd.ms-excel'); //2003
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$file_name.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
}
}
?>
Download...