Search and export to excel with php - 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

Related

data entry into web page from excel

I have an excel file with 3 columns, "First name", "Last name", "email" and about 700 entries (rows). I also have a website with a form for first name, last name and email. Obviously I don't want to type out all 700 entries manually. Is there a way to get these columns from excel and enter them into the online form? Perhaps using visual basic/php or any other alternative. If such code already exists then it would be great if somebody could show me. If not, then it would be appreciated if somebody could give some advice on how to approach this. Thanks!
PHP has a CSV method, you can convert/save your excel file to csv and process if using PHP.
Reading the File in PHP
Example 1
How to create an array from a CSV file using PHP and the fgetcsv function
$file = fopen('youruploadedfile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
// each line of the array
print_r($line);
}
fclose($file);
Example 2
https://www.w3schools.com/php/func_filesystem_fgetcsv.asp
$file = fopen("youruploadedfile.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
check out https://www.w3schools.com/php/func_filesystem_fgetcsv.asp
Definition and Usage
The fgetcsv() function parses a line from an open file, checking for CSV fields.
The fgetcsv() function stops returning on a new line, at the specified length, or at EOF, whichever comes first.
This function returns the CSV fields in an array on success, or FALSE on failure and EOF.
Selenium
Selenium is a tool to automate user interface testing. It helps with testing your application against the browser
Take a look at the web driver https://github.com/facebook/php-webdriver
There is also varied information on how to's on SO.
How to use Selenium with PHP?
Its easier to do this in python/java. Excel is just used to store the list of input values but to actually input it into a webpage using an automated technique you will need to read about webscraping.
Things like BeautifulSoup (a package in python) and Selenium (a java package) can be used for this purpose. You will have to read about them and maybe ask questions to people who know python/java not excel/vba experts.
Here is one link that can get you started
How can I input data into a webpage to scrape the resulting output using Python?
Edit:
I found one link which could help in doing it through VBA but you need to tell us which website only then can someone help you further
Input text into html input box using vba

Read data from xlsx file in php and print as php array

I need a php script which can read the data from an uploaded xlsx file. Also I need to print those data in an array format. Please help.
Save as CSV first and after that - parse it with http://gist.github.com/385876

how to format exported excel sheet with column color in php?

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

PHP Export Excel to specific Path?

Is it possible to export a PHP MySQL Excel sheet to a specified path such as USB Flash Drive.
Its because I'm using php as Point of Sale and all What i want now is once you click on a Button- it will collects records from MySQL database and exports it as excel or csv file to a USB Flash Drive.
I've tried to Google it out, but I can't seem to find anything.
Thank You.
You need to write the file. To write to usb drive you just need to specify correct file path. If you're on *nix it would be like /media/USB/ on linux or /Volumes/USB/ on Mac. For Windows it would be like F:/.
so rough example is like
$a = array('1','2','3'); //This is imaginable row from MySQL
$f = fopen('F:/mycsv.csv', 'w');
fputcsv($f, $a);
fclose($f);
Manual for fputcsv is here.
If needed, you can ask user for a file path in any way supported by your application.
Not a simple process to save it as excel, but relatively straightforward to write a csv.
$temp = tempnam(sys_get_temp_dir(),'tmp');
$handle = fopen($temp,'w');
$query = $pdo->prepare("select * from table");
$query->execute();
$row=$statement->fetch();
$keys=array_keys($row);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=export_'.date('dmY').'.csv');
fputcsv($handle,$keys);
while($row){
fputcsv($handle,$row);
$row=$statement->fetch();}
echo file_get_contents($temp);
fclose($handle);
This creates a temporary file in your temp folder, you write the csv to that and then give it csv headers and echo it out which gives the user a save file box.
I call $row once before the loop to get the table columns with array_keys and output those first and then loop through with a while calling $row=$statement->fetch() at the end of the loop.

PHP import Excel into database (xls & xlsx)

I tried to search for some plugins to import Excel file into MySQL database, one of them is http://code.google.com/p/php-excel-reader/
The tool is so powerful that it displays the entire excel content into html.
However, I think I just need to read the Excel file and extract the contents, for example, into an array, and then write a SQL statement for entering into the database.
Would there be any good codes and packages? Thanks!
This is best plugin with proper documentation and examples
https://github.com/PHPOffice/PHPExcel
Plus point: you can ask for help in its discussion forum and you will get response within a day from the author itself, really impressive.
Sometimes I need to import large xlsx files into database, so I use spreadsheet-reader as it can read file per-row. It is very memory-efficient way to import.
<?php
// If you need to parse XLS files, include php-excel-reader
require('php-excel-reader/excel_reader2.php');
require('SpreadsheetReader.php');
$Reader = new SpreadsheetReader('example.xlsx');
// insert every row just after reading it
foreach ($Reader as $row)
{
$db->insert($row);
}
?>
https://github.com/nuovo/spreadsheet-reader
If you can convert .xls to .csv before processing, you can use the query below to import the csv to the database:
load data local infile 'FILE.CSV' into table TABLENAME fields terminated by ',' enclosed by '"' lines terminated by '\n' (FIELD1,FIELD2,FIELD3)
If you save the excel file as a CSV file then you can import it into a mysql database using tools such as PHPMyAdmin
Im not sure if this would help in your situation, but a csv file either manually or programatically would be a lot easier to parse into a database than an excel file I would have thought.
EDIT: I would however suggest looking at the other answers rather than mine since #diEcho answer seems more appropriate.
I wrote an inherited class:
<?php
class ExcelReader extends Spreadsheet_Excel_Reader {
function GetInArray($sheet=0) {
$result = array();
for($row=1; $row<=$this->rowcount($sheet); $row++) {
for($col=1;$col<=$this->colcount($sheet);$col++) {
if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
$val = $this->val($row,$col,$sheet);
$result[$row][$col] = $val;
}
}
}
return $result;
}
}
?>
So I can do this:
<?php
$data = new ExcelReader("any_excel_file.xls");
print_r($data->GetInArray());
?>
You can use PHPOFFICE as "diEcho" said. And then use bin2hex() function to insert data to database (SQL SERVER or MySQL) as a BLOB data type.
bin2hex() function helped me alot.

Categories