How to get cell values and cell colors from spreadsheet-reader-master or PHPExcell?
<?php
require('spreadsheet-reader-master/php-excel-reader/excel_reader2.php'); //spreadsheet-reader-master
require('spreadsheet-reader-master/SpreadsheetReader.php'); //spreadsheet-reader-master
$reader = new SpreadsheetReader('Book1.xlsx'); // xlsx file
foreach ($reader as $key ) {
//foreach loop
echo "<pre>";
print_r($key);
}
<?php
require('./PHPExcel-1.8/Classes/PHPExcel.php');
$tmpframe = './Book1.xlsx';
$exceReader = PHPExcel_IOFactory::createReaderForFile($tmpframe);
$excelObj = $exceReader->load($tmpframe);
$worksheet = $excelObj->getActiveSheet();
$lastRow = $worksheet->getHighestRow();
// $highestRow = $sheet->getHighestRow();
// $highestColumn = $worksheet->getHighestColumn();
$rowl = $worksheet->getHighestRow();
$coll = $worksheet->getHighestColumn();
echo "number of rows---".$rowl."<br>";
echo "number of column---".$coll."<br>";
$rowdumy = 20;
$crt = 'QT';
$crt++;
for ($row=1; $row <= $rowdumy; $row++) {
echo "<h6>number of-----".$row."</h5><br>";
for ($i = 'A'; $i !== $crt; $i++){
$cell = $worksheet->getCell($i.$row);
$colurc = $excelObj->getActiveSheet()->getStyle($i.$row)->getFill()->getStartColor()->getARGB();
if($cell != '')
{
echo $cell."-[[".$colurc."]]";
}
}
echo "<br>";
}
?>
Related
Is it possible by code to skip the Header (Top Row) in Excel Spreadsheet?
I'm using PHPExcel_Reader to process upload into database.
this image is my data excel
this is my code:
<?php require_once('../../php-excel-reader/excel_reader2.php'); require_once('../../SpreadsheetReader.php'); if (isset($_POST["import"])) { $allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']; if(in_array($_FILES["file"]["type"],$allowedFileType)){ $targetPath = '../assets/uploads/'.$_FILES['file']['name']; move_uploaded_file($_FILES['file']['tmp_name'], $targetPath); $Reader = new SpreadsheetReader($targetPath); $sheetCount = count($Reader->sheets()); for($i=0;$i<$sheetCount;$i++) { $Reader->ChangeSheet($i); foreach ($Reader as $Row) { $student_id = ""; if(isset($Row[0])) { $student_id = mysqli_real_escape_string($conn,$Row[0]); } $roll_no = ""; if(isset($Row[1])) { $roll_no = mysqli_real_escape_string($conn,$Row[1]); } $student_name = ""; if(isset($Row[2])) { $student_name = mysqli_real_escape_string($conn,$Row[2]); } $class_name = ""; if(isset($Row[3])) { $class_name = mysqli_real_escape_string($conn,$Row[3]); } ?>
I use this method for skip header :
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('FILE.xlsx');
$sheetData = array();
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
$sheetData[$row][$col] = $val;
}
}
}
unset($sheetData[1]); // SKIP HEADER
foreach ($sheetData as $val) {
// set data for upload DB
}
i just unset that from array.
I am just learning to code, i dont know much yet.
<!doctype>
<html>
<head>
</head>
<body>
<?php
require_once "Classes/PHPExcel.php";
$tmpfname = "tabula1.xlsx";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);//
$lastRow = $worksheet->getHighestRow();
echo "<table>";
for ($row = 1; $row <= $lastRow; $row++) {
echo "<tr><td>";
echo $worksheet->getCell('A'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('B'.$row)->getValue();
echo "</td><tr>";
}
echo "</table>";
</body>
</html>
here's my code. I need to output the data in json from excel table. I have been searching for solution in youtube and google, but i have found nothing i understand so far. What should i write, what should i do? Also, i got this code from this forum and i edited it so it works for me. I would be very grateful if someone could help me with this <3
Try something like this, using your loop to create an array and the json_encode to convert it to a json string:
<!doctype>
<html>
<head>
</head>
<body>
<?php
require_once "Classes/PHPExcel.php";
$tmpfname = "tabula1.xlsx";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);//
$lastRow = $worksheet->getHighestRow();
$data = [];
for ($row = 1; $row <= $lastRow; $row++) {
$data[] = [
'A' => $worksheet->getCell('A'.$row)->getValue(),
'B' => $worksheet->getCell('B'.$row)->getValue()
];
}
echo json_encode($data);
</body>
</html>
Only logic not tested
$result=array();
for ($row = 1; $row <= $lastRow; $row++)
{
array_push($result,array($worksheet->getCell('A'.$row)->getValue(),$worksheet->getCell('B'.$row)->getValue()));
}
echo json_encode($result);
https://github.com/PHPOffice/PhpSpreadsheet
<?php
require(DIR_SYSTEM . 'library/export_import/vendor/autoload.php');
$inputFileName = __DIR__ . '/1.xlsx';
$inputFileType = 'Xlsx';
/** Create a new Reader of the type defined in $inputFileType **/
$reader = PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader that we only want to load cell data **/
$reader->setReadDataOnly(true);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
$worksheet = $spreadsheet->getSheet(0);//
// Get the highest row and column numbers referenced in the worksheet
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
$data = array();
for ($row = 1; $row <= $highestRow; $row++) {
$riga = array();
for ($col = 1; $col <= $highestColumnIndex; $col++) {
$riga[] = $worksheet->getCellByColumnAndRow($col, $row)->getValue();
}
if (1 === $row) {
// Header row. Save it in "$keys".
$keys = $riga;
continue;
}
$data[] = array_combine($keys, $riga);
}
header('Content-Type: application/json');
print json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
I have the code that can save the uploaded csv file in the database. But the problem is that it can only save 100-200 rows out of more than 1000 rows. I have an idea to save rows batch by batch instead of a whole but i don't know how to code it.
require_once 'PHPexcel/PHPExcel.php';
$objPHPExcel = PHPExcel_IOFactory::load($file);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 2; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
}
}
I have a excel file which contains certain rows with color i want to get the row id of a particular color code but unable to do it .. already searched but found nothing below is my code for PHPEXCEL
$cellColor = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();
This will give me the color code and for the value i have $cell->getValue() where $cell is some variable for $cellIterator
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell)
{
$cellColor = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();
if (!empty($cell->getCalculatedValue())) {
if ($cellColor == 'yellow') {
echo ($cellColor.'======'.$cell->getValue());
}
}
}
}
$cell->getValue() will give me the value of that particular color code But, the problem is if i have 2 rows with color yellow then $cell->getValue() will give two value like 0-> yellow1 1-> yellow2 but after deleting the 1st yellow colour data in excel then result will be 0-> yellow2 which is wrong what i need is 0->'' 1-> yellow2 Thats why i need row id for that particular color so that i can identify the row.
After hours of practicing got my expected result
$objPHPExcel = PHPExcel_IOFactory::load('someFile.xls');
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
//$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//$nrColumns = ord($highestColumn) - 64;
$groupCount = array();
$standardSetCount = array();
$standardCount = array();
$learningTargetCount = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col < $highestColumnIndex; ++$col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$colorCode = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();
/*
* Yellow
*/
if ($colorCode == 'FFFF00') {
$val = $cell->getValue();
//$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
$groupCount[] = $val; // $groupCount[] = $dataType;
}
/*
* gold
*/
if ($colorCode == 'CC9900') {
$val = $cell->getValue();
$standardSetCount[] = $val;
}
/*
* red
*/
if ($colorCode == 'FF3333') {
$val = $cell->getValue();
$standardCount[] = $val;
}
/*
* green
*/
if ($colorCode == '00CC33') {
$val = $cell->getValue();
$learningTargetCount[] = $val;
}
}
}
$group = (array_chunk($groupCount, $highestColumnIndex));
$standardSet = (array_chunk($standardSetCount, $highestColumnIndex));
$standard = (array_chunk($standardCount, $highestColumnIndex));
$learningTarget = (array_chunk($learningTargetCount, $highestColumnIndex));
echo '<pre>';
print_r($learningTarget);
}
I have used PHPExcel to import data from excel to PHP.
In that i am getting one date column using PHPExcel_shared_date::ExcelToPHP thats is getting properly if i put valid date in cell but Problem is if i make it blank while importing, it takes Current Date.
for ($row = 2; $row <= $highestRow + 1; $row++) {
$date_cell = $ci->excel->setActiveSheetIndex(0)->getCell('D' . $row)->getValue();
//echo $date_cell;
if ($row == $highestRow + 1 || in_array($row, $merge_row)) {
if (isset($detail_array) && !empty($detail_array))
$data[] = $detail_array;
}
if (in_array($row, $merge_row)) {
$i = 0;
$detail_array = array();
$detail_array['account'] = $ci->excel->setActiveSheetIndex(0)->getCell("A" . $row)->getvalue();
} else {
for ($col = 0; $col < count($field); $col++) {
$val = $workSheet->getCellByColumnAndRow($col, $row)->getValue();
if ($value_cell && $val) {
$detail_array['detail'][$i][$field[$col]] = $val;
$detail_array['detail'][$i]['date'] = date('d-m-y', PHPExcel_Shared_Date::ExcelToPHP($date_cell));
}
}
$i++;
}
}
print_r($data);
You help would be appreciated, Thanks!
There is problem in flow, May you have to do like:
for ($row = 2; $row <= $highestRow + 1; $row++) {
if ($row == $highestRow + 1 || in_array($row, $merge_row)) {
if (isset($detail_array) && !empty($detail_array))
$data[] = $detail_array;
}
if (in_array($row, $merge_row)) {
$i = 0;
$detail_array = array();
$detail_array['account'] = $ci->excel->setActiveSheetIndex(0)->getCell("A" . $row)->getvalue();
} else {
$alpha = "A";
for ($col = 0; $col < count($field); $col++) {
$val = $workSheet->getCellByColumnAndRow($col, $row)->getValue();
$detail_array['detail'][$i][$field[$col]] = "";
if ($value_cell && $val && trim($val) != "") {
if (PHPExcel_Shared_Date::isDateTime($ci->excel->setActiveSheetIndex(0)->getCell($alpha . $row)))
$detail_array['detail'][$i][$field[$col]] = date('d-m-y', PHPExcel_Shared_Date::ExcelToPHP($val));
else
$detail_array['detail'][$i][$field[$col]] = $val;
}
$alpha++;
}
$i++;
}
}
print_r($data);