php 导出csv时如何设置属性,合并单元格之类的。。
发布网友
发布时间:2022-04-06 03:46
我来回答
共1个回答
热心网友
时间:2022-04-06 05:15
CSV文件本身就是不带格式符号的纯文字,无法直接在PHP中控制单元格合并这样的事情,我觉得你可以用html代码生成execl能直接读的文档比较好
$result = mysql_query("select * from student order by id asc");
$str = "<tr><td>姓名</td><td>性别</td><td>年龄</td></tr>\n";
$str = iconv('utf-8','gb2312',$str);
while($row=mysql_fetch_array($result)){
$name = iconv('utf-8','gb2312',$row['name']);
$sex = iconv('utf-8','gb2312',$row['sex']);
//要控制合并啥的,在下边修改即可
$str .="<tr><td>".$name."</td><td>".$sex."</td><td>".$row['age']."</td></tr>";
}
$filename = date('Ymd').'.xls';
export_xls($filename,$str);
function export_xls($filename,$string){
//可以修改样式,控制字号、字体、表格线、对齐方式、表格宽度、单元格padding等,在下边的<style></style>
$header="<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\nxmlns=\"http://www.w3.org/TR/REC-html40\">\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html>\n<head>\n<meta http-equiv=\"Content-type\" content=\"text/html;charset=GBK\" />\n<style>\ntd{padding:4px;mso-ignore:padding;color:windowtext;font-size:10.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:Arial;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;mso-protection:locked visible;white-space:nowrap;mso-rotate:0;}\n</style>\n</head><body>\n<table x:str border=0 cellpadding=0 cellspacing=0 width=100% style=\"border-collapse: collapse\">";
$footer="</table>\n</body></html>";
$exportString=$header.$string.$footer;
header("Cache-Control:public");
header("Pragma:public");
header("Content-type: Content-type:application/vnd.ms-excel");
header("Accept-Ranges: bytes");
header("Content-Disposition:attachment; filename=".$filename);
header("Content-length:".strlen($exportString));
echo $exportString;
exit;
}
追问导出时把html代码被当成文本给导出来了啊