Merging cells in Excel by rows and columns together using PHPExcel - php

I need to merge cells in Excel (xlsx) by rows and again by columns using PHPExcel. I tried the following.
$sheet->mergeCells("G".($row_count+1).":G".($row_count+4));
$sheet->mergeCells("H".($row_count+1).":H".($row_count+4));
$sheet->mergeCells("I".($row_count+1).":I".($row_count+4));
Where the variable $row_count has some unpredictable dynamic value like 25, 50, 75 and so on (no regular pattern).
It merges the cells as shown in the preceding snap shot as can be seen immediately below the Note cell. After merging these cells by rows, I'm trying to merge them by columns as follows.
$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));
but it doesn't work. When I try to open the excel file, it asks for a confirmation (with a confirmation box)
Excel found unreadable content in 'report.xlsx'. Do you want to
recover the contents of this workbook? If you trust the source of this
workbook, click Yes.
How to merge cells by rows and columns together in Excel then?

Merging simply requires a valid range of cells like A1:B2, so your
$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));
should work without any problem.
Can you please experiment with a simple test case to prove that this is causing you a problem, and not something else in your script
EDIT
After rereading your question:
Your problem may be that you're trying to merge cells that are already part of a merge range, rather than merging each row, then trying to merge by column, try merging the full rangein one go.
$sheet->mergeCells("G".($row_count+1).":I".($row_count+4));

There is one more method for cell merging
/**
* Set merge on a cell range by using numeric cell coordinates
*
* #param int $pColumn1 Numeric column coordinate of the first cell
* #param int $pRow1 Numeric row coordinate of the first cell
* #param int $pColumn2 Numeric column coordinate of the last cell
* #param int $pRow2 Numeric row coordinate of the last cell
* #throws Exception
* #return PHPExcel_Worksheet
*/
public function mergeCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1)

function cellsToMergeByColsRow($start = -1, $end = -1, $row = -1){
$merge = 'A1:A1';
if($start>=0 && $end>=0 && $row>=0){
$start = PHPExcel_Cell::stringFromColumnIndex($start);
$end = PHPExcel_Cell::stringFromColumnIndex($end);
$merge = "$start{$row}:$end{$row}";
}
return $merge;
}
Addition to the case:
$objPHPExcel->getActiveSheet()->mergeCells(cellsToMergeByColsRow(0,2,3))

I make a simple function to calc cells to merge by cols and row.
function cellsToMergeByColsRow($start = NULL, $end = NULL, $row = NULL){
$merge = 'A1:A1';
if($start && $end && $row){
$start = PHPExcel_Cell::stringFromColumnIndex($start);
$end = PHPExcel_Cell::stringFromColumnIndex($end);
$merge = "$start{$row}:$end{$row}";
}
return $merge;
}
And call
$sheet->mergeCells(cellsToMergeByColsRow($col, $col+5, $row));
Thanks #Mark Baker

I was also looking solution for this question. where i want to merge cell and put content (value) on that. After few search i got some solution on this. but did not checked because i am using Maatawebsite for get Excel file.
But any one can try thing.. Solution is based on PHPExcel nit sure ,it will work on Maatawebsite.
Source Link
Merge from column A row 1 to column E row 1
$objPHPExcel->getActiveSheet()->mergeCells('A1:E1');
// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');
Merge from column A row 1 to column E row 3
$objPHPExcel->getActiveSheet()->mergeCells('A1:E3');
// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');
I checked maatawebsite document and they have same method mergeCells. so i think i would be work.
This Solution from Maatawebste.
$sheet->cells('A1:C1', function($cells) {
$cells->setBorder('thin', 'thin', 'thin', 'thin');
});
$sheet->mergeCells('A1:C1');
Solution 2nd
$sheet->setMergeColumn(array(
'columns' => array('A','B','C','D'),
'rows' => array(
array(2,3),
array(5,11),
)
));

$sheet -> mergeCellsByColumnAndRow($col1, $row1, col2, row2);
is the function.

Related

How grouping rows by field in PHPExcel

