[risolto] Creazione file excel con PHP

  • Creatore Discussione Creatore Discussione Garaux
  • Data di inizio Data di inizio

Garaux

Utente Attivo
24 Feb 2013
50
0
0
Salve, avrei una domanda in merito alla formattazione di un file excel creato in php.

Per farla breve...cliccando un tasto di una pagina web in cui viene visualizzata una tabella con diversi dati, creo un file excel grazie alla funzione sottostante:

PHP:
<?php

function setHeader($excel_file_name)//this function used to set the header variable
	{
		
		header("Content-type: application/octet-stream charset=utf-8");//A MIME attachment with the content type "application/octet-stream" is a binary file.
		//Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor. 
		header("Content-Disposition: attachment; filename=$excel_file_name");//with this extension of file name you tell what kind of file it is.
		header("Pragma: no-cache");//Prevent Caching
		
		header("Expires: 0");//Expires and 0 mean that the browser will not cache the page on your hard drive
 	
	
	}
        
       if(isset($_POST['submit']))
       { 
        $body = $_POST['body'];
		$catName = $_POST['catName'];
        setHeader($catName.'.xls');
		//LINGUE ASIATICHE E CARATTERI SPECIALI
        echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
        echo $body;
        
       }

?>

Ho notato dei problemi di visualizzazione.
L'accento non viene mostrato come dovrebbe.
Nella tabella la parola Baha Boucles d'Oreilles viene visualizzata nel file excel cosi:
Baha Boucles d\'Oreilles con una slash prima dell'accento.

Sapreste dirmi come risolvere il problema?

Grazie!
 
Ciao, ho risolto facendo cosi:

PHP:
//Testo intero
$text = $body; 

//Apostrofo da sostituire
$oldWord = "\'"; 

//Apostrofo senza slash
$newWord = "'"; 

//Attivo la funzione str_replace
$body = str_replace($oldWord , $newWord , $text); 

//Mostro il contenuto nel file excel
echo $body;

Spero sia la strada giusta.
 

Discussioni simili