How to dynamically add to PHPExcel spreadsheet - php

Alright, So I have this code
$key = $_SESSION['order_nums'];
$sqll = "SELECT * FROM `money` WHERE `order` = :key";
$qq=$con->prepare($sqll);
$qq->bindvalue(":key", $key);
$qq->execute();
$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('nTest.xlsx'); // Empty Sheet
$excel2->setActiveSheetIndex(0);
$worksheet = $excel2->getActiveSheet();
while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
{
$worksheet
->setCellValue('A7', $fdata['code']);
}
Where it it setting the cell value for A7 there is about 6 more of those that match. When I do it like this however, It only puts its into the CELL A7
while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
{
$worksheet
->setCellValue('A7', $fdata['code']);
}
How can I make it where
The above value will Drop down one cell for each new entry.
So the next would be A8, A9..... and so on.

That 'A7' isn't some kind of magic value, it's just a normal PHP string that's passed as a standard function argument to the setCellValue() method.... you can replace it with a string variable that you define yourself, and change for each row
$column = 'A';
$row = 7;
while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
{
$worksheet
->setCellValue($column . $row, $fdata['code']);
$row++;
}

$row = 'A';
while($fdata=$qq->fetch(PDO::FETCH_ASSOC)){
$col = 1;
foreach($fdata as $data){
$worksheet->setCellValue("$row$col", $data);
$col++;
}
$row++;
}

Related

Fetch range of columns with PhpSpreadsheet

I have an excel file that looks something like this
What I want to do is read the data from all rows but not all columns. I want to fetch data for all rows from column A to E. Currently, I am able to read the entire row (column A to CL) with this code
// Read data from excel file
$reader = IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
// Convert read data to array
$sheetData = $spreadsheet->getActiveSheet()->toArray(null,true,true,true);
// Put captured array to use
for ($row=1; $row <= count($sheetData) ; $row++) {
$xData = "'".implode("','",$sheetData[$row])."'";
}
print_r($xData);
exit;
Could someone guide me achieve this? I tried
// Specify columns to fetch data from
$cols = array('A','B','C','D','E','F','G','H','I','J','K');
// Put captured array to use
for ($row=1; $row <= count($sheetData) ; $row++) {
foreach ($cols as $col) {
$xData = "'".implode("','",$sheetData[$col.$row])."'";
}
}
print_r($xData);
exit;
but that didn't work.
Use utility helper e.g. PhpOffice\PhpSpreadsheet\Cell\Coordinate to convert column to/from index/letter.
$ws = $spreadsheet->getActiveSheet();
$firstColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString('A'); // A->1
$lastColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString('E'); // E->5
$firstRow = 1;
$lastRow = 4;
$data = [];
for($row = $firstRow; $row <= $lastRow; ++$row){
for($col = $firstColumnIndex; $col <= $lastColumnIndex; ++$col){
$data[$row][$col] = $ws->getCellByColumnAndRow($col, $row)->getValue();
}
}
print_r($data, true); // data are now collected in 2-dimensional array
There are also other methods such as stringFromColumnIndex, coordinateFromString, indexesFromString, buildRange, rangeBoundaries, rangeDimension, getRangeBoundaries etc. which may come handy, but seems not so well documented.

Remove hidden rows when reading file with phpExcel?

When reading a sheet with phpExcel using the toArray method, hidden rows are also parsed.
Is there a method I can use before toArray to remove hidden rows?
Code so far, using Codeigniter
$this->load->library('excel');
$objPHPExcel = PHPExcel_IOFactory::load($upload_data['full_path']);
foreach ($objPHPExcel->getAllSheets() as $sheet) {
$sheets[$sheet->getTitle()] = $sheet->toArray();
}
$data = array();
foreach ($sheets['Data'] as $key => $row) {
if ($key > 0) {
$item = array();
$item['name'] = $row[1];
$item['code'] = $row[2];
$data[] = $item;
}
}
When converting the sheet to Array using PHPExcel_Worksheet::toArray you will get all of the rows, regardless if they are visible or not.
If you want to filter only the visible rows you will have to iterate over the rows and check for each of them if they are visible or not. You can check the visibility of a row using
$sheet->getRowDimension($row_id)->getVisible()
$row_id starts with 1 (and not 0), same is in excel
Here is an example of how to use it in your code. I changed a bit the way you get the Data sheet, since you don't need to iterate over the sheets, you can just get that specific sheet using the getSheetByName function.
$data_sheet = $objPHPExcel->getSheetByName('Data');
$data_array = $data_sheet->toArray();
$data = [];
foreach ($data_sheet->getRowIterator() as $row_id => $row) {
if ($data_sheet->getRowDimension($row_id)->getVisible()) {
// I guess you don't need the Headers row, note that now it's row number 1
if ($row_id > 1) {
$item = array();
$item['name'] = $data_array[$row_id-1][1];
$item['code'] = $data_array[$row_id-1][2];
$data[] = $item;
}
}
}

