Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Esportare dbMysql in excel

    Salve a tutti, ho la necessità di esportare dei dati da mysql in formato ms-excel; in rete ho trovato e adattato questo script:

    <?
    $filename="sheet.xls";
    header ("Content-Type: application/vnd.ms-excel");
    header ("Content-Disposition: inline; filename=$filename");
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang=it><head>
    <title>Titolo</title></head>
    <body>

    <?php
    $hostname = "localhost";
    $database = "nomedb";
    $username = "root";
    $password= "pass";
    $conn= mysql_pconnect($hostname, $username, $password) or die(mysql_error());
    mysql_select_db($database, $conn);
    $query = "SELECT id_treno, luogo, note FROM treni";
    $clienti = mysql_query($query, $conn) or die(mysql_error());
    $row_clienti = mysql_fetch_assoc($clienti);
    ?>

    <?php
    do {
    $righe.= "\"".$row_clienti['id_treno']."\"\t\"".$row_clienti['luogo']."\"\t\"".$row_clienti['note']."\"\n";
    } while ($row_clienti= mysql_fetch_assoc($clienti));
    ?>

    <?php
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: nomefile.xls");
    ?>

    <?php
    print $righe;
    exit;
    ?>
    </body></html>

    Viene in effetti generato il file xls, ma invece dei dati della query, sulla tabella vengono riportati i seguenti warning:

    Warning: Cannot modify header information - headers already sent by (output started at C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\provaxls.php:11) in C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\provaxls.php on line 30

    Warning: Cannot modify header information - headers already sent by (output started at C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\provaxls.php:11) in C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\provaxls.php on line 31

    Qualcuno sà indicarmi dove sbaglio?
    Grazie a tutti

  2. #2
    Ciao,
    prova ad eliminare le prime righe
    Codice PHP:
    <?
    header 
    ("Content-Type: application/vnd.ms-excel");
    header ("Content-Disposition: inline; filename=$filename");
    ?>
    Spero di esserti stato d'aiuto.

    Se non dovesse funzionare, ti dico un altro metodo...
    --Cosimo
    MAIL: cosimo.g18@gmail.com

  3. #3
    ho fatto la modifica:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang=it><head>
    <title>Titolo</title></head>
    <body>
    <?php
    $hostname = "localhost";
    $database = "portaletreni";
    $username = "root";
    $password= "testpass";
    $conn= mysql_pconnect($hostname, $username, $password) or die(mysql_error());
    mysql_select_db($database, $conn);
    $query = "SELECT id_treno, luogo, note FROM treni";
    $clienti = mysql_query($query, $conn) or die(mysql_error());
    $row_clienti = mysql_fetch_assoc($clienti);
    ?>

    <?php
    do {
    $righe.= "\"".$row_clienti['id_treno']."\"\t\"".$row_clienti['luogo']."\"\t\"".$row_clienti['note']."\"\n";
    } while ($row_clienti= mysql_fetch_assoc($clienti));
    ?>

    <?php
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: nomefile.xls");
    ?>

    <?php
    print $righe;
    exit;
    ?>


    </body></html>

    ora però non apre + il xls ma ho gli stessi warning; che mi consigli?

  4. #4
    Originariamente inviato da stuartmcr
    ho fatto la modifica:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang=it><head>
    <title>Titolo</title></head>
    <body>
    <?php
    $hostname = "localhost";
    $database = "portaletreni";
    $username = "root";
    $password= "testpass";
    $conn= mysql_pconnect($hostname, $username, $password) or die(mysql_error());
    mysql_select_db($database, $conn);
    $query = "SELECT id_treno, luogo, note FROM treni";
    $clienti = mysql_query($query, $conn) or die(mysql_error());
    $row_clienti = mysql_fetch_assoc($clienti);
    ?>

    <?php
    do {
    $righe.= "\"".$row_clienti['id_treno']."\"\t\"".$row_clienti['luogo']."\"\t\"".$row_clienti['note']."\"\n";
    } while ($row_clienti= mysql_fetch_assoc($clienti));
    ?>

    <?php
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: nomefile.xls");
    ?>

    <?php
    print $righe;
    exit;
    ?>


    </body></html>

    ora però non apre + il xls ma ho gli stessi warning; che mi consigli?
    Devi spostare in alto le righe di header prima di <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...
    --Cosimo
    MAIL: cosimo.g18@gmail.com

  5. #5
    ho fatto quanto detto, non mi apre il file xls ma mi chiede di aprire/salvare direttamente lo script?!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.