phpexcel

/* freebies导出 */
public function richang_fbexport(){
ob_clean();
if(isset($_POST['f_year']) && isset($_POST['f_month']) && isset($_POST['export_btn'])){
require_once('include/PHPExcel.php');
$objExcel = new PHPExcel();
$objProps = $objExcel->getProperties();
$objProps->setCreator("TrueBlue");
$objProps->setLastModifiedBy("TrueBlue");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test");
$objActSheet = $objExcel->getActiveSheet();
$objActSheet->setCellValue('A1', 'coupon_code');
$objActSheet->setCellValue('B1', 'freebie金额');
$objActSheet->setCellValue('C1', '邮件');
$objActSheet->setCellValue('D1', '发送时间');
$objActSheet->getColumnDimension('A')->setWidth(20);
$objActSheet->getColumnDimension('B')->setWidth(10);
$objActSheet->getColumnDimension('C')->setWidth(40);
$objActSheet->getColumnDimension('D')->setWidth(25);

$sql = 'select coupon_id,coupon_code,coupon_amount from coupons where coupon_type = "G" ';
$result = mysql_query($sql);
$id_array = array();
$code_array = array();
$amount_array= array();

while($row = mysql_fetch_assoc($result)){
$id_array[] = $row['coupon_id'];
$code_array[] = $row['coupon_code'];
$amount_array[] = $row['coupon_amount'];
}

$id_array_count = count($id_array);

$f_year = $_POST['f_year'];
$f_month = $_POST['f_month'];
$sqle = 'select coupon_id,emailed_to,date_sent from coupon_email_track';
$resulte = mysql_query($sqle);
$ide_array = array();
$email_array= array();
$date_array = array();

while($row = mysql_fetch_assoc($resulte)){
if(date('Y',strtotime($row['date_sent'])) == $f_year && date('n',strtotime($row['date_sent'])) == $f_month){
$ide_array[] = $row['coupon_id'];
$email_array[] = $row['emailed_to'];
$date_array[] = $row['date_sent'];
}
}

$ide_array_count = count($ide_array);

$array_a = array();
$array_b = array();
$array_c = array();
$array_d = array();

for($i = 0;$i < $ide_array_count;$i++){
for($j = 0;$j < $id_array_count;$j++){
if($id_array[$j] == $ide_array[$i]){
$array_a[] = $code_array[$j];
$array_b[] = $amount_array[$j];
$array_c[] = $email_array[$i];
$array_d[] = $date_array[$i];
}
}
}

$array_a_count = count($array_a);

for($i = 0; $i < $array_a_count;$i++){
$objActSheet->setCellValue('A'.($i+2), $array_a[$i]);
$objActSheet->setCellValue('B'.($i+2), $array_b[$i]);
$objActSheet->setCellValue('C'.($i+2), $array_c[$i]);
$objActSheet->setCellValue('D'.($i+2), $array_d[$i]);
}

$objExcel->getActiveSheet()->setTitle('works');
$filename = 'freebi.xls';
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter->save('php://output');

echo '<script>alert("删除成功!");window.location.href="index.php?m=richang&a=richang_freebi"</script>';
}else{
echo '<script>alert("获取信息失败请重新发送!");window.location.href="index.php?m=richang&a=richang_freebi"</script>';
}
}

你可能感兴趣的:(phpexcel)