How to read empty cells in PHPExcel?

This following code simply ignore all empty cells in my excelsheet.
Is there any way to read empty cell or replace them to "Null" values?
See the code here---https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
$file = './files/test.xlsx';
//load the excel library
$this->load->library('excel');
//read file from path
$objPHPExcel = PHPExcel_IOFactory::load($file);
//get only the Cell Collection
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
$maxCell = $objPHPExcel->getHighestRowAndColumn();
$newdata = array();
//extract to a PHP readable array format
foreach ($cell_collection as $cell) {
I tried this following code
$newdata = $objPHPExcel->rangeToArray($cell . $maxCell['column'] . $maxCell['row']);
$newdata = array_map('array_filter', $data);
$newdata = array_filter($data);
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
//header will/should be in row 1 only. of course this can be modified to suit your need.
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
}
}
//send the data in an array format
$data['header'] = $header;
$data['values'] = $arr_data;
print_r($newdata);
Get this error messgae... Fatal error: Call to undefined method PHPExcel::getHighestRowAndColumn()
you're try to wrong way, in this way you only get the last active cell value and if you try to get empty cell cell value in middle you're not able to get value. and you use some PhpSpreadsheet function and also some time you try get value using class object. check your code one more time.

Import an XLSX file into a PHP array

Is it possible to import each line of an XLSX file to a row in a PHP array?
You can use PHPExcel which is available here: https://phpexcel.codeplex.com/releases/view/119187
Here is what I use to read either xls or xlsx to an array:
require_once('/path/to/PHPExcel.php');
$filename = "example.xlsx";
$type = PHPExcel_IOFactory::identify($filename);
$objReader = PHPExcel_IOFactory::createReader($type);
$objPHPExcel = $objReader->load($filename);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheets[$worksheet->getTitle()] = $worksheet->toArray();
}
print_r($worksheets);
UPDATE / 2022-02-13:
PhpSpreadsheet has been available for a few years now and has replaced PHPExcel. The following code is more or less the same as above with a couple small improvements:
Converted code to a function or method.
Auto detect filetype.
Added ability to specify how null values, formatting and formulas are handled.
Most importantly, call the destructor and clear memory. Without this last step I was running out of memory all the time after loading large files.
/**
* Create a multidimensional array of worksheets from a filename.
*
* #param mixed $nullValue Value returned in the array entry if a cell doesn't exist
* #param bool $calculateFormulas Should formulas be calculated?
* #param bool $formatData Should formatting be applied to cell values?
*
* #return array
*/
function spreadsheet_to_array($nullValue = null, $calculateFormulas = true, $formatData = false) {
$results = [];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$results[$worksheet->getTitle()] = $worksheet->toArray($nullValue, $calculateFormulas, $formatData);
}
// save memory
$spreadsheet->__destruct();
$spreadsheet = NULL;
unset($spreadsheet);
return $results;
}
I use this:
include 'simplexlsx.class.php';
$xlsx = #(new SimpleXLSX('myFile.xlsx'));
$data = $xlsx->rows();
You can simplexslx from here.
UPDATE
Apparently the link above doesn't work anymore. You can now use this. (Thanks #Basti)
Problem can be solved using PHPExcel library:
$data = [];
$type = PHPExcel_IOFactory::identify($filepath);
$objReader = PHPExcel_IOFactory::createReader($type);
$objPHPExcel = $objReader->load($filepath);
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
foreach($rowIterator as $row){
$cellIterator = $row->getCellIterator();
foreach ($cellIterator as $cell) {
$data[$row->getRowIndex()][$cell->getColumn()] = $cell->getCalculatedValue();
}
}
where $filepath - path to your xls or xlsx file.
Yes with phpspreadsheet :
include 'vendor/autoload.php';
if($_FILES["import_excel"]["name"] != '')
{
$allowed_extension = array('xls', 'csv', 'xlsx');
$file_array = explode(".", $_FILES["import_excel"]["name"]);
$file_extension = end($file_array);
if(in_array($file_extension, $allowed_extension))
{
$file_name = time() . '.' . $file_extension;
move_uploaded_file($_FILES['import_excel']['tmp_name'], $file_name);
$file_type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file_name);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($file_type);
$spreadsheet = $reader->load($file_name);
unlink($file_name);
$data = $spreadsheet->getActiveSheet()->toArray();
foreach($data as $row)
{
$insert_data = array(
':test1' => $row[0],
':test2' => $row[1],
':test3' => $row[2],
':test4' => $row[3]
);
};
$query = "
INSERT INTO post
( test1, test2, test3, test4)
VALUES
( :test1, :test2, :test3, :test4)
";
$statement = $connect->prepare($query);
$statement->execute($insert_data);
}
echo "succes";
}else{
echo "only xls,csv,xlsx are allowed";
}
With the new version of PHPSpreadSheet you can simply do that :
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
/*...*/
$reader = new Xlsx();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load('upload/file.xls');
$sheet = $spreadsheet->getSheet($spreadsheet->getFirstSheetIndex());
$data = $sheet->toArray();
Just be careful, you have all cells as value. For exemple, date is converted to int so you need to convert it
You can use NumberFormat to see all converter.
Exemple to convert an int cell to date :
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
/*....*/
//$value is an integer of a cell value
$value = 44823
$stringDate = NumberFormat::toFormattedString($value, 'YYYY-MM-DD');
// 2022-09-19 is displayed
echo $stringDate;
Found here : https://blog.programster.org/phpspreadsheet-read-excel-file-to-array
More information in the documentation : https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-files/ https://phpspreadsheet.readthedocs.io/en/latest/
Source code of NumberFormat : https://phpoffice.github.io/PhpSpreadsheet/classes/PhpOffice-PhpSpreadsheet-Style-NumberFormat.html
<?php
require_once 'SimpleXLSX.php';
if ( $xlsx = SimpleXLSX::parse('pricelist.xlsx') ) {
print_r( $xlsx->rows() );
} else {
echo SimpleXLSX::parseError();
}
?>
SimpleXLSX

Getting cells by coordinate

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
// I wish
echo $cellIterator->getCell("A3"); // row: $row, cell: A3
}
}
I'm looking for a similar method which named getCell above or well-writed PHPExcel documentation.
Thanks.
If you have the $row information from RowIterator, you can just easily call:
$rowIndex = $row->getRowIndex ();
$cell = $sheet->getCell('A' . $rowIndex);
echo $cell->getCalculatedValue();
The complete code would be:
foreach($worksheet->getRowIterator() as $row){
$rowIndex = $row->getRowIndex();
$cell = $worksheet->getCell('A' . $rowIndex);
echo $cell->getCalculatedValue();
$cell = $worksheet->getCell('B' . $rowIndex);
echo $cell->getCalculatedValue();
}
This is what I needed:
function coordinates($x,$y){
return PHPExcel_Cell::stringFromColumnIndex($x).$y;
}
implementation:
coordinates(5,7); //returns "E7"
Though one could also do this for A-Z columns:
function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}
function lCoordinates($x,$y){
$x = $toNumber($x);
return PHPExcel_Cell::stringFromColumnIndex($x).$y;
}
implementation:
lCoordinates('E',7); //returns "E7"
Rather than iterate all the Cells in a row, when not use the rangeToArray() method for the row, and then use array_intersect_key() method to filter only the columns that you want:
$worksheet = $objPHPExcel->getActiveSheet();
$highestColumn = $worksheet->getHighestColumn();
$columns = array_flip(array('A','C','E'));
foreach($worksheet->getRowIterator() as $row)
{
$range = 'A'.$row->getRowIndex().':'.$highestColumn.$row->getRowIndex();
$rowData = $worksheet->rangeToArray( $range,
NULL,
TRUE,
TRUE,
TRUE);
$rowData = array_intersect_key($rowData[$row->getRowIndex()],$columns);
// do what you want with the row data
}
EDIT
The latest SVN code introduces a number of new methods to th iterators, including the ability to work with ranges, or set the pointer to specific rows and columns

Categories