php的两种导出为excel表方法

<?php
/**
* phpexcel导出excel 在thinkphp项目中,基于thinkphp的一个CRM系统原有的导出功能有bug,如果列多一点就有问题
*
**/
public function phpExcelExport($contractList)
{
C('OUTPUT_ENCODE', false);
import("ORG.PHPExcel.PHPExcel");
$objPHPExcel = new PHPExcel();
$objProps = $objPHPExcel->getProperties();
$objProps->setCreator("5kcrm");
$objProps->setLastModifiedBy("5kcrm");
$objProps->setTitle("5kcrm Contract");
$objProps->setSubject("5kcrm Contract Data");
$objProps->setDescription("5kcrm Contract Data");
$objProps->setKeywords("5kcrm Contract Data");
$objProps->setCategory("5kcrm");
$objPHPExcel->setActiveSheetIndex(0);
$objActSheet = $objPHPExcel->getActiveSheet();
$objActSheet->setTitle('Sheet1');
$ascii = 65;
$cv = '';
//$field_list = M('Fields')->where('model = \'Contract\'')->order('customer_id')->select();
//$field_list = M('contract')->where(array('is_deleted'=>'0'))->order(array('customer_id'=>'desc'))->select();
//$field_list = M('contract')->where(array('is_deleted'=>'0'))->field('content,delete_time',true)->select();
$field_list = M('Fields')->where('model = \'contract\'')->order('order_id')->select();
// print_r($contractList);
//println($field_list);die;
$cv = chr($ascii);
foreach ($field_list as $field) {
//$objActSheet->setCellValue($cv.chr($ascii).'2', $field['number']);
$objActSheet->setCellValue($cv . '2', $field['name']);
$tempCv = '';
$ascii = ord(substr($cv, -1));
$cv = substr($cv, 0, -1);
$ascii++;
while ($ascii > 90) {
$ascii = 65;
$tempCv = chr($ascii) . $tempCv;
if (strlen($cv) > 0) {
$ascii = ord(substr($cv, -1));
$cv = substr($cv, 0, -1);
$ascii++;
}
}
$tempCv = chr($ascii) . $tempCv;
$cv .= $tempCv;
}
$mark_Contract_cv = $cv;

if (is_array($contractList)) {
$list = $contractList;
} else {
$where['owner_role_id'] = array('in', implode(',', getSubRoleId()));
$where['is_deleted'] = 0;
$list = M('Contract')->where($where)->select();
}
$i = 2;
foreach ($list as $k => $v) {
$dateList = M('contract')->where("contract_id = $v[contract_id]")->field('content', true)->find();
$zxzj = getUserByRoleId($dateList['zxzj_role_id']);
$bmzg = getUserByRoleId($dateList['bmzg_role_id']);
$list[$k]['zxzj_name'] = $zxzj['user_name'];
$list[$k]['bmzg_name'] = $bmzg['user_name'];
}
foreach ($list as $k => $v) {
$date = M('contract')->where("contract_id = $v[contract_id]")->field('content', true)->find();
/*$zxzj = getUserByRoleId($date['zxzj_role_id']);
$bmzg = getUserByRoleId($date['bmzg_role_id']);
$list[$k]['zxzj_name'] = $zxzj['user_name'];
$list[$k]['bmzg_name'] = $bmzg['user_name'];*/
//println($list);die;
//print_r($date);die;
//var_dump($v);
if (!empty($date)) {
$v = $v + $date;
}
$i++;
$ascii = 65;
//$cv = 'A';
$cv = chr($ascii);
foreach ($field_list as $field) {
if ($field['form_type'] == 'datetime') {
$objActSheet->setCellValue($cv . $i, date('Y-m-d', $v[$field['field']]));
} elseif ($field['form_type'] == 'number' || $field['form_type'] == 'floatnumber' || $field['form_type'] == 'phone' || $field['form_type'] == 'mobile' || ($field['form_type'] == 'text' && is_numeric($v[$field['field']]))) {
//防止使用科学计数法,在数据前加空格
$objActSheet->setCellValue($cv . $i, ' ' . $v[$field['field']]);
} else {
$objActSheet->setCellValue($cv . $i, $v[$field['field']]);
}

$tempCv = '';
$ascii = ord(substr($cv, -1));
$cv = substr($cv, 0, -1);
$ascii++;
while ($ascii > 90) {
$ascii = 65;
$tempCv = chr($ascii) . $tempCv;
if (strlen($cv) > 0) {
$ascii = ord(substr($cv, -1));
$cv = substr($cv, 0, -1);
$ascii++;
}
}
$tempCv = chr($ascii) . $tempCv;
$cv .= $tempCv;
}
}

$objActSheet->mergeCells('A1:' . $mark_Contract_cv . '1');
//$objActSheet->mergeCells($mark_Contract_cv.chr($mark_Contract_ascii).'1');
$objActSheet->getStyle('A1')->getFont()->getColor()->setARGB('FFFF0000');
$objActSheet->getStyle('A1')->getAlignment()->setWrapText(true);
$objActSheet->getStyle($mark_Contract_cv . '1')->getFont()->getColor()->setARGB('FFFF0000');
$objActSheet->getStyle($mark_Contract_cv . '1')->getAlignment()->setWrapText(true);
$objActSheet->setCellValue('A1', L('CONTRACT_INFO'));
//设置背景色
$objActSheet->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objActSheet->getStyle('A1')->getFill()->getStartColor()->setARGB('F5DEB3');
$objActSheet->getStyle($mark_Contract_cv . '1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objActSheet->getStyle($mark_Contract_cv . '1')->getFill()->getStartColor()->setARGB('FFFFE0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
header("Content-Type: application/vnd.ms-excel;");
header("Content-Disposition:attachment;filename=5kcrm_contract_" . date('Y-m-d', mktime()) . ".xls");
header("Pragma:no-cache");
header("Expires:0");
$objWriter->save('php://output');
}
//直接导出为excel
public function simpleExcelExport(){//todo 简单输出会把模板中内容也输出,没有die掉。
$date = M('Contract')->where(array('is_deleted'=>'0'))->select();
header("Content-Type: application/vnd.ms-excel; charset=gbk");
header("Content-Disposition:filename=xls_region.xls");
echo '<table>';
echo '<tr>';
echo ' <th>合同编号</th>';
echo ' <th>商机</th>';
echo ' <th>合同价格</th>';
echo ' <th>到期时间</th>';
echo ' <th>描述</th>';
echo ' <th>状态</th>';
echo ' <th>合作项目</th>';
echo ' <th>合作成本</th>';
echo ' <th>合作性质</th>';
echo '</tr>';
foreach ($date as $k => $r) {
echo '<tr>';
echo '<th>'.$r['number'].'</th>';
echo '<th>'.$r['business_id'].'</th>';
echo '<th>'.$r['price'].'</th>';
echo '<th>'.$r['due_time'].'</th>';
echo '<th>'.$r['description'].'</th>';
echo '<th>'.$r['status'].'</th>';
echo '<th>'.$r['hzxm'].'</th>';
echo '<th>'.$r['hzcb'].'</th>';
echo '<th>'.$r['hzxz'].'</th>';
echo '</tr>';
}
echo '</table>';
die();// 防止在模板中把其他内容导出来
}

?>

 

posted @ 2015-05-06 17:09  SunsCheung  阅读(307)  评论(0编辑  收藏  举报