在PHP导出时将Excel单元格格式化为文本

时间:2014-05-30 09:21:20

标签: php mysql excel

当我创建一个excel文件并通过php导出它时,它会转换一个特定的单元格" BATCH"时间格式。我希望所有单元格都是默认文本。我真的可以改变代码吗?

mysql_select_db("Famhouse") or die(mysql_error());
mysql_query("SET NAMES 'LATIN5'");
mysql_query("SET character_set_connection = 'LATIN5'");
mysql_query("SET character_set_client = 'LATIN5'");
mysql_query("SET character_set_results = 'LATIN5'");
$header .="\t\t Batch Export
\t\t ".$_GET['c']." Production Batches \n\t\t\n\n";


$select = "select fname as 'First Name', sname as 'Second Name', tbatch as 'Batch'";


$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );

$fields = mysql_num_fields ( $export );


for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}

while( $row = mysql_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }

    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
    $data = "\n(0) Records Found!\n";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$_GET['dt']."-pbatch.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; //}

0 个答案:

没有答案