PHPExcel 常用方法

查看次数:99,最后更新时间:2020-06-10 15:16

插件开发中涉及到PHPExcel导出常用代码

// 引用phpexcel类
$this->lib('phpexcel');

// 创建对象
$objPHPExcel = new PHPExcel();

// 设置属性
$objPHPExcel->getProperties()->setCreator("1245049149@qq.com")
    ->setLastModifiedBy("1245049149@qq.com")
    ->setTitle("Office 2007 XLSX Test Document")
    ->setSubject("Office 2007 XLSX Test Document")
    ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
    ->setKeywords("office 2007 openxml php")
    ->setCategory("Test result file");

// 字体和样式
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12);      //字体大小
$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getFont()->setBold(false); //第二行是否加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);      //第一行是否加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);         //第一行字体大小
$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);         //第二行字体大小
$objPHPExcel->getActiveSheet()->getStyle('A6')->getFont()->setSize(14);         //第六行字体大小
$objPHPExcel->getActiveSheet()->getStyle('A11')->getFont()->setSize(14);         //第十一行字体大小

// 设置垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

// 设置水平居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A3:G5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A7:G10')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A12:B15')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

//设置左对齐
$objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('A6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('A11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

// 设置行高度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20); //设置默认行高
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);    //第一行行高

//设置单元格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);

// 合并
$objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
$objPHPExcel->getActiveSheet()->mergeCells('A2:G2');
$objPHPExcel->getActiveSheet()->mergeCells('A6:G6');
$objPHPExcel->getActiveSheet()->mergeCells('A11:B11');

// 设置内容
$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', '利润核算统计')
    ->setCellValue('A2', '【用户充值】')
    ->setCellValue('A3', '费率')
    ->setCellValue('B3', '笔数    ')
    ->setCellValue('C3', '充值进账')
    ->setCellValue('D3', '手续费')
    ->setCellValue('E3', '利润')
    ->setCellValue('A4', '1.0%')
    ->setCellValue('B4', $pay_log_list['aa'])
    ->setCellValue('C4', $pay_log_list['bb'])
    ->setCellValue('D4', $pay_log_list['cc'])
    ->setCellValue('E4', $pay_log_list['dd'])
    ->setCellValue('A5', '合计')
    ->setCellValue('B5', $pay_log_list['ee'])
    ->setCellValue('C5', $pay_log_list['ff'])
    ->setCellValue('D5', $pay_log_list['gg'])
    ->setCellValue('E5', $pay_log_list['hh'])
    ->setCellValue('A6', '【用户提现】')
    ->setCellValue('A7', '类别')
    ->setCellValue('B7', '笔数')
    ->setCellValue('C7', '申请提现')
    ->setCellValue('D7', '手续费')
    ->setCellValue('E7', '快钱提现')
    ->setCellValue('F7', '手续费')
    ->setCellValue('G7', '利润')
    ->setCellValue('A8', '收取手续费')
    ->setCellValue('B8', $withdraw_list['ii'])
    ->setCellValue('C8', $withdraw_list['kk'])
    ->setCellValue('D8', $withdraw_list['ll'])
    ->setCellValue('E8', $withdraw_list['mm'])
    ->setCellValue('F8', $withdraw_list['nn'])
    ->setCellValue('G8', $withdraw_list['oo'])
    ->setCellValue('A9', '未收取手续费')
    ->setCellValue('B9', $withdraw_list['pp'])
    ->setCellValue('C9', $withdraw_list['qq'])
    ->setCellValue('D9', $withdraw_list['rr'])
    ->setCellValue('E9', $withdraw_list['ss'])
    ->setCellValue('F9', $withdraw_list['tt'])
    ->setCellValue('G9', $withdraw_list['ww'])
    ->setCellValue('A10', '合计')
    ->setCellValue('B10', $withdraw_list['uu'])
    ->setCellValue('C10', $withdraw_list['vv'])
    ->setCellValue('D10', $withdraw_list['xx'])
    ->setCellValue('E10', $withdraw_list['yy'])
    ->setCellValue('F10', $withdraw_list['zz'])
    ->setCellValue('G10', $withdraw_list['aaa'])
    ->setCellValue('A11', '【利润汇总】')
    ->setCellValue('A12', '充值手续费收')
    ->setCellValue('B12', $all_summary['bbb'])
    ->setCellValue('A13', '提现手续费支')
    ->setCellValue('B13', $all_summary['ccc'])
    ->setCellValue('A14', '快钱提现手续费支')
    ->setCellValue('B14', $all_summary['ddd'])
    ->setCellValue('A15', '利润')
    ->setCellValue('B15', $all_summary['eee']);

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('利润核算表');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// 设置输出
$tableName = '利润核算表【'.$st.'--'.$et.'】';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' .$tableName. '.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;


没有找到答案?

您可以通过留言获取更多帮助。也可以通过社区提交相关问题。

留言 开发者社区
论坛咨询