fputcsv显示前导零

fputcsv显示前导零

本文介绍了fputcsv显示前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php脚本从结果集查询生成excel CSV文件.一切正常,但是当我读取excel文件时,无法显示前导零.

I'm using a php script to generate an excel CSV file from a result-set query.All works fine but when i read my excel file, I can not display leading zeros.

这是我的代码:

$rows = $this->Query($sql);

$filename = "/www/zendsvr/htdocs/Project/public/report.xls";
$realPath = realpath( $filename );

$filename = realpath( $filename );
$handle = fopen( $filename, "w" );
$finalData = array();

for( $i = 0; $i < count( $rows ); $i++ ) {
    $finalData[] = array( utf8_decode( $rows[$i]->CODE ) );
}

foreach ( $finalData AS $finalRow ) {
    fputcsv( $handle, $finalRow, "\t" );
}

fclose( $handle );

如果我将var_dump()设置为$ finalData [],则可以看到正确的值,例如'000198','000199','000200',但我的xls文件中的值是198,199,200

if i make a var_dump() of $finalData[] i see the correct value, for exmaple '000198', '000199', '000200' but tha same value in my xls file is 198,199,200

dysplay如何在xls文件中也领先零?谢谢

how can dysplay leading zero also in xls file?thanks

推荐答案

您的数据已正确保存,这是Excel试图变得聪明的原因.

Your data is saved correctly, it is Excel who is trying to be smart.

使用Excel导入数据向导,而不是直接打开文件(如有必要,将.csv文件重命名为.txt).在导入向导上,将列的数据类型选择为文本"而不​​是常规":

Use the Excel import data wizard instead of opening the file directly (rename the .csv file to .txt if necessary). On the import wizard, choose the data type of the column as "text" instead of "general":

这篇关于fputcsv显示前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:52