在数据导出到excel时数字格式不对,一般分为以下两种情况。
1、excel单元格设置长度不够
解决方法:
//在excel.php文件中
$objActSheet = $objPHPExcel->getActiveSheet();
// 设置 栏目名称
$objActSheet->setCellValue("b1", "卡号");
// 设置列的宽度
$objActSheet->getColumnDimension('b')->setWidth(20);//改变此处设置的长度数值
2、字符被excel理解成数字了,一般就是把那个字段设置成文本 或者想办法加入一些空格一类的。 解决方法:
//添加数据处,主要是把要显示数据以chunk_split()函数处理以下,此函数的具体用法可以自己查看
$objActSheet->setCellValue ( "b$i", chunk_split("123456789 ",4," ") );//当然,如果不想让用户看到数字间有空格,那就把要分割的字段值设大一些,如例子中的4设为大于等于9的即可。
我导出EXcel的主要代码的前面部分:
if(count($data)>40000){
$filename_type='csv';
}else{
$filename_type='xls';
}
header("Content-Type: application/vnd.ms-excel");
Header("Accept-Ranges:bytes");
Header("Content-Disposition:attachment;filename=".$filename.".".$filename_type); //$filename导出的文件名
header("Pragma: no-cache");
header("Expires: 0");
if($filename_type=='xls'){
echo 'xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
';
}
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn