phpexcel 上传
getActiveSheet();//获得当前活动sheet的活动对象
$objSheet->setTitle("\*\*\*\*表");//设置当前活动Sheet名称
$objSheet->getDefaultStyle()->getAlignment()->setVertical(PHPExcel\_Style\_Alignment::VERTICAL\_CENTER)->setHorizontal(PHPExcel\_Style\_Alignment::HORIZONTAL\_CENTER);//设置excel文件默认水平垂直方向居中
$cellKey = array(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM',
'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'
);
//手动填充表头信息
$i = 0;
$condition\[\]='缴费类型';
$condition\[\]='缴费人姓名';
$condition\[\]='缴费金额';
$condition\[\]='缴费有效期开始日期';
$condition\[\]='缴费有效期结束日期';
$condition\[\]='提醒';
foreach ($condition as $key => $val) {
$width = strlen($val) < 15 ? 15 : (strlen($val) + 5);
$objPHPExcel->getActiveSheet()->getColumnDimension($cellKey\[$i\])->setWidth($width);
$objSheet->setCellValue($cellKey\[$i++\] . '1', $val);
}
//填充数据
#行数
$js=1;
for ($i = 1; $i < count($data) + 1; $i++) {
$x = 0;
#是否有缴费记录
$cz=0;
foreach ($data\[$i - 1\] as $key => $value) {
//当有物业费记录时 换到下一行填充数据
if($key=='wyf'){
$cz=1;
$js++;
$sum1=0;
foreach ($value as $k =>$v){
$width = strlen($v) < 15 ? 15 : (strlen($v) + 5);
$objPHPExcel->getActiveSheet()->getColumnDimension($cellKey\[$i\])->setWidth($width);
$objSheet->setCellValue($cellKey\[$sum1++\] . $js, $v);
}
//当有停车费记录时 换到下一行填充数据
}else if($key=='tcf'){
$cz=1;
$js++;
$sum1=0;
foreach ($value as $k =>$v){
$width = strlen($v) < 15 ? 15 : (strlen($v) + 5);
$objPHPExcel->getActiveSheet()->getColumnDimension($cellKey\[$i\])->setWidth($width);
$objSheet->setCellValue($cellKey\[$sum1++\] . $js, $v);
}
} else{
if($\_POST\['chooice'\]==1){
if($data\[$i - 1\]\['wyf'\] || $data\[$i - 1\]\['tcf'\]){
$js++;
$width = strlen($value) < 15 ? 15 : (strlen($value) + 5);
$objPHPExcel->getActiveSheet()->getColumnDimension($cellKey\[$i\])->setWidth($width);
$objSheet->setCellValue($cellKey\[$x++\] . $js, $value);
$objSheet->mergeCells($cellKey\[0\] . $js.":".$cellKey\[5\] . $js);//合并A1-E1之间的单元格
}
}else{
$js++;
$width = strlen($value) < 15 ? 15 : (strlen($value) + 5);
$objSheet->setCellValue($cellKey\[$x++\] . $js, $value);
$objSheet->mergeCells($cellKey\[0\] . $js.":".$cellKey\[5\] . $js);//合并A1-E1之间的单元格
}
}
}
}
$objPHPExcel->createSheet(); //创建一个WorkSheet
$objWriter = PHPExcel\_IOFactory::createWriter($objPHPExcel, 'Excel2007');//生成excel文件 如:Excel2007
browser\_export('Excel2007', '物业管理缴费表.xlsx');//输出到浏览器 设置游览器保存文件名
$objWriter->save("php://output");
function browser\_export($type, $filename)
{
if ($type == "Excel5") {
header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
} else {
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
}
header('Content-Disposition: attachment;filename="' . $filename . '"');//告诉浏览器将输出文件的名称
header('Cache-Control: max-age=0');//禁止缓存
}
/\*\*\*
\* @param $fromTime
\* @param $toTime
\* @return string
\* 计算相差时间
\*/
function calcTime($fromTime, $toTime){
//转时间戳
$fromTime = strtotime($fromTime);
$toTime = strtotime($toTime);
//计算时间差
$newTime = $toTime - $fromTime;
return round($newTime / 86400);
}
?>