Create CSV file from PHP

Asked

Viewed 4,811 times

3

I want to create an XSV file from PHP. What I want is to be able to create columns and give background color to an element for example. I have the following:

<?php

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

outputCSV(array(
    array("name 1", "age 1", "city 1"),
    array("name 2", "age 2", "city 2"),
    array("name 3", "age 3", "city 3")
));

function outputCSV($data) {
    $output = fopen("php://output", "w");
    foreach ($data as $row) {
        fputcsv($output, $row); // here you can change delimiter/enclosure
    }
    fclose($output);
}
?>

The file is well generated. What is generated looks like this: inserir a descrição da imagem aqui

And I want it to stay that way (all divided by column and if possible a background color): inserir a descrição da imagem aqui

Is there an API you can use?

  • I tested with libreoffice 4.x the values came in the right columns without the background color. Soen has a list of other libs who manipulate .xls

  • @lost phpexcel would be a good option, but I have to even create the file in CSV

  • Have you tried to remove Enclosure? fputcsv($output, $row, ',', '');

  • @Can Jader explain it better? How he got it wrong

  • @pc_oc look at the comment I put in your reply.

2 answers

2


I ended up doing it this way:

<?php
$table = 'nome_tabela';
$outstr = NULL;

header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=cars-models.csv");

$conn = mysql_connect("localhost", "user", "pass");
mysql_select_db("bd",$conn);

// Query database to get column names  
$result = mysql_query("show columns from $table",$conn);
// Write column names
while($row = mysql_fetch_array($result)){
    $outstr.= $row['Field'].';';
}  
$outstr = substr($outstr, 0, -1)."\n";

// Query database to get data
$result = mysql_query("select * from $table",$conn);
// Write data rows
while ($row = mysql_fetch_assoc($result)) {
    $outstr.= join(';', $row)."\n";
}

echo $outstr;
mysql_close($conn);
?>

That is, I have to put ";" to create a new column instead of having a ",". Now the question is missing of having the line with color.

  • 3

    It is impossible to color CSV files because it is a text file with bounded values without formatting (Comma-separated values). If you want formatting, you have to use a . xls, . xlsx, . ods. That is, not having colors is a file type limitation.

  • Okay, thank you! It has to be in csv format because the client wants it. Regarding the UTF-8 encoding, how can I solve it? Thank you! @gmsantos

  • What problem do you have with UTF-8? I did not find the question in the question.

  • Yeah, I didn’t ask that in the question but I asked now. It’s just that my comic book is coming with special characters and I remembered to ask. Anyway thanks!

  • It can help you: http://answall.com/questions/8235/problema-ao-usar-substr-texto-com-php/8641#8641

0

You only need to set the Delimiter field in the fputcsv function

CSV separates each column through the character ;

In this case you have to define the delimiter of the function as being ";"

fputcsv($output, $row, ";");

The command to execute what you want would look like this:

function outputCSV($data) {
    $output = fopen("php://output", "w");
    foreach ($data as $row) {
        fputcsv($output, $row, ";"); // here you can change delimiter/enclosure
    }
    fclose($output);
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.