I have a problem when creating excel files with the phpExcel library.
I want to create groups on certain lines based on the sales column (with same sales name).
I have made the file manually by using the Subtotal feature on the Data tab in Excel.
Is it possible that phpexcel has such feature?
You can see the sample file that I mean on the link / image that I uploaded.
You can group the rows like this
$objPHPExcel->getActiveSheet()->getRowDimension('5')->setOutlineLevel(1);
For more details you can visit here.
You can also go here and here for working examples
Using the PHPExcel , you can use the methods :
setOutlineLevel($level) // the level of the current row
setVisible(false) // show or hide the group
setCollapsed(true) // add collapse to group (as figure out in the picture above )
THe point here is to define group condition , example :
We suppose that the file excel is already generated with PHPExcel and
now you are going to read the file again for grouping rows .
First let's read the column C that contain name of salesman and store them inside the $salemans :
$salesmans = array();
/*
the output of $salesmans seems to be something like :
[0] ==> "Abdul Karim",
[1] ==> "Apan Total",
[2] ==> ""Ari Total
*/
The array $salesman should have distinct value , so when trying to
insert inside the array verify if the current value doesn't exist
already in the $salesmans.
Example :
$salesmans=array();
if (!in_array($currentSalesman, $salesmans))
{
$array[] = $value;
}
Here , we are going to to set the level of each row by getting the value of the current salesman with the key in the $salesmans
Caution : please try to modify this section because i'm not getting
how you manage fetch rows.I'm just making code more clear to understand easy
for ($row = 0; $row <= 10000; ++$row) {
$currentsalesman = $row['c']; //
$keylevel = array_search($currentsalesman, $salesmans);// this will return the key in $salesmans array
$objPHPExcel->getActiveSheet()
->getRowDimension($row)
->setOutlineLevel($keylevel) // set here the level .
->setVisible(false)
->setCollapsed(true);
}

PHPExcel: Value not available (#N/A) from formula in Excel spreadsheet [duplicate]

I have the following Excel file:
I read it in by looping over every cell and getting the value with getCell(...)->getValue():
$highestColumnAsLetters = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); //e.g. 'AK'
$highestRowNumber = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$highestColumnAsLetters++;
for ($row = 1; $row < $highestRowNumber + 1; $row++) {
$dataset = array();
for ($columnAsLetters = 'A'; $columnAsLetters != $highestColumnAsLetters; $columnAsLetters++) {
$dataset[] = $this->objPHPExcel->setActiveSheetIndex(0)->getCell($columnAsLetters.$row)->getValue();
if ($row == 1)
{
$this->column_names[] = $columnAsLetters;
}
}
$this->datasets[] = $dataset;
}
However, although it reads in the data fine, it reads in the calculations literally:
I understand from discussions like this one that I can use getCalculatedValue() for calculated cells.
The problem is that in the Excel sheets I am importing, I do not know beforehand which cells are calculated and which are not.
Is there a way for me to read in the value of a cell in a way that automatically gets the value if it has a simple value and gets the result of the calculation if it is a calculation?
Answer:
It turns out that getCalculatedValue() works for all cells, makes me wonder why this isn't the default for getValue() since I would think one would usually want the value of the calculations instead of the equations themselves, in any case this works:
...->getCell($columnAsLetters.$row)->getCalculatedValue();
getCalculatedValue() seems to work for all cells, see above
If you are unsure about the content of a cell (value or formula included),
I recommend you to primarily do a check if the cell has a formula and then copy - paste accordingly. getOldCalculatedValue() is very helpful in this case. Here is an example of that:
$code = $sheet->getCell('A'.$y)->getValue();
if(strstr($code,'=')==true)
{
$code = $sheet->getCell('A'.$y)->getOldCalculatedValue();
}
$objPHPExcel4->setActiveSheetIndex(0)
->setCellValue('A'.$l, $code);
For large data sets, getCalculatedValue() function is really cumbersome and lots of memory will be required to perform correctly.
Looks like getCalculatedValue() is deprecated. Try using getFormattedValue() instead.
getCalculatedValue() seems to do the right job you wanted. It will return the correct value if the cell contains FBV ( formula based value ). If not then the normal value will be returned instead.
getCalculatedValue
seems to work for all cells
$sheets = $spreadsheet->getAllSheets();
$priceCasegetCellByColumnAndRow = $sheet->getCellByColumnAndRow(14, ($key))->getCalculatedValue()
$priceCasegetCell = $sheet->getCell('O' . $key)->getCalculatedValue();
I have never imported an excel file in PHP so this is just a stab in the dark.
Why not check the first character in the cell for an "="
If true getCalculatedValue()
if not getCell()

PHPExcel How to get only 1 cell value?

I would think that a getCell($X, $y) or getCellValue($X, $y) would be available for one to easily pick a a certain value. This can be usefully, as example crosscheck data prior to a larger process.
How do you get a specific value from say cell C3.
I do not want an array of values to sort through.
Section 4.5.2 of the developer documentation
Retrieving a cell by coordinate
To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCell method. A cell’s value can be read again using the following line of code:
$objPHPExcel->getActiveSheet()->getCell('B8')->getValue();
Section 4.5.4 of the developer documentation
Retrieving a cell by column and row
To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCellByColumnAndRow method. A cell’s value can be read again using the following line of code:
// Get cell B8
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, 8)->getValue();
If you need the calculated value of a cell, use the following code. This is further explained in 4.4.35
// Get cell B8
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, 8)->getCalculatedValue();
By far the simplest - and it uses normal Excel co-ordinates:
// Assuming $sheet is a PHPExcel_Worksheet
$value = $sheet->getCell( 'A1' )->getValue();
You can separate the co-ordinates out in a function if you like:
function getCell( PHPExcel_Worksheet $sheet, /* string */ $x = 'A', /* int */ $y = 1 ) {
return $sheet->getCell( $x . $y );
}
// eg:
getCell( $sheet, 'B', 2 )->getValue();
This is a source based answer feel free to improve or comment.
function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}
function myFunction($s,$x,$y){
$x = toNumber($x);
return $s->getCellByColumnAndRow($x, $y)->getFormattedValue();
}
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->setActiveSheetIndex(0);
$sheetData = $objPHPExcel->getActiveSheet();
$cellData = myFunction($sheetData,'B','2');
var_dump($cellData);
This does not work past the letter Z, and could be improved but works for my needs.

