Jump to content

export php page to a file (excel or pdf)


Recommended Posts

is your output going to be display as webpage?

one of the best ways is do the read/write to file if you only need specific data from the table...

select the data then write it to file...

Super simple instruction here, might just do the trick

http://www.w3schools.com/php/php_file.asp

 

goodluck :D

Well, you take his code snippet:

<?PHP 
   function cleanData($str) { 
      $str = preg_replace("/\t/", "\\t", $str); 
      $str = preg_replace("/\r?\n/", "\\n", $str);
    }
header("Content-Type: text/plain"); 
  $flag = false;
  foreach($data as $row) { 
     if(!$flag) { 
        # display field/column names as first row 
        echo implode("\t", array_keys($row)) . "\r\n"; 
        $flag = true; 
    } 
     array_walk($row, 'cleanData');
   echo implode("\t", array_values($row)) . "\r\n";  
  } 
?>

 

And make some changes:

<?PHP 
    function cleanData($str) { 
      $str = preg_replace("/\t/", "\\t", $str); 
      $str = preg_replace("/\r?\n/", "\\n", $str);
    }
//header("Content-Type: text/plain"); //<-lose the header function.
$filename = 'excel_export.xls';
$output = NULL; //define our holding variable.
  $flag = false;
  foreach($data as $row) { 
     if(!$flag) { 
        # display field/column names as first row 
        $output .= implode("\t", array_keys($row)) . "\r\n"; //set this row to our variable.
        $flag = true; 
    } 
    array_walk($row, 'cleanData');
   $output .= implode("\t", array_values($row)) . "\r\n";  //set this row to our variable.
  } 
file_put_contents($filename,$output);  //write the output to a file;
?>

 

Well, I guess that was on me, as I thought you knew how to populate an array from the database.  Being that I don't know your database structure, I will give you arbitrary code.

 

<?php

include 'database_connection.php';

$sql = "SELECT * FROM table";
$result = mysql_query($sql) or trigger_error($sql . ' has an error.<br/>' . mysql_error());
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
  }
}

if(!is_array($data)) {
  exit('There is no data to create an excel file from');
}

   function cleanData($str) { 
      $str = preg_replace("/\t/", "\\t", $str); 
      $str = preg_replace("/\r?\n/", "\\n", $str);
    }
//header("Content-Type: text/plain"); //<-lose the header function.
$filename = 'excel_export.xls';
$output = NULL; //define our holding variable.
  $flag = false;
  foreach($data as $row) { 
     if(!$flag) { 
        # display field/column names as first row 
        $output .= implode("\t", array_keys($row)) . "\r\n"; //set this row to our variable.
        $flag = true; 
    } 
    array_walk($row, 'cleanData');
   $output .= implode("\t", array_values($row)) . "\r\n";  //set this row to our variable.
  } 
file_put_contents($filename,$output);  //write the output to a file;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.