PHPExcel - Proceed if sheet name equals - php

Here's the scenario, I want to be able to run a specific section of PHP code for each different excel sheet, so basically if I have a workbook that has the following sheets, "Funds", "Accounts", "Spending Data", how can I look through each sheet to be able to run separate code depending on the sheet name, so for example, I only want to fetch the cell data from A5:D5 on "Funds", but on "Accounts" I want to get the A5:P5, etc.
I've tried using the foreach ($objPHPExcel->getWorksheetIterator() as $worksheet), but that just loops through each sheet, which I would expect it to do then runs the same code on each sheet which is not what I'm looking for.
Sorry if I've not explained it very well, but if there was someway I could do something like;
if sheetname == "Funds" {
//get cells A5:D5
}
if sheetname == "Accounts" {
//get cells A5:P5
}
etc...
Any help would be greatly appreciated.
Many thanks in advance.
Kind regards.

Use getTitle to know the sheet's title:
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$sheetname = $worksheet->getTitle();
if ($sheetname == "Funds")
{
//get cells A5:D5
}
if ($sheetname == "Accounts")
{
//get cells A5:P5
}
}

Related

How to search and read excel file in php

I would like someone to help me find a solution to my two problems: I have an excel file with 2 columns (A and B)
Column A contains barcode numbers that I must assign to an order number (in column B). Below an example:
COLUMN A....................COLUMN B
barcode.........................order number
12345678*******************35098
98765432 ******************35433
45454545
66554433
98766334 *******************43221
What I am looking for is the following:
from a given order number (in a variable), I would like to know if it exists in column B, if yes, I would like to recover its bar code.
If it does not exist, I would like to add this order number in one of the empty cells in column B and then retrieve the barcode that has been assigned to it.
All this using PHPExcel ... I tried some, but I'm blocking ... If anyone could help me, I'd be very grateful to him.
Thank you
PS: A small precision, if you have to know the number of non-empty line of column A, we will say that there are 1000 lines
Here what I did. This code only check if the order number exist. But I'm not sure if this is the best way:
with this code, I know if the ordernumber exist in file.
My problem now is how to get the bar code number if the ordernumber exist in column B, or if it doesn't exist, how to add the order number in one of the empty cells and retrieve the corresponding barcode
Thanks to everyone for your help
<?php
$orderNumber = 12341;
function searchOrder ($num){
/** PHPExcel_IOFactory */
require_once 'Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("barreCode_ordernumber.xlsx");
$foundInCells = array();
$ordernumber = $num;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
$numligne = $cell->getRow();
$numcolonne = $cell->getColumn();
if ($cell->getValue() == $ordernumber) {
return true;
}
}
}
}
}
?>

Search a cell by string in PHPExcel

I want to know if it's possible to get a cell by its name in an xls document, I mean Ii have this info in a excel file:
Normally to get the coordinate of the cell with the value "ASUS"
$objPHPExcel->getActiveSheet()->getCell('B3')->getValue();
my problem is that users send my this excel file, and sometimes the rows are in disorder, e.g the row B3 sometimes appear in a different row like "B6" or "B7" or "B5", how I can get the cell "ASUS" getting by cell name "Modelo"
There is nothing built-in to PHPExcel to do a search, but using the following example, it can do well for your problem.
$foundInCells = array();
$searchTerm = 'ASUS';
foreach ($objPHPExcel->getWorksheetIterator() as $CurrentWorksheet) {
$ws = $CurrentWorksheet->getTitle();
foreach ($CurrentWorksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) {
if ($cell->getValue() == $searchTerm) {
$foundInCells[] = $ws . '!' . $cell->getCoordinate();
}
}
}
}
var_dump($foundInCells);
You'd have to loop through the entire file to find it just like searching a value in a 2D array.
Take a look at this answer

PHPExcel search cell by value

I'm wondering, how it's possible to help with such issue.
Let's say, I have an excel with such info in it (it could be much more info):
**Country** **Currency**
Germany EUR
USA USD
Russia RUB
and I'm entering in input form "USA", and I want to see the result USD from excel.
Is there some kind of function in PHP, which allows to search for a value in excel?
Or at least, if there existing such function, which returns in which cell (e.g. B2) such value exists?
There's nothing built-in to PHPExcel to do a search, but it's pretty straightforward to write something yourself based around the iterators.... take a look at 28iterator.php in /Examples
$foundInCells = array();
$searchValue = 'USA';
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$ws = $worksheet->getTitle();
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) {
if ($cell->getValue() == $searchValue) {
$foundInCells[] = $ws . '!' . $cell->getCoordinate();
}
}
}
}
var_dump($foundInCells);
Of course, if you're only wanting to search a specific column in a specific worksheet, you can simplify this a great deal, e.g. using rangeToArray() and then searching the array using standard PHP array functions.
As there a re many different Excel Formats (2003, 2010 quirks, ooxml, etc.) you will have to look for a third party library to read excel files.
Find some examples in this question or in this question.
Edit: added a more current question.

To upload Excel and store it in database?

I want to upload an Excel file into our webpage, then corresponding data store it in database. And then I want to retrieve all data and display it in table format. I have one code but using that I can't upload all Excel files. Only a single format can be upload.
Below is the function. But there is some restriction.
public function check_excel($filename)
{
$path='./assets/uploads/excel/'.$filename;
$this->load->library('excel');
$inputFileType = PHPExcel_IOFactory::identify($path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = PHPExcel_IOFactory::load($path);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$xf[]='';
$result[]='';
$first_check='';
$var_check=0;
for ($row = 13; $row <= $highestRow; $row++)
{
$xf[$row]=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex(); // Get sheet index value
if($row>13 && $row<16) //This block check first kpi data expand or not
{
if($xf[$row-1]==$xf[$row]) //check parent and child sheet index value same
$first_check='false';
if ($row==15)
{
if($xf[$row]==$xf[$row-1] || $xf[$row]==$xf[$row-2]) // check the grand-child sheet index value same in parent and child
$first_check='false';
else
{
$first_check='true';
$a=$row-2;
$b=$row-1;
$check_kpi=$objPHPExcel->getActiveSheet()->getCell('A'.$a)->getXfIndex();
$check_unit=$objPHPExcel->getActiveSheet()->getCell('A'.$b)->getXfIndex();
$check_sub_unit=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex();
}
}
}
if($first_check=='true') //This block check second kpi to upto last kpi data expand or not
{
if($row>15)
{
if($var_check==1) // This block check the child data expand or not
{
if($check_unit!=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex())
{
$result[$row]='false';
break;
}
}
if($var_check==2) // this block check the grand - child data expand or not
{
if($check_sub_unit!=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex())
{
$result[$row]='false';
break;
}
}
if($xf[$row]!=$check_sub_unit)
{
if($xf[$row]!=$check_unit)
$var_check=1; // var_check value is one, the kpi is present
else
$var_check=2; // var_check value is two, the unit is present
}
else
$var_check=0; // var_check value is zero, the sub_unit is present
}
}
else if($first_check=='false')
{
$result[$row]='false';
break;
}
}
$return='true';
for ($row = 13; $row <= $highestRow; $row++)
{
if(!empty($result[$row]))
{
if($result[$row]=='false'){
$return='false';
break;
}
}
}
return $return;
}
It sounds like you are using a relational DB (e.g. MySQL, Postgres, etc), which uses fixed column tables.
You should probably use a Document-based DB (e.g. CouchDB, Mongo, etc). This would be the best solution.
But, if you're stuck using a relational DB, you can use an EAV model.
Here is a basic example:
Create a table for the entity (excel file): EntityID, ExcelFileName
Create a table for the attribute (column info): AttributeID, EntityID, AttributeName
Create a table for the value (excel row/column): ValueID, RowNumber, AttributeID, AttributeValue
The downside is that the AttributeValue isn't specifically typed (it's just varchar/text). You can solve this by adding a "AttributeType" to the attribute table that is then used in your code to know what type of data that column should contain. BUT, unless you know the contents/format of the Excel file in advance, you'll probably have to GUESS what the type of a column is...which isn't hard as long as the excel file isn't messed up.
If you're just displaying the data that was imported, this probably isn't a big deal.
There are other (more complex) ways to implement EAV, including one with typed columns, if you have such a need.
Have you tried PHPExcel?
They also have a codeigniter library.
And this post might interest you : how to use phpexcel to read data and insert into database?
You can use PHPExcel of course, but have a look at other data format. Using comma-separated or tab-separated values can help you to solve your problem easily. Excel can save datasheets in these simple formats. Anyway, you cannot save formulas or conditional formatting in you database Moreover, it is much faster and robust and you can import CSV files with LOAD DATA INFILE query.

PHPExcel. How to check if current cell is merged with another?

I use PHPExcel to import Excel files to my site. For example, there is two cells, A1 and A2, both merged. Is there a way to to find out is A1 merged to another cell (A2 or other) or not?
$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sheet->mergeCells('A1:E1');
$cell = $sheet->getCell('A1');
// Check if cell is merged
foreach ($sheet->getMergeCells() as $cells) {
if ($cell->isInRange($cells)) {
echo 'Cell is merged!'
break;
}
}
I think there isn't better solution because of the way phpexcel stores information about merged cells.
The easiest way is to use the getMergeRange() method.
if ($cell->getMergeRange()) {
// cell is merged
}
I suspect many people will want to know the value of the merged cell, in which case you can refer to the following code:
if (($range = $cell->getMergeRange()) && !$cell->isMergeRangeValueCell()) {
$first_in_range_coordinates = strtok($range, ':');
$value = $sheet->getCell($first_in_range_coordinates)->getValue();
}

Categories