您好,欢迎访问一九零五行业门户网

使用phpexcel类导出xls文档格式代码示例

使用phpexcel类导出xls文档格式代码示例
为方便,之前使用csv方式来导出xls文档。现在需求改了,要导出待有合并单元格形式展示的文档。这次使用 phpexcel来生成,并设置单元格的格式,如宽度、居中、数字格式等。
基本步骤为:加载phpexcel.php文件,并实例化phpexcel类,写入数据并设置单元格的格式。
效果截图如下:
代码如下:
setactivesheetindex(0);//设置活动sheet$objactsheet = $phpexcel->getactivesheet();//获取当前活动sheet对象$objactsheet->setcellvalue('a1','单号')->mergecells('a1:a2');$objactsheet->setcellvalue('b1','出行人')->mergecells('b1:d1');$objactsheet->setcellvalue('b2','姓名');$objactsheet->setcellvalue('c2','年龄');$objactsheet->setcellvalue('d2','性别');$objactsheet->setcellvalue('e1','时间')->mergecells('e1:e2');$objactsheet->setcellvalue('a3','201512010001');$objactsheet->setcellvalue('b3','小张');$objactsheet->setcellvalue('c3','28');$objactsheet->setcellvalue('d3','男');$objactsheet->setcellvalue('e3','2015-12-01');$objactsheet->setcellvalue('a4','201512010002');$objactsheet->setcellvalue('b4','小妹');$objactsheet->setcellvalue('c4','24');$objactsheet->setcellvalue('d4','女');$objactsheet->setcellvalue('e4','2015-12-02');$objstylea1 = $objactsheet->getstyle('a1');//设置单元格内容的数字格式,原始内容全部显示出来$objstylea1 ->getnumberformat() ->setformatcode(phpexcel_style_numberformat::format_number); //设置水平居中,垂直居中$objstylea1->getalignment()->setvertical(phpexcel_style_alignment::vertical_center);$objstylea1->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);//从指定的单元格复制样式信息.$objactsheet->duplicatestyle($objstylea1, 'a1:e4');//设置单元格的宽度$objactsheet->getcolumndimension('a')->setwidth(25);$objactsheet->getcolumndimension('a')->setwidth(10);$objactsheet->getcolumndimension('a')->setwidth(10);$objactsheet->getcolumndimension('a')->setwidth(10);$objactsheet->getcolumndimension('a')->setwidth(16);header(content-type: application/force-download);header(content-type: application/octet-stream);header(content-type: application/download);header('content-disposition:inline;filename=demo.xls');header(content-transfer-encoding: binary);header(expires: mon, 26 jul 1997 05:00:00 gmt);header(last-modified: . gmdate(d, d m y h:i:s) . gmt);header(cache-control: must-revalidate, post-check=0, pre-check=0);header(pragma: no-cache);$objwriter = phpexcel_iofactory::createwriter($phpexcel, 'excel5');$objwriter->save('php://output');?>
其它类似信息

推荐信息