PHPExcel随笔
一、设置PHPExcel单元格文本格式
1.在设置值的时候显示的指定数据类型 $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->getActiveSheet()->setCellValueExplicit('D1',123456789033,PHPExcel_Cell_DataType::TYPE_STRING); 2.在数字字符串前加一个空格使之成为字符串 $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->getActiveSheet()->setCellValue('D1', ' ' . 123456789033);
二、多sheet
vendor('PHPExcel.PHPExcel'); $obpe = new \PHPExcel(); //设置当前sheet索引,用于后续的内容操作 //一般用在对个Sheet的时候才需要显示调用 //缺省情况下,PHPExcel会自动创建第一个SHEET被设置SheetIndex=0 //设置SHEET $obpe->setactivesheetindex(); $obpe->getActiveSheet()->setTitle('电费'); //处理sheet内容 //写入多行数据 foreach($mulit_arr as $k=>$v){ $k = $k+1; /* @func 设置列 */ $obpe->getactivesheet()->setcellvalue('A'.$k, $v[0]); $obpe->getactivesheet()->setcellvalue('B'.$k, $v[1]); $obpe->getactivesheet()->setcellvalue('C'.$k, $v[2]); } //创建一个新的工作空间(sheet) $obpe->createSheet(); $obpe->setactivesheetindex(1); //写入多行数据 foreach($mulit_arr as $k=>$v){ $k = $k+1; /* @func 设置列 */ $obpe->getactivesheet()->setcellvalue('A'.$k, $v[0]); $obpe->getactivesheet()->setcellvalue('B'.$k, $v[1]); } //写入类容 $obwrite = \PHPExcel_IOFactory::createWriter($obpe, 'Excel5'); //创建一个新的工作空间(sheet) $obpe->createSheet(); $obpe->setactivesheetindex(1); //写入多行数据 foreach($mulit_arr as $k=>$v){ $k = $k+1; /* @func 设置列 */ $obpe->getactivesheet()->setcellvalue('A'.$k, $v[0]); $obpe->getactivesheet()->setcellvalue('B'.$k, $v[1]); $obpe->getactivesheet()->setcellvalue('C'.$k, $v[2]); } //写入类容 $obwrite = \PHPExcel_IOFactory::createWriter($obpe, 'Excel5'); //ob_end_clean(); //保存文件 ob_end_clean();//清楚缓存区,解决乱码问题 header('Content-Type: application/vnd.ms-excel'); $name = time(); header('Content-Disposition: attachment;filename="'.$name.'.xls"'); header('Cache-Control: max-age=0'); $obwrite->save('php://output');