• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Csv File Generate

How to generate dynamically row wise print in Excel(CSV) file from PHP?
0
atulmodi
Asked:
atulmodi
  • 2
1 Solution
 
StingRaYCommented:
0
 
forethoughtCommented:
If you're wanting to be able to offer a link to dynamically generate a CSV file for download, here's what I do:

<?php
 /*
  * if you're using sessions, you'll need this in order for it to work with Internet Explorer
  */
  if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
	session_cache_limiter('public');
  }
  session_start();

   /*
    * grab your dynamic content here, be it from a file or from a database
    */
   $content = /* whatever, but when querying from a database I always have it return as an array so I can loop it */;
   if (count($content) > 0) {
     /*
      * since we're sending header information, this needs to precede any output
      */
     header('Content-type: text/comma-separated-values');
     header('Content-disposition: attachment; filename="YOUR_CSV_FILENAME.csv"');
     $csv = '"Header1", "Header2", "Header3"\015\012';
     foreach ($content as $row) {
       $csv .= '"' . $row['col1'] . '", '"' . $row['col2'] . '", "' . $row['col3'] . '"\015\012';
     }
     echo $csv;
   }
?>

Open in new window


Hope that helps.
0
 
atulmodiAuthor Commented:
It is Not Fully Satisfactory Result but i got some Idea from this.
0
 
atulmodiAuthor Commented:
It's Partially Helpful to me i got some idea from the link you provide.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now