How do I find out how many rows I have loaded using PHPSpreadsheet\Reader\Xlsx::load() method?
I cannot find methods (or properties) for getting row count in Spreadsheet or Worksheet classes either.
BTW I am using following code:
$filename = 'test.xlsx';
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($filename);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$reader->setLoadSheetsOnly($sheet);
$this->spreadsheet = $reader->load($filename);
$this->worksheet = $this->spreadsheet->getActiveSheet();
Using the worksheet's getHighestRow() method
$highestRow = $this->spreadsheet->getActiveSheet()->getHighestRow();
or getHighestDataRow() if you're only interested in rows where cells contain data and not any blank rows at the end of the worksheet
Related
Following is the code in which I am using the PhpSpreadsheet library to convert xlsx file to csv. I am trying to achieve the ability that the converted file should have one extra column called ClientID which should contain the same value for all the number of rows present in that file. Please let me know is it possible to do? Any help would be highly appreciated.
use \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use \PhpOffice\PhpSpreadsheet\Writer\Csv;
$reader = new Xlsx();
$uploaded_xls_file = $_FILES['file']['tmp_name'];
$spreadsheet = $reader->load($uploaded_xls_file);
$loadedSheetNames = $spreadsheet->getSheetNames();
$writer = new Csv($spreadsheet);
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$writer->setSheetIndex($sheetIndex);
$writer->setPreCalculateFormulas(false);
$savingpath = $loadedSheetName.'.csv';
$writer->save($savingpath);
//$writer->save($loadedSheetName.'.csv');
}
Hi I'm new to this library called PHPSpreadsheet.
I tried reading it's docs but I can't understand it.
I want to insert a new row on an existing Excel File and
here is what I have so far:
<?php
require '../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$inputFileName = 'Excel/hello.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Updated');
$writer = new Xlsx($spreadsheet);
$writer->save('../controller/excel/hello.xlsx');
?>
This inserts new data on the 'hello.xsls' file replacing the cell's previous data. How can I make it write data into a new row?
To create a new row, you need to call insertNewRowBefore() with the row number you want to insert before...
$sheet = $spreadsheet->getActiveSheet();
$sheet->insertNewRowBefore(1);
$sheet->setCellValue('A1', 'Updated');
You can also call it with a number of rows to insert, the default is 1.
If you want to append a row, you can call getHighestRow() to find the last row and add 1 to it for the new row. Also change the hard coding of the column in the setCellValue() call to use this row as well...
$sheet = $spreadsheet->getActiveSheet();
$row = $sheet->getHighestRow()+1;
$sheet->insertNewRowBefore($row);
$sheet->setCellValue('A'.$row, 'Updated');
Hello I think this issue came because of memory exhausted so increase your memory limit by php.ini file
like memory_limit = 256M
After increase memory limit , restart your apache
The accepted answer throws exception:
"Column references should not be numeric."
For me works:
...
$row = 'A' . ($sheet->getHighestRow() +1);
$sheet->insertNewColumnBefore($row);
$sheet->setCellValue($row, 'Hello World again!');
...
I have a xlsx file where the first row has:
image :
I need to dynamically check how much locales are there (by column name), as it can happen that it can be only one or 5/6 columns.
How to set it?
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filePath);
$activeSheet = $phpExcelObject->getActiveSheet()->rangeToArray('B1:G1');
dump($activeSheet);die;
at first, you can use PhpSpreadsheet for loading Excel files.
there is example code for counting first row keys :
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\xlsx();
$spreadsheet = $reader->load($filePath);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
$firstRow = $sheetData[0];
$cnt = 0;
foreach ($firstRow as $value) {
if($value)
$cnt++;
}
//because of first column
$cnt--;
the last line, mines one is because of your first column "key"
I need to write in a .xlsx file about 111.100 rows, using fromArray() but I have a strange error
I use phpspreadsheet library
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
// Allow to access AA column if needed and more
$sheet->setCellValue($columnLetter.'1', $columnName);
$columnLetter++;
}
$i = 2; // Beginning row for active sheet
$columnLetter = 'A';
foreach ($columnValues as $columnValue) {
$sheet->fromArray(array_values($columnValue), NULL, $columnLetter.$i);
$i++;
$columnLetter++;
}
// Create your Office 2007 Excel (XLSX Format)
$writer = new Xlsx($spreadsheet);
// In this case, we want to write the file in the public directory
// e.g /var/www/project/public/my_first_excel_symfony4.xlsx
$excelFilepath = $directory . '/'.$filename.'.xlsx';
// Create the file
$writer->save($excelFilepath);
And I get the exception :
message: "Invalid cell coordinate AAAA18272"
#code: 0
#file: "./vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php"
Can you help me please ?
Excel pages are limited. The limit is huge but still limited. This is a correct filter so you can't write if there is no space for it.
Anyway you shouldnt use excel pages for such a big amount of data, you can try fragmenting it into smaller pieces, but databases should be the way to manipulate such amount of information
I am using phpexcel library to read an excel file.It works perfeclty on 99%. But sometimes it reads empty columns also.My code is
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file');
}
$worksheet=$objPHPExcel->getActiveSheet();) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
sometimes the $highestcolumn returns 'WVL' even if the data in excel column up to 'C' why?.
Also i want to check all the rows under a particular column is empty or not,Is there any easy method to do it instead of iterating all rows using for loop.
The getHighestRow() and getHighestColumn() methods work on the basis of testing for anything related to a cell, even if that's a style setting or a named range or print settings or a column/row setting such as width/height or hidden.
That's why the getHighestDataRow() and getHighestDataColumn() methods exist. These two methods look at the actual data in cells.
Note: Just because a cell looks empty when you view it in MS Excel, doesn't mean that it actually is empty. NULL is a valid cell value, as is a space character, neither of which is visible.
In answer to your second question: you can pass an optional argument to the getHighestRow(), getHighestColumn() and to the getHighestDataRow() and getHighestDataColumn(), so a row number passed to getHighestColumn() or getHighestDataColumn() will return the highest column in the specified row; and a column letter passed to getHighestRow() or getHighestDataRow() will return the highest row in that column.
e.g.
$highestColumnInRow5 = $worksheet->getHighestColumn(5);
or
$highestDataRowInColumnAA = $worksheet->getHighestDataRow('AA');