Jump to content

Create excel file with PHP


n14charlie

Recommended Posts

Hey

 

I'm having problems creating a simple excel file with PHP,

I followed this guide:

http://www.999tutorials.com/php/create-excel-files-with-php/

But had no luck at creating this simple excel file, I first tried on my web host then on my local host.

 

Here's the code:

<?php
require_once "excel.php";

  $fp = fopen("xlsfile://file.xls", "wb");
if (!is_resource($fp))
{
die("Cannot open ");
}

$data = array(
        0 => array(
            "column1" => "value1",
            "column2" => "value2",
            "column3" => "value3",
            "column4" => "value4",
            "column5" => "value5",
        ),

        1 => array(
            "column1" => "value6",
            "column2" => "value7",
            "column3" => "value8",
            "column4" => "value9",
            "column5" => "value10",
        ),

      
    );


fwrite($fp, $data);
fclose($fp);


?>

 

If anyone can point out where I'm wrong or even give me the simplest of examples of how to do it , I'll really appreciate it..

 

thanks,

 

charlie

Link to comment
https://forums.phpfreaks.com/topic/168443-create-excel-file-with-php/
Share on other sites

when I try to run the script it just doesn't echo anything but doesn't do anything either

 

I also tried this example file both on server and my localhost and still nothing :

 

<?php
/**
* MS-Excel stream handler
* Excel export example
* @author      Ignatius Teo            <[email protected]>
* @copyright   (C)2004 act28.com       <http://act28.com>
* @date        21 Oct 2004
*/
require_once "excel.php";
$export_file = "xlsfile://tmp/example.xls";
$fp = fopen($export_file, "wb");
if (!is_resource($fp))
{
    die("Cannot open $export_file");
}

// typically this will be generated/read from a database table
$assoc = array(
    array("Sales Person" => "Sam Jackson", "Q1" => "$3255", "Q2" => "$3167", "Q3" => 3245, "Q4" => 3943),
    array("Sales Person" => "Jim Brown", "Q1" => "$2580", "Q2" => "$2677", "Q3" => 3225, "Q4" => 3410),
    array("Sales Person" => "John Hancock", "Q1" => "$9367", "Q2" => "$9875", "Q3" => 9544, "Q4" => 10255),
);

fwrite($fp, serialize($assoc));
fclose($fp);
?>

Try adding this to the top of the script.

 

ini_set('display_errors', 1);

error_reporting(E_ALL);

 

The script not doing anything is likely a PHP error, these lines will display the error if any exists when the script is executed.

 

I assume you've created the excel.php file as well in the same file path right?

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.