Home >Backend Development >PHP Tutorial >php输出excel格式的有关问题

php输出excel格式的有关问题

WBOY
WBOYOriginal
2016-06-13 10:43:07913browse

php输出excel格式的问题

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php @$con = mysql_connect("localhost","root","mysql");mysql_query('set names gb2312');if (!$con){    die("数据库链接失败:".mysql_error());    exit;}$data = mysql_select_db("berecord",$con);//include('conn.php');//mysql_query('set names gb2312');if(!$data){    echo mysql_error();    mysql_close($con); //关闭数据库链接    exit;}else if($name=="" && $carnumber=="" && $do=="" &&  $usercode==""){    //当四项数据全为空时$sql = "select * from record;  $result = mysql_query($sql,$con);$fp = fopen("php://output","a");$head = array("年","月","日","周","姓名","年龄","班级","学号","上/下学时间","学习记录","体育成绩","学习汇报","学习总结")";foreach($head as $i => $v){    $head[$i] = iconv('utf-8','gb2312',$v);    //格式转换    //$head[$i] = mb_convert_encoding("$v","utf-8","gb2312");}fputcsv($fp,$head);while($row = mysql_fetch_array($result,MYSQL_ASSOC)){    //fputcsv($fp,array_map('zero_change',$row));    fputcsv($fp,$row);    //echo "<br>"; 没有必要 在$row输出时就实现了}mysql_close($con);}?>


我现在是这样导出csv文件的,这里导出的话是13列,但是每列的宽度都是一样的,这里我想将每列设成我想要的宽度,应该怎么调整?
还有就是学好的问题,比如我在数据库中的学号是0015132的话,mysql里的整理是utf8_general_ci,导出到csv的话,就把前面的0都给去掉了,要如何解决,谢谢

------解决方案--------------------
csv文件,不含排版信息。所以要“每列设成我想要的宽度”是不大可能的。
或许你可用空格来充填宽度

把前面的0都给去掉了 的是 excel,他把纯数字串解释成了数值
通常可在前面加单撇号
------解决方案--------------------
不要用csv格式,用Excel的XML格式

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls;");
?>



100 This is an example
50 Do you understand?

------解决方案--------------------
给学号td加样式:****** 可解决少0问题
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