标签:
require_once ‘./class/Excel/PHPExcel.php‘; //将(1,1)转换成"A1"形式 function getCoordinate($row, $col){ return PHPExcel_Cell::stringFromColumnIndex($col) . $row; } $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objActiveSheet = $objPHPExcel->getActiveSheet(); //将时间列设置为文本 $objActiveSheet->getStyle(‘B‘)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); $objActiveSheet->getStyle( ‘A1:A3‘)->getFont()->setBold(true); $objActiveSheet->getStyle( ‘A5:F5‘)->getFont()->setBold(true); $objActiveSheet->getColumnDimension(‘A‘)->setWidth(15); $objActiveSheet->getColumnDimension(‘B‘)->setWidth(15); $objActiveSheet->getColumnDimension(‘C‘)->setWidth(15); $objActiveSheet->getColumnDimension(‘D‘)->setWidth(15); $objActiveSheet->getColumnDimension(‘E‘)->setWidth(15); $objActiveSheet->getColumnDimension(‘F‘)->setWidth(15); $objActiveSheet->setTitle("$sheetname"); $objActiveSheet->setCellValue(‘A1‘, $head) ->setCellValue(‘A2‘, $title) ->setCellValue(‘A3‘, $amount); //设置标题 $col = 1; foreach ($fields as $value) { $objActiveSheet->setCellValue(getCoordinate(5,$col), $value); $col++; } //设置数据 $row = 6; foreach ($data as $key => $cells) { for ($i = 0; $i < 6; $i++) { $objActiveSheet->setCellValue(getCoordinate($row, $i), $cells[$i]); } $row++; } header(‘Content-Type: application/vnd.ms-excel‘); header(‘Content-Disposition: attachment;filename="‘. $filename .‘"‘); header(‘Cache-Control: max-age=0‘); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘); $objWriter->save(‘php://output‘); exit;
标签:
原文地址:http://www.cnblogs.com/dream-bccb/p/4580937.html