How to include 3rd party library in Magento 2 (like php xlsx library)
below is the steps to create custom lib
in this i will take PHpexcel lib
download the lib https://github.com/PHPOffice/PHPExcel/tree/1.8/Classes
keep the folder in lib/internal
open PHPExcel.php and change the class name to Phpexcel_PHPExcel
in side PHPExcel folder lot of function initialization PHPExcel change it to Phpexcel_PHPExcel else it ill through error ex lib\internal\Phpexcel\PHPExcel\Calculation.php __construct and getInstance function all PHPExcel change to Phpexcel_PHPExcel like this change in all place
where ever your using use like
use Phpexcel_PHPExcel; use Phpexcel_PHPExcel_IOFactory;
and inside __construct
Phpexcel_PHPExcel $xlsx, $this->excelFactory = $excelFactory;
as like example you can build xlsx file
public function getXlsxFile(){ $component = $this->filter->getComponent(); $name = md5(microtime()); $file = 'export/'. $component->getName() . $name . '.xml'; $this->filter->prepareComponent($component); $this->filter->applySelectionOnTargetProvider(); /** @var SearchResultInterface $searchResult */ $component->getContext()->getDataProvider()->setLimit(0, 999999); $searchResult = $component->getContext()->getDataProvider()->getSearchResult(); $this->prepareItems($component->getName(), $searchResult->getItems()); /** @var SearchResultIterator $searchResultIterator */ $searchResultIterator = $this->iteratorFactory->create(['items' => $searchResult->getItems()]); $filterDataArr = ''; foreach($searchResultIterator as $dataRow){ $filterDataArr[]= $dataRow->getData(); } $excelDataArray = ''; $sheetTitle = 'export'; $HeadersArray = $this->metadataProvider->getHeaders($component); $FieldsArray = $this->metadataProvider->getFields($component); if(($key = array_search('actions', $FieldsArray)) !== false) { unset($FieldsArray[$key]); } if($component->getData('worksheetlabel')){ $sheetTitle = $component->getData('worksheetlabel'); } $hPos = 0; foreach($HeadersArray as $key =>$value){ $excelDataArray[$hPos][$key] = $value; } $dPos = 1; if(count($filterDataArr) > 1){ foreach($filterDataArr as $Filterdata){ foreach($FieldsArray as $key=>$value){ $excelDataArray[$dPos][$key] = $Filterdata[$value]; } $dPos++; } } $this->_Xlsx->getActiveSheet()->fromArray($excelDataArray, null, 'A1' ); $this->_Xlsx->getActiveSheet()->setTitle($sheetTitle); $this->_Xlsx->setActiveSheetIndex(0); $callStartTime = microtime(true); $name = md5(microtime()); $file = '/export/'. $component->getName() . $name . '.xlsx'; $this->directory->create('export'); $objWriter = Phpexcel_PHPExcel_IOFactory::createWriter($this->_Xlsx, 'Excel2007'); $filepath = $this->directory->getAbsolutePath($file); $objWriter->save($filepath); return [ 'type' => 'filename', 'value' => $file, 'rm' => true // can delete file after use ]; }
what library exactly do you want to install?
The first result on google searching for php xlsx library gave me this: https://github.com/PHPOffice/PHPExcel
Since its registered on packagist https://packagist.org/packages/phpoffice/phpexcel
you can easily install it into your magento2 instance with the following shell command (from your magento2 root dir):
composer require phpoffice/phpexcel
If you are using it in a Magento Extension you should require the library in your Modules composer.json File, so it always is automatically installed when your Extension is installed.
The Autoloading will be handled by composer then