Export csv file to open with Excel

Discussion in 'PHP' started by piropeator, Oct 25, 2017.

  1. #1
    Hi. I have this code, it works.
    $arch = fopen('sales.csv', 'w+');
    fputs($arch, "Code, Name, Amount".PHP_EOL);
    $sql = "SELECT code, name, amount from table";
    $sth = $BD->query($sql);
    $sth->execute();
    if ($sth->rowCount() > 0) {
        while ($fila = $sth->fetch(PDO::FETCH_ASSOC)){
            fputs($arch, implode($fila, ',').PHP_EOL);
            }
        }
    fclose($arch);
    PHP:
    The csv file show this records. There are ok.
    Code, Name, Amount
    178,CAÑAVERAL,45.00
    677,DIVINO NIÑO,30.00
    679,HUACHO,15.00
    2291,D' VANIA,120.00
    2297,SEÑOR DEL MAR,150.00
    
    Code (markup):
    When the csv file is opened with Excel (users using Excel), show this:
    Código     Nombre     Importe
    178    CAÑAVERAL    45
    677    DIVINO NIÑO    30
    679    HUACHO    15
    2291    D' VANIA    120
    2297    SEÑOR DEL MAR    150
    Code (markup):
    How I do to show the corrects characters?
    Thanks.
     
    piropeator, Oct 25, 2017 IP
  2. SoftLink

    SoftLink Active Member

    Messages:
    141
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    The Windows operating system (Windows 7+) has a setting for the 'list separator'.
    In Windows 10:
    To change
    Settings -> Time & Language
    -> Additional date, time & regional setting (at the bottom of the screen)
    -> change date, time or number formats
    -> Additional settings ... (Button)

    The List Separator field is the field that sets this.
     
    SoftLink, Oct 25, 2017 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,875
    Likes Received:
    4,547
    Best Answers:
    123
    Trophy Points:
    665
    #3
    I'd be looking at this line

    fputs($arch, implode($fila, ',').PHP_EOL);
    //and trying this
    fputs($arch, '"'.implode($fila, '","').'"'.PHP_EOL);
    //or this
    fputs($arch, '"'.implode($fila, "\"\t\"").'"'.PHP_EOL);
    PHP:
     
    sarahk, Oct 25, 2017 IP
  4. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #4
    Is it just me or did the language just change while importing?
     
    Einheijar, Nov 2, 2017 IP