I have this code to get date from my database und get this as a csv file:
<?php
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
$output = fopen('php://output', 'w');
$sql = 'SELECT * FROM `myTable`';
$result = $db->query( $sql );
while ($zeile = $result->fetch_object()) {
$myData[] = array (
'column_A' => $zeile->value_A,
'column_B' => $zeile->value_B
);
}
foreach ($myData as $data) {
fputcsv($output, $data, ";");
}
?>
This works fine !
But now I need a file, which has more the one sheet.
CSV can't handle this, so I need a xlsx file instead a csv file.
How can I export my data (like above) as a xlsx file with different sheets ?
Thanks !!
Related
I am a beginner student of PHP and i am doing some practice by coding a script to generate a CSV file.
Once that the csv file is generated , the format is correct as I've expected :
See the attached :
But if i add the download option the csv is generated wrongly with all the PHP page (i can see all the code)
Here the picture of the wrong csv
Could you help me to find the issue ?
Here the code :
<?php
include("db.php");
session_start();
$sql = "SELECT isocode FROM test_geo ORDER BY isocode";
$result_csv = mysqli_query($conn, $sql);
//facebook csv creation //
$filename = 'test.csv';
$fp = fopen($filename, 'w');
$headers = ['key', 'country'];
fputcsv($fp, $headers);
foreach ($result_csv as $key => $id) {
$new = array_push($id, "country");
fputcsv($fp, $id);
var_dump($id);
}
fclose($fp);
?>
Try this
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=result_file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "w");
foreach ($new as $row) {
fputcsv($outputt, $row);
}
fclose($outputt);
exit();
I believe you put the header statements at the top of your script, so the download data includes the HTML codes too (like below...)
<!DOCTYPE html><html lang="en"><meta charset=...... >
You may use ob_flush() and set header (at the appropriate position of the code) so that the file will be correctly downloaded as csv when it is generated.
Here is the code:
<?php
session_start();
include("db.php");
$sql = "SELECT isocode FROM test_geo ORDER BY isocode";
$result_csv = mysqli_query($conn, $sql);
$data=array();
while($row = mysqli_fetch_array($result_csv, MYSQLI_ASSOC)) {
$ok=$row["isocode"] . "," . "Country";
array_push($data ,$ok);
}
ob_flush();
$filename="ok1.csv";
$csvData = join("\n", $data);
header('Content-Disposition: attachment; filename="'.$filename.'";');
header('Content-Type: application/csv; charset=UTF-8');
die($csvData);
?>
I want to put csv file under /wp-content/uploads instead of downloading it . Following php code download this csv file
I am using this php code
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=data.csv');
$stream = fopen('php://output', 'w+');
fputcsv($stream, $csv_header);
foreach($arrCSV as $key => $assoc_row){
$numeric_row = array();
foreach ($csv_header as $header_name) {
mb_convert_variables('UTF-8', $assoc_row[$header_name]);
$numeric_row[] = $assoc_row[$header_name];
}
fputcsv($stream, $numeric_row);
I'm successfully getting the desired output in console, but the browser isn't automatically downloading the CSV file.
header('Content-Type: application/csv');
$filename = 'Totals Report for ' . $name . ' All-Time.csv';
header('Content-Disposition: attachment; filename="'.$filename.'";');
$output = fopen('php://output', 'rb');
// output the column headings
fputcsv($output, array('Procedure Name', 'Totals'));
foreach ($rows as $row){
fputcsv($output, $row);
}
fopen($filename);
fclose($output);
A sample wich running in my browser:
$csv= '"Procedure Name", Totals'."\n";
$rows= array(
array('aaa', 'bbb'),
array('123', '456')
);
foreach ($rows as $row){
$csv .= implode(",",$row)."\n";
}
$filename= 'Totals Report for '.$name.' All-Time.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
die($csv);
Bah, this got unruly in the comments. I took it a step further and made it loop through the values too!
// Set headers
header('Content-Type: text/csv');
$filename = 'Totals Report for ' . $name . ' All-Time.csv';
header('Content-Disposition: attachment; filename="'.$filename.'";');
// Column titles
echo 'Procedure Name,Totals' . "\n"; // Don't forget the newlines!
// Data
foreach ($rows as $row) {
foreach ($row as $index => $value) {
echo $value;
if ($index < count($row) - 1) {
echo ',';
}
}
echo "\n";
}
You could also buffer it into a file so you can use fputcsv. That would work almost exactly as your current code, but at the end you'd have echo file_get_contents($the_buffer_file);
The main point is that all you need to do is output your data in CSV format, i.e. separate values by commas and a new line is a new row. It would also be smart to escape the values with quotation marks (not shown in my code).
I am trying to export mysql data into csv file,but the problem is that the csv file shows the name of fields not the fields data in csv file.
Here is my php code
<?php
error_reporting(0);
include"../connection.php";
$output = fopen('php://output', 'w');
$query = "SELECT 'claim_dateclaim','claim_clientname', 'claim_account', 'claim_contract', 'claim_parcelno', 'claim_refno', 'claim_datecollected', 'claim_coladdress', 'claim_colpostcode', 'claim_counsineaddres', 'claim_conpostcode', 'claim_retailvalue', 'claim_claimvalue', 'claim_protitle', 'claim_reason', 'claim_type', 'claim_repparcelno' FROM tbl_claimdetail ";
$rows = mysql_query($query );
fputcsv($output, array('Claim Date Raised','Client Name','Account no','Contract Number','Parcel Number','Company','Claim Ref no','Collection Date', 'Collection Address','Collection Postal code','Consignee Address','Consignee Post Code','Retail value','Claim value','Product','Reason','Type','Replacement no'));
while ($row = mysql_fetch_assoc($rows)) {
fputcsv($output, $row);
}
fclose($output);
header("Content-type: text/csv; charset=utf-8'");
header("Content-Disposition: attachment; filename=discussdesk".date('dmY').".csv");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
And here is my csv file
[![enter image description here][1]][1]
[1]: http://i.stack.imgur.com/9GNh6.jpg
use:
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
fputcsv($fp, array_values($row));
}
associative array will work with array_values
With PHP I'm opening a .csv file, I'm adding som rows in it then I'm downloading the new .csv file.
the code I'm using is:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen("csv.csv","r");
$list = array();
while(! feof($file))
{
$list[] = (fgetcsv($file));
}
fclose($file);
$list[] = array('name5', 'town5');
$list[] = array('name6', 'town6');
$list = array_filter($list);
outputCSV($list);
function outputCSV($list) {
$output = fopen("php://output", "w");
foreach ($list as $row) {
fputcsv($output, $row);
}
fclose($output);
}
My issue is the other PHP code in the page don't generate, and only the .csv file is being download.
for exemple if I'm adding:
echo "test";
it won't be display and only csv.csv will be downloaded
How can I display my "echo test;" ?
With the headers you are telling the browser to download a CSV, there is no where to output your echo.
You could make a page with the information you want to show that has an iframe with the csv code in it.