PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label phpoffice. Show all posts
Showing posts with label phpoffice. Show all posts

Tuesday, October 4, 2022

[FIXED] What are the main differences between PHPExcel and PhpSpreadsheet?

 October 04, 2022     php, phpexcel, phpoffice, phpspreadsheet     No comments   

Issue

In project of PHPOffice there are two projects associated with spreadsheet file formats:

PHPExcel

PHPExcel is a library written in pure PHP and providing a set of classes that allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML, ... This project is built around Microsoft's OpenXML standard and PHP.

and

PhpSpreadsheets

PhpSpreadsheet is a library written in pure PHP and providing a set of classes that allow you to read from and to write to different spreadsheet file formats, like Excel and LibreOffice Calc.

What are the main differences between them?


Solution

PHPExcel has been maintained as a library for working with spreadsheet files for many years now, and has been shackled by retaining support for older versions of PHP (>= 5.2) making it very difficult to move forward and improve it. It is a stable library, but will not be developed any further.

PHPSpreadsheet is the newest version of PHPExcel, and large parts of it have been rewritten to take advantage of the newer features of PHP. While retaining all the functionality of PHPExcel, it requires a minimum PHP version of 5.5 (and soon that will be dropped to require a minimum of 5.6).

The change in library name was to reflect the fact that it isn't limited to Excel spreadsheets; but supports a wider range of spreadsheet file formats.

EDIT 2020:

PHP Excel was officially deprecated in 2017 and permanently archived in 2019.

PHP Excel has not be maintained for years and must not be used anymore. All users must migrate to its direct successor PhpSpreadsheet, or another alternative.



Answered By - Mark Baker
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to get specific columns in Laravel [Maatwebsite/Laravel-Excel]

 October 04, 2022     excel, laravel, php, phpexcel, phpoffice     No comments   

Issue

the file i am importing has thousands of records which is causing my network to get slow. i want to read only those columns that i need, before inserting it into database. When file is processed , it should first search those columns not the whole file, and fetch the rows of those columns

Excel::load($path, function($reader) {

//Getting headers using this


       $headers = $reader->first()->keys()->toArray();

//This is the array of required columns

       $headings = array('registrant_name','registrant_address','registrant_phone','registrant_zip','registrant_email','registrant_country','registrant_state','registrant_city');


});

Data insertion after file read.

 if($data->count() > 0)
             {
              foreach($data->toArray() as $value)
              {
                $insert[] = array(
                 'registrant_name'  => $value['registrant_name'],
                 'registrant_address'   => $value['registrant_address'],
                 'registrant_phone'   => $value['registrant_phone'],
                 'registrant_zip'   => $value['registrant_zip'],
                 'registrant_email'   => $value['registrant_email'],
                 'registrant_country'   => $value['registrant_country'],
                 'registrant_state'   => $value['registrant_state'],
                 'registrant_city'   => $value['registrant_city']

                );                
               }
              }


      if(!empty($insert))
      {
       DB::table('customers')->insert($insert);
      }    

Solution

In collections, you can check the method only documentation

i.e.

$headers = $reader->first()->only(['registrant_name','registrant_address','registrant_phone','registrant_zip','registrant_email','registrant_country','registrant_state','registrant_city']);

You might also use chunk, to insert the data in smaller collections than in one big.

Could you update the post with the App\Import class in order to help you more.



Answered By - Nikolas
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How get grouping rows in PHP from EXCEL?

 October 04, 2022     excel, php, phpexcel, phpoffice, phpspreadsheet     No comments   

Issue

How get grouping rows in PHP from EXCEL?

enter image description here

This code does not issue a groups:

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("bulat_price.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->toArray();

Solution

$spreadsheet = PHPExcel_IOFactory::load($file); // load file
$objWorksheet = $spreadsheet->getActiveSheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0); // select firts sheet

$i = 0;
$arrLevel = [];

foreach ($worksheet->getRowDimensions() as $rowDimension) {
    $i++;
    $arrLevel[$i]['level'] = $rowDimension->getOutlineLevel(); // get level
}
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'

foreach ($objWorksheet->getRowIterator() as $row) {    
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(true);

    foreach ($cellIterator as $cell) {
        $arrLevel[$row->getRowIndex()]["excel"][$cell->getColumn()] = $cell->getValue(); // merge level and value
    }
}

var_dump($arrLevel);

We get:

enter image description here



Answered By - Stanislav
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

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
All Comments
Atom
All Comments

Copyright © PHPFixing