0

我使用标头机制通过下载概念将 MySQL 列数据导出到 excel 文件中。问题是,如果一个列textdatatype数据包含breaklines类似enumeration的数据,那么 csv 文件很奇怪:枚举的数据被放置在 excel 文件的不同行中。那么如何让数据放在一个单元格内呢?

这是下载的代码:

function downloadFile($outPut, $fileName){

        $filesize = strlen($outPut);

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header("Content-type: text/enriched");
        header("Content-length: $filesize");
        header("Content-disposition: attachement; filename=$fileName");

        ob_clean();
        flush();

        print($outPut);

        exit();
    }
4

2 回答 2

1

My first suggestion would be to ensure that each of the fields you are exporting are within double quotes if they are not already, this will save a lot of formatting issues e.g. with commas within a field etc. If you are still having problems I would suggest trying to replace the newline characters with something that you will interpret as a new line (or just as space). Try this in your sql (around the field that ,ay have newline characters:

REPLACE(your_column_name,'\n',' ')
于 2013-06-27T10:06:54.163 回答
0

一种肮脏的方法是使用“\r\n”作为以CSV文件结尾的实际行;和一个简单的“\n”作为单元格内的分隔线,确保引用包含分隔线的任何单元格值。

$fh = fopen('test.csv', 'w+');
fwrite($fh, "sep=\t" . "\r\n");
fwrite($fh, 'A' ."\t" . 'B' . "\t" . 'C' . "\r\n");
fwrite($fh, 'D' ."\t" . "\"E\nF\nG\"" . "\t" . 'H' . "\r\n");
fclose($fh);

此示例在单元格之间使用制表符分隔符;并在单元格 B2 中显示三行值

编辑

或使用 fputcsv()

$fh = fopen('test2.csv', 'w+');
fwrite($fh, "sep=\t" . PHP_EOL);
fputcsv($fh, array('A', 'B', 'C'), "\t");
fputcsv($fh, array('D', "E\nF\nG", 'H'), "\t");
fclose($fh);
于 2013-06-27T10:03:39.267 回答