导出到excel文件,单元格换行符,ALT + ENTER

时间:2014-05-05 17:05:00

标签: php excel export xls

我需要在使用PHP到xls导出的单元格中放置换行符,我尝试过像\ r或\ n这样的字符将文本放到下一个单元格中,而不是仅仅将文本放在具有换行符的同一单元格中。

示例:

hello world

我需要在一行和世界中使用Hello,就像在相同的单元格一样。

1 个答案:

答案 0 :(得分:1)

这是脏代码..但正在工作..希望这能帮到你。

我参考了这些

http://www.bennadel.com/blog/1095-maintaining-line-breaks-in-an-html-excel-file.htm

Export MySQL data to Excel in PHP

<?php
/*******EDIT LINES 3-8*******/
$DB_Server = "localhost"; //MySQL Server    
$DB_Username = "root"; //MySQL Username     
$DB_Password = "";             //MySQL Password     
$DB_DBName = "survey";         //MySQL Database Name  
$DB_TBLName = "results"; //MySQL Table Name   
$filename = "survey results";         //File Name
/*******YOU DO NOT NEED TO EDIT ANYTHING BELOW THIS LINE*******/    
//create MySQL connection   
$sql = "Select username, email, q1, q2, q3, createdat from $DB_TBLName";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database   
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());   
//execute query 
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());    
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");
/*******Start of Formatting for Excel*******/   
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields

echo <<<EOF
    <!--- Store Excel-HTML output. --->
<cfsavecontent variable="strData">

    <table border=".5pt">
    <thead>
        <tr>
EOF;

for ($i = 0; $i < mysql_num_fields($result); $i++) {
    echo "<th>" . mysql_field_name($result,$i) . "</th>";
}

echo <<<EOF
    </thead>
    <tbody>

EOF;

//end of printing column names  
//start while loop to get data
    while($row = mysql_fetch_row($result))
    {
        echo "<tr valign=\"top\">";
        for($j=0; $j<mysql_num_fields($result);$j++)
        {
            echo "<td>";
            if(!isset($row[$j]))
                echo preg_replace("/\r\n|\n\r|\n|\r/", "<br style=\"mso-data-placement:same-cell;\" />", "NULL".$sep);
            elseif ($row[$j] != "")
                echo preg_replace("/\r\n|\n\r|\n|\r/", "<br style=\"mso-data-placement:same-cell;\" />", "$row[$j]".$sep);
            else
                echo preg_replace("/\r\n|\n\r|\n|\r/", "<br style=\"mso-data-placement:same-cell;\" />", "".$sep);
            echo "</td>";
        }
        echo "</tr>";
    }       
echo <<<EOF
    </tbody>
    </table>

</cfsavecontent>


<!--- Set header for file attachment. --->
<cfheader
    name="content-disposition"
    value="attachment; filename=data.xls"
    />

<!--- Stream the content. --->
<cfcontent
    type="application/excel"
    variable="#ToBinary( ToBase64( strData ) )#"
    />
EOF;
?>