在excel文件中导出记录[关闭]

I created a web application and I want to add a feature like user can export selected records...

Check Box   Name    Email

            Sumit   sumit@gmail.com
            Karan   karan@gmail.com

I want to export this records into an excel file, but only the selected records should be exported.

Thanks in advance

There's no need to download external libraries - everything you need for this is already built into PHP.

Steps:

  1. Do query in php to get the rows you want to output
    You can use SELECT * FROM table WHERE id IN (1,2,...)
  2. Use mysql_fetch_array() or mysql_fetch_assoc() to get the rows one at a time
  3. Use fputcsv() to output them to a file ending in csv - this will properly escape your data

http://www.php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/function.fputcsv.php

Excel will be able to read the file.

Override the defaults for fputcsv to use tabs for delimiters and Excel will have an even easier time reading the file. If you use commas (the default) you may need to pick commas as the delimiter on Excel import.

Here's a working example assuming you already have rows set up:

$rows; // predefined
$filename = 'webdata_' . date('Ymd') . '.csv';

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/octet-stream"); 
// that indicates it is binary so the OS won't mess with the filename
// should work for all attachments, not just excel

$out = fopen("php://output", 'w');  // write directly to php output, not to a file
foreach($rows as $row)
{
  fputcsv($out, $row);
}
fclose($out);

You can install and use the very nice PHP exel library to import exel files as well as export data in exel.

More info here : http://phpexcel.codeplex.com/

Otherwise the question itself is a bit vague so that's all I can say. :/

If you send the data as CSV to the browser (i.e. implode() each entry in the result set array with \t and separate each line with ), Excel can easily convert it to something it can use when the document is loaded.

But note, you must tell the browser that the document it's loading is an excel file so it knows to use excel to load the file. Do so by providing a content type and content disposition header like so:

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=exportExcel.xls");

Please make sure you call the header() functions before you send the CSV'd text.

Note: I chose \t (tabs) to separate each field rather than commas because in my experience, they've caused issues as it might be common for a field to contain commas in it... but perhaps not in your particular case.

Assuming that your mysql database is on the same server as your web host, then you should be able to use the "select into outfile" method in mysql. http://dev.mysql.com/doc/refman/5.1/en/select-into.html

You can create a csv file (which opens in excel) using this kind of syntax:

SELECT * FROM people INTO OUTFILE '/www/your/output/directory/output.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '
';

And then you can let people download the file using php like this:

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=/www/your/output/directory/output.csv");
header("Pragma: no-cache");
header("Expires: 0");

this last part coming from other post -> create csv file