1,excel的单元格坐标,从A1开始,纵向递增数字的值,横向递增字母,当横向字母变为Z后,下一个字母为AA,然后AB,...,ZZ,...,AAA...
2,纵向坐标简单,根据每条数据的索引值就可以计算得出
3,横向坐标,可以发现是一种类似26进制的字母数字,A如果看作0,那Z表示25。但是这种数字与我们常见的16进制,8进制等又不太一样,因为当Z进位的时候,下一个数不是BA,而是AA。常见的进制中,如十进制9进位,变成10,而不是00;16进制0xF进位,变为0x10,而不是0x00。因此可以参考进制转换的算法,然后变化一下,得出计算excel的横向坐标的方法(10进制转伪26进制):
//AAA转换
public function toAAA($dec)
{
if ($dec < 0) return '';
$y = $dec % 26;
$x = floor($dec / 26);
return self::toAAA($x - 1) . chr($y + 65);
}
复制代码
4,导出Excel通用方法(开发时用的是tp5框架)
//导出xls
public function exportXls($array)
{
set_time_limit(0);
vendor('PHPExcel.PHPExcel');
$titles = $array['titles'];
$fields = $array['fields'];
$list = $array['list'];
$name = $array['name'];
$count = count($titles);
$keys = [];//A=>chr(65)
foreach ($titles as $k => $v) {
$keys[] = $this->toAAA($k);
}
/*$keys打印结果
array(6) {
[0] => string(1) "A"
[1] => string(1) "B"
[2] => string(1) "C"
[3] => string(1) "D"
[4] => string(1) "E"
[5] => string(1) "F"
}*/
$exportTime = '导出日期:'.date('Y/m/d h:i:s',time());
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties()
->setTitle($name)
->setKeywords("excel")
->setCategory("result file");
$objPHPExcel->setActiveSheetIndex(0);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->getStyle("A1:{$keys[$count-1]}1")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->mergeCells("A1:{$keys[$count-1]}1");//A1:F1
$activeSheet->mergeCells("A2:{$keys[$count-1]}2");//A2:F2
$activeSheet->setCellValue('A1', $exportTime);
$activeSheet->setCellValue('A2', $name);
//设置title,样式
foreach ($titles as $key => $title) {
$activeSheet->setCellValue($keys[$key] . '3', $title);
$activeSheet->getColumnDimension($keys[$key])->setWidth(16);
//$activeSheet->getRowDimension(($key + 1))->setRowHeight(18);
}
$i = 4;
foreach ($list as &$item) {
foreach ($keys as $k => $v) {
//$val = isset($item[$fields[$k]]) ? $item[$fields[$k]] . ' ' : ' ';
$val = isset($item[$fields[$k]]) ? $item[$fields[$k]] : ' ';
$activeSheet->setCellValue($v . $i, $val);
}
$i++;
}
//第一行靠右对齐
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
//第二行标题
$objPHPExcel->getActiveSheet()->getStyle('A2')
->getFont()->getColor()
->setARGB(\PHPExcel_Style_Color::COLOR_RED);
$objPHPExcel->getActiveSheet()->getStyle('A2')
->applyFromArray(
array(
'font'=>array(
'size'=>20,
'bold'=>true
),
'alignment' => array(
'horizontal' =>\PHPExcel_Style_Alignment::HORIZONTAL_CENTER
)
)
);
//设置默认全部居中
$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
//第三行父表头格式
$objPHPExcel->getActiveSheet()->getStyle("A3:{$keys[$count-1]}3")->applyFromArray(array('font'=>array('size'=>12,'bold'=>true)));
ob_end_clean(); //清除bom头
ob_start();
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control:must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Type:application/force-download' );
header( 'Content-Type:application/vnd.ms-execl' );
header( 'Content-Type:application/octet-stream' );
header( 'Content-Type:application/download' );
header( 'Content-Disposition:attachment;filename='. $name.'.xlsx' );
header( 'Content-Transfer-Encoding:binary' );
$objWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel2007' );
$objWriter->save( 'php://output' );
exit();
}
复制代码
5,调用导出方法,传入表头,字段,文件名,导出数据数组即可。