Searching a CSV With PHP

I have a large CSV file. The first column contains the name of a processor. The second, the processor's benchmark score. EG:
Intel Pentium Dual T3400 # 2.16GHz,1322
Using a PHP script, I would like to search the first column for a string (EG. Pentium Dual T3400), and (assuming that there is only one result per search, else return an error message) create a variable containing the value of the second column.
I don't know if this will help, but I was sort of hoping it would look a little like this:
$cpuscore = csvsearch(CSV_file,query_string,column#_to_search,column#_to_return)
Where $cpuscore would contain the score of the processor name that matches the search query.
Feel free to suggest something that would produce similar results. I have MySQL, but I don't have the permissions to import tables from CSV.
You can use the php function fgetcsv(), http://php.net/manual/en/function.fgetcsv.php to traverse the csv file row by row. For instance:
$ch = fopen($path_to_file, "r");
$found = '';
/* If your csv file's first row contains Column Description you can use this to remove the first row in the while */
$header_row = fgetcsv($ch);
/* This will loop through all the rows until it reaches the end */
while(($row = fgetcsv($ch)) !== FALSE) {
/* $row is an array of columns from that row starting at 0 */
$first_column = $row[0];
/* Here you can do your search */
/* If found $found = $row[1]; */
/* Now $found will contain the 2nd column value (if found) */
}
I like to iterate through each line of a csv file and find the words i'm looking for, and compile a result from there. Here's something to get you started:
<?php
$query = "Intel Core i7 3600M"
$file = file('db.csv');
foreach($file as $value) {
if(stristr($value,$query)){
$items = explode(",", $value); echo $items[1];
};
};
?>

How to automatically read in calculated values with PHPExcel?

I have the following Excel file:
I read it in by looping over every cell and getting the value with getCell(...)->getValue():
$highestColumnAsLetters = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); //e.g. 'AK'
$highestRowNumber = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$highestColumnAsLetters++;
for ($row = 1; $row < $highestRowNumber + 1; $row++) {
$dataset = array();
for ($columnAsLetters = 'A'; $columnAsLetters != $highestColumnAsLetters; $columnAsLetters++) {
$dataset[] = $this->objPHPExcel->setActiveSheetIndex(0)->getCell($columnAsLetters.$row)->getValue();
if ($row == 1)
{
$this->column_names[] = $columnAsLetters;
}
}
$this->datasets[] = $dataset;
}
However, although it reads in the data fine, it reads in the calculations literally:
I understand from discussions like this one that I can use getCalculatedValue() for calculated cells.
The problem is that in the Excel sheets I am importing, I do not know beforehand which cells are calculated and which are not.
Is there a way for me to read in the value of a cell in a way that automatically gets the value if it has a simple value and gets the result of the calculation if it is a calculation?
Answer:
It turns out that getCalculatedValue() works for all cells, makes me wonder why this isn't the default for getValue() since I would think one would usually want the value of the calculations instead of the equations themselves, in any case this works:
...->getCell($columnAsLetters.$row)->getCalculatedValue();
getCalculatedValue() seems to work for all cells, see above
If you are unsure about the content of a cell (value or formula included),
I recommend you to primarily do a check if the cell has a formula and then copy - paste accordingly. getOldCalculatedValue() is very helpful in this case. Here is an example of that:
$code = $sheet->getCell('A'.$y)->getValue();
if(strstr($code,'=')==true)
{
$code = $sheet->getCell('A'.$y)->getOldCalculatedValue();
}
$objPHPExcel4->setActiveSheetIndex(0)
->setCellValue('A'.$l, $code);
For large data sets, getCalculatedValue() function is really cumbersome and lots of memory will be required to perform correctly.
Looks like getCalculatedValue() is deprecated. Try using getFormattedValue() instead.
getCalculatedValue() seems to do the right job you wanted. It will return the correct value if the cell contains FBV ( formula based value ). If not then the normal value will be returned instead.
getCalculatedValue
seems to work for all cells
$sheets = $spreadsheet->getAllSheets();
$priceCasegetCellByColumnAndRow = $sheet->getCellByColumnAndRow(14, ($key))->getCalculatedValue()
$priceCasegetCell = $sheet->getCell('O' . $key)->getCalculatedValue();
I have never imported an excel file in PHP so this is just a stab in the dark.
Why not check the first character in the cell for an "="
If true getCalculatedValue()
if not getCell()

Categories