码迷,mamicode.com
首页 > Web开发 > 详细

PHPExcel导出数据

时间:2015-06-16 16:16:58      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

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;

PHPExcel导出数据

标签:

原文地址:http://www.cnblogs.com/dream-bccb/p/4580937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!