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
Related
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.
I am using simplexlsx to read xlsx sheet,
I have multiple tabs in my xlsx file, When I am trying to read data, its shows data only for first tab,
for better understanding i have attach print screen.
Try This,
$this->load->library('simplexlsx');
$xlsx = new SimpleXLSX( $file_path );
$data['csv_data'] = $xlsx->rows();
It is because when you select something from an excel file it performs that action with the active sheet which is by default is the first one.
You need to change that to the required sheet
Use the following code to do that
$objPHPExcel->setActiveSheetIndex($count); // Use no of the sheet you want to select -1 as count
I wanted to append data in the pre formated excel sheet that is basically header footer in the excel sheet I wanted to append the contents. And will create many files dynamically.
A simple workaround is:
create a html table with the formatting you need
add values in php to the table (or generate table with php)
save file as .xls (filled with content from html table)
open file (will show formatted table in Excel)
Reason:
handling XLS files is very complex and many libraries have big limits (only available on windows servers....)
html table saved as .xls can be opened in Excel.
Thanks I have found the way PHPExcel is a good library.
In order to get PHPExcel http://www.codeplex.com/PHPExcel working with CodeIgniter, there are a few steps you must take to ensure compatibility with CodeIgniter's naming standards.
1: Class names must match the file names. PHPExcel has a few files(such as PHPExcel/IOFactory.php) that have names like PHPExcel_IOFactory. Change these names by removing the "PHPExcel_" part. These constructors in these files must be public in order for CI to access them.
$this->load->library('phpexcel');
$this->load->library('PHPExcel/iofactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("title")
->setDescription("description");
// Assign cell values
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'cell value here');
// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("nameoffile.xls");
i have export a excel sheet which will collect data from my customer table (Mysql database) now i want to format it with column color.
here is my code to export excel sheet....
<?php
ob_start();
session_start();
include("include/session.php");
include("common_function.php");
//connect the database
$customer_id=$_GET['pid'];
//Enter the headings of the excel columns
$contents="Sr.,Agent Name,Contact Person,Contact Number,Password,Deal With Customer,References,Time to reach,Package Offered,Mode of Payment,Note,NEAREST CROSS STREET,DATE OF BIRTH\n";
//Mysql query to get records from datanbase
//You can customize the query to filter from particular date and month etc...Which will depends your database structure.
$sql = "SELECT id,agent_id,fname1,mobile_no,password,fname,rentfrom,cheque_no,package_id,monthly_monitoring,final_comment,cross_street,dob1 FROM customer WHERE `id`='$customer_id'";
$user_query = mysql_query($sql);
//While loop to fetch the records
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['id'].",";
$contents.=$row['agent_id'].",";
$contents.=$row['fname1'].",";
$contents.=$row['mobile_no'].",";
$contents.=$row['password'].",";
$contents.=$row['fname'].",";
$contents.=$row['rentfrom'].",";
$contents.=$row['cheque_no'].",";
$contents.=$row['package_id'].",";
$contents.=$row['monthly_monitoring'].",";
$contents.=$row['final_comment'].",";
$contents.=$row['cross_street'].",";
$contents.=$row['dob1']."\n";
}
// remove html and php tags etc.
$contents = strip_tags($contents);
//header to make force download the file
header("Content-Disposition: attachment; filename=Sale_report".date('d-m-Y').".csv");
print $contents;
?>
now i want to color my first row...
Heay Cnik,
Ayyappan Sekar is right.
PHPExcel is a library that can be used to generate xls( and many other formats ) file. Its purpose is to generate a file and not sending it on some id.
What you can do is, first generate a file using PHPExcel library and save it somewhere.
For sending email to some id, you can use http://php.net/manual/en/function.mail.php'>mail function. However, its little complex to send emails with attachments using php's mail function though not very difficult.
Refer the links given below:
1] http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/
2] Attach File Through PHP Mail (refer to the answer of asprin)
3] http://www.texelate.co.uk/blog/send-email-attachment-with-php/
The CSV file is a simple text file, you can't format it with color
http://en.wikipedia.org/wiki/Comma-separated_values
Hi instead of using header to export to excel, U can use PHPExcel library. It provides much easier inbuilt methods to achieve what u want.. even u can apply styles, merge cells etc
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...
}