之前在做个人项目时遇到超长数字无法输出的情况,百度了很多的地方都是让用PHPexcel库来设置,最后找到了解决方案:
在需要按照字符串方式输出的单元格上添加:
style="vnd.ms-excel.numberformat:@"添加上这个就可以让单元格按照字符串方式输出,具体实现远离有待研究,附上PHP原生输出Excel文件代码。
class echoEXcel{
public function Excel($name,$titleArray,$bodyArray){
ob_end_clean();//清除缓冲区,避免乱码
header('Content-type:application/vnd.ms-excel');
header('Content-Disposition:attachment;filename='.$name.'.xls');//文件名
echo ("<style>th,td,tr{text-align:center;}</style>");//设置居中
echo '<table border=1>'
// 输入第一行标题-start
echo '<tr>';
foreach ( $titleArray as $title ){
echo '<th>' . $title . '</th>';//直接输出即可,
}
echo '</tr>';
// 输入第一行标题-end
// 遍历输出内容行
foreach ($bodyArray as $key => $item) {
echo '<tr>';
foreach ($item as $value) {
echo '<td style="vnd.ms-excel.numberformat:@">' . $value . '</td>';
// style="vnd.ms-excel.numberformat:@" <-加入此代码可以使输出的表格转为文本
}
echo '</tr>';
}
echo '</table>';
}
}
$titles = ['序号','装分(万分)','心法','角色名',"账号","密码",'是否双修',"备注"];
$body = [
['1','8','冰心','法外狂徒张三',"asdasdasda","123123123123",'双修',"备注内容"],
['2','6','补天','法外狂徒张三',"asdasdasda","123123123123",'单修',"备注内容"],
['3','7','冰心','法外狂徒张三',"asdasdasda","123123123123",'双修',"备注内容"],
['4','6.8','补天','法外狂徒张三',"asdasdasda","123123123123",'单修',"备注内容"],
['5','7.9','冰心','法外狂徒张三',"asdasdasda","123123123123",'双修',"备注内容"],
['6','8','补天','法外狂徒张三',"asdasdasda","123123123123",'单修',"备注内容"],
['7','9.9','冰心','法外狂徒张三',"asdasdasda","123123123123",'双修',"备注内容"],
['8','8','补天','法外狂徒张三',"asdasdasda","123123123123",'单修',"备注内容"]
];
echoEXcel::Excel("Ceshi",$titles,$body);//调用方法