Spread sheet header row data validation using PHP Excel - php

I uploaded the excel and the data is successfully inserted to the database, by submitting the "submit button" using php script. There is an final stage for me to do is the validation of the excel before inserting the data to the database. I don't get any idea on validation.
In my excel, i should validate that the first row should contain "the following heading"(ie numbers,marks,grade,attention etc) and then from the second row the values should be checked(validate) by the PHP script(number should be only 8digits,marks should be <=3digits,grade should be 1digit,attention should be <=2digits and finally empty row) and finally should gets inserted to the database by successfully. How can i validate the excel rows and columns using PHP script?
I am using PHP Excel library for read the spread sheet data
Thanks in advance

You can do it using following code
$objReader = PHPExcel_IOFactory::load($filename);
$objWorksheet = $objReader->getActiveSheet();
//This will fetch the first row
$row = $objWorksheet->getRowIterator(1)->current();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
//You can loop through the columns to see the data and match it with your some predefined array of headings.
foreach ($cellIterator as $cell) {
echo $cell->getValue();
}
Hope this helps

Related

PHP CSV Importer : Multiple rows due to line break in one column

Trying to import data from CSV files using PHP Codeigniter.
The file has column description in which the data entered has some enter or new line within a cell but it does not create a new row in CSV. On the other hand, while importing using php it generates a new row in the loop which is being inserted as a new record in the database.
How to resolve this issue. Picture attached.
$csv_array = $this->csvreader->parse_file($file_path);
foreach ($csv_array as $row) {
$description =$row['description'];
echo '<pre>';
var_dump(($row));
}

How can I retrieve a worksheet by checking for a name in an array using PHPExcel?

I am working on creating an upload function for Excel Sheets in PHP using the extension PHPExcel. I have an array called standard, in which all allowed sheet names within an Excel document are saved. I need to loop through this array using a foreach loop and retrieve a sheet, should one of the worksheets be named the same as an entry in the array standard.
I have implemented it as follows:
foreach (array_keys($this->standard) as $k => $klasse) {
$sheet = $this->xlsx->getSheetByName($klasse);
die($klasse.' - '.print_r($sheet,true));
The die at the end is to check if the process even returns any data. Unfortunately, I do not get any sheet returned, only the variable klasse. I am assuming this is due to the wrong use of the function getSheetByName from PHPExcel. I'd be grateful for any help.

Search and export to excel with php

i have to write module search and export to excel with php.But if data is big,it is very slow.I think i will create a button download excel after user has search,i don't know how do it?Please help me,thank you very much!!
You haven't provided enough details for me to help you with search, but here's an easy way to export to a CSV file (which Excel can open):
$f = fopen('export.csv', 'w');
foreach ($data as $row) fputcsv($f, $row);
fclose($f);
This is assuming that $data is an array of arrays.
You can follow following steps:
Search something using forms on webpage.
Create SQL query using user entered search terms.
Fetch result from database.
Store this result in array.
Show this result on webpage by looping using result array.
Now there is a button on same webpage to export result into CSV/Excel
This button submit functionality will use same result array to export CSV
If you are allowed to use Javascript/jQuery:
Announcements: TableTools v1.0.2 - Save as Excel, CSV, copy and print
HTML Table to CSV

Need to read Excel spreadsheets, store in Oracle DB using PHP, to view in EXTJS Grouping grid

I have a PHP parser using PHPExcel that reads in a Excel file and stores the contents into an Oracle database.
The problem is that the parser reads every line and does not set up any distinction between headers of rows and the data contained within those rows. When the info is read from the database it is read in a flat file listing and is not easy to navigate.
I am currently reading the data into an EXTJS Grid. I would like to be able to read the Excel, store it in the DB, then pull it out and view it in a new EXTJS GroupingGrid, where the group would be the 'header' for each worksheet in the Excel file.
Has anyone ever used PHPExcel or know how to use PHPExcel to read the Excel file and output the header (1,1) in each worksheet, so that I can store it in the database and pull it out and show it in the JSON so the groupingGrid will give me the ability to have a plus sign for each header so that I can click the plus sign and view all the contents under that header within the grid?
Hy, you can use this php-excell-reader http://code.google.com/p/php-excel-reader/ . I'm using it and works perfect. You can manipulate every cell of every row.
Count the cells
Count the rows
Make a loop to that count and manipulate data (you can add +1 if you want to skip
first row or first cell.
// Include the PHPExcel library
require_once './Classes/PHPExcel.php';
// Load the Excel File
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("myExcelFile.xls");
// Loop through every worksheet
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
// Read the value at Cell A1 in the current worksheet
$cellValue = $worksheet->getCell('A1')->getValue();
// Do whatever you want with $cellValue
// Store it in a database
// Whatever...
}
of course, you might want a bit of error handling in there as well, in case myExcelFile.xls doesn't exist, or you don't have read permissions, or isn't really an Excel file, but just wrap it in a try/catch block and handle as you wish.
You can also set up a read filter if you only want to load the first line of each worksheet rather than every single row and column:
// Include the PHPExcel library
require_once './Classes/PHPExcel.php';
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
class firstRowFilter implements PHPExcel_Reader_IReadFilter
{
public function readCell($column, $row, $worksheetName = '') {
// Only read the heading row
return ($row == 1);
}
// Create an instance of our Read Filter
$firstRowFilter = new firstRowFilter();
// Instantiate the correct Reader
$objReader = PHPExcel_IOFactory::createReader('Excel5');
// Tell the Reader only to load cells that match our Read Filter
$objReader->setReadFilter($firstRowFilter)
// Load the Excel File
$objPHPExcel = $objReader->load("myExcelFile.xls");
// Loop through every worksheet
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
// Read the value at Cell A1 in the current worksheet
$cellValue = $worksheet->getCell('A1')->getValue();
// Do whatever you want with $cellValue
// Store it in a database
// Whatever...
}

Excel validation using php

I have an excel sheet that is loaded with a dynamic result set of data.I need to validate the excel sheet before insert into mysql table.Validation such as Is there any duplicate entry,email validation etc.Any idea about how can validate using php.
As easy as :
parse your excel using phpexcel for example
make an array with all entries.
use array_unique to discart duplicates
then validate email fields
How do you parse the xls itself? For me the most simple solution is to parse the whole xls into an array then validate that. You can easily check for duplicates then iterate through with one foreach and validate the remaining.
You can use COM functions under PHP
$excel = new COM("excel.application") or die("Unable to instanciate excel");
//Open your file
$excel->Workbooks->Open("files/test.xls");
$Workbook = $excel->Worksheets(1); //Select the wortksheet
foreach($Workbook = $excel->Worksheets as $Worksheet)
{
//Loop each page in the book
$Worksheet->Activate; //Activate Sheet 1
for($row=0;$row<=$Worksheet->rows;$row++)
{
$row_item = $Worksheet->rows[$row];
//Hmm, i forgot the rest but you can do that ;)
}
}
You will have to read more about it, as i have never used it to open Excel sheets.
http://www.php.net/manual/en/class.com.php

Categories