Add heading on export using fputcsv php
Asked Answered
A

2

8

I am trying to export db rows using fputcsv() in csv file. how i can add heading on first, center align then columns then data my code works well without heading. I know there is many api's but is this possible with in my code.

Here is my code:-

                      Enquiry Report

   id         name         class         func
    1          rk           ba            call()
    2          bk           bd            that()

function exportdata_to_excel($details) {
 
  // filename for download 
  $filename = date("Y-m-d").".csv";
  header('Content-Type: text/csv');
  header("Content-Disposition: attachment; filename=\"$filename\""); 
  $out = fopen("php://output", 'w'); 
  
  $flag = false;
 
   //$result = $orderDetails; 
  while($row = mysql_fetch_assoc($details)) {
     $arr =array('Enquiry id'=>$row['id'],'Date'=>$row['created_on'],'Name'=>$row['name'], 'Email'=>$row['email'], 'Telephone'=>$row['telephone'], 'Customer Request'=>$row['customer_request'], 'Special Request'=>$row['special_request']);
    
     if(!$flag) { 
       // display field/column names as first row 
       fputcsv($out, array_keys($arr), ',', '"');
       $flag = true; 
     } 
     
     fputcsv($out, array_values($arr), ',', '"'); 
   } 
   
    fclose($out); 
    exit();
}
Arsenault answered 29/6, 2013 at 8:27 Comment(3)
Am I the only one having trouble understanding what is being asked here?Lermontov
Ah, so you are trying to make it display nice. It is not possible with CSV, this file format is designed to stores data only. If you want to make a nice looking data sheet, you will need to export it into XSLX, and without third-party api it would be very hard.Lermontov
What's your exact question? "Center align" does not sound like CSVHeadline
P
14

This worked for me.

function downloadCSV($data)
{
    $filename = date("Y-m-d").".csv";

    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename=' . $filename);
    header("Content-Transfer-Encoding: UTF-8");

    $f = fopen('php://output', 'a');
    fputcsv($f, array_keys($data[0]));

    foreach ($data as $row) 
    {
        fputcsv($f, $row);
    }
    fclose($f);
}
Persons answered 15/12, 2014 at 11:26 Comment(1)
Can you explain your code a bit? Where's the part to add a heading and center align it?Headline
F
6

Try this after opening the file, you want to write in e.g. i want to write a file at below path:

$fp = fopen('csvfiles/myfile.csv','w')

You have to enter headers separately, so for this make an array of headers like:

$csv_fields=array();

$csv_fields[] = 'Enquiry id';
$csv_fields[] = 'Date';
$csv_fields[] = 'Name';
$csv_fields[] = 'Email';
$csv_fields[] = 'Telephone';
$csv_fields[] = 'Customer Request';
$csv_fields[] = 'Special Request';

After making headers add these headers to the file.

fputcsv($fp, $csv_fields);

while($row = mysql_fetch_assoc($details)) {

     fputcsv($fp,$row); 
   } 
fclose($fp);

Also add following headers on top so that file can be viewed easily:

header("content-type: application/force-download");
header('Content-Type: application/csv');
header('Pragma: no-cache');
Finny answered 9/7, 2013 at 6:22 Comment(2)
Can you explain your code a bit? Where's the part to add a heading and center align it?Headline
Nice and simple to apply, i have to put headers for my fields, thanks !Monnet

© 2022 - 2024 — McMap. All rights reserved.