Leah
Leah

Reputation: 245

create .xls file using php

This is what I have:

   $sep = "\t";        //tabbed character
    $fp = fopen('registrars.xls', "w");

$schema_insert_rows = "";
//printing column names 

        $schema_insert_rows.="#" . $sep;
        $schema_insert_rows.="Registrar" . $sep;
        $schema_insert_rows.="Country" . $sep;
        $schema_insert_rows.="Website" . $sep;
        //$schema_insert_rows.="Postal Address" . $sep;
        //$schema_insert_rows.="Contact Number" . $sep;
        $schema_insert_rows.="Email Address" . $sep;

        $schema_insert_rows.="\n";
        fwrite($fp, $schema_insert_rows);

// printing data:
    $row = 0; $i = 0; $schema_insert_rows = "";

        $schema_insert_rows .= (++$row) . "" . $sep;
        $schema_insert_rows .= $registrarArr[$i] . "" . $sep;
        $schema_insert_rows .= $registrarArr[$i+1] . "" . $sep;
        $schema_insert_rows .= $linkArr[$i] . "" . $sep;
        $schema_insert_rows .= (string)(strip_tags($address)) . "" . $sep;
        $schema_insert_rows .= (string)(strip_tags($phone)). "" . $sep;
        $schema_insert_rows .= "email test";

        $schema_insert_rows .= "\n";



fwrite($fp, $schema_insert_rows);

fclose($fp);

The other column works fine but the ones i commented out are not working [for postal address and email address].. I i get rid of the comments, the data will go to the next row though it should be on the same row as the others.. I checked via var_dump to see if there's < br> but there's none.. What's wrong here?

Upvotes: 1

Views: 28319

Answers (2)

Reactgular
Reactgular

Reputation: 54771

I've used PHPExcel on a client project with good results. It can generate Microsoft Excel, Open Excel and PDF documents.

You might find the file format CSV limiting.

The newer, but not yet stable project is PhpSpreadsheet. It also supports more formats.

Upvotes: 6

KaeruCT
KaeruCT

Reputation: 1645

It looks like what you're trying to make is a CSV file (comma-separated-values) with tab as a separator.

You can use PHP's function fputscsv function to accomplish the same task.

Code sample (from PHP manual):

$list = array (
    array('#', 'Registrar', 'Country', 'Website', 'Email Address'),
    array('123', '456', '789', 'aaa', 'bbb'),
    array('123', '456', '789', 'aaa', 'bbb'),
);

$fp = fopen('registrars.xls', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields, "\t", '"');
}

fclose($fp);

Upvotes: 0

Related Questions