function exportExcel($data, $filename) { import('Vendor.phpexcel.Classes.PHPExcel', "", ".php"); //创建PHPExcel对象 $objExcel = new PHPExcel(); //获得当前活动sheet的操作对象 $objSheet = $objExcel->getActiveSheet(); //给当前活动sheet重命名 //$objSheet->setTitle("order_excel"); //直接加载数据块来填充数据 $objSheet->fromArray($data); //按照指定格式生成excel文件 $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5'); $time = date("Ymd"); //输出到浏览器 browser_export($filename . "_" . $time . '.xls'); $objWriter->save("php://output"); }
$objStyleFont->getFont()->setSize(16)->setBold(True)->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_GREEN));//加一些样式 $objRichText->createText(",课程丰富多样"); $objSheet->getCell("F4")->setValue($objRichText);//将文字块插入sheet里 /**代码结束**/ /**添加批注代码开始**/ $objSheet->mergeCells("F4:N4");//合并单元格 $objSheet->getComment("F4")->getText()->createTextRun("Van:\r\n慕课网\n\n时尚时尚最时尚");//添加批注 /**代码结束**/ /**超链接代码开始**/ $objSheet->setCellValue("I3","慕课网");//添加文字 $objSheet->getStyle("I3")->getFont()->setSize(16)->setBold(true)->setUnderline(true)->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLUE));//添加样式 $objSheet->getCell("I3")->getHyperlink()->setUrl("http://www.imooc.com");//给文字加上链接地址 /**代码结束**/ $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');//生成excel文件 //$objWriter->save($dir."/export_1.xls");//保存文件 browser_export('Excel2007','browser_excel03.xlsx');//输出到浏览器 $objWriter->save("php://output"); function browser_export($type,$filename){ if($type=="Excel5"){ header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件 }else{ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件 } header('Content-Disposition: attachment;filename="'.$filename.'"');//告诉浏览器将输出文件的名称 header('Cache-Control: max-age=0');//禁止缓存 } ?>
//设置当前sheet的名称 $data = $db->getDataByGrade($i); //查询每个年级的学生数据 $objSheet->setCellValue('A1', '姓名')->setCellValue('B1', '分数')->setCellValue('C1', '班级'); //填充数据 $j = 2; foreach ($data as $key => $val) { $objSheet->setCellValue('A' . $j, $val['username'])->setCellValue('B' . $j, $val['score'])->setCellValue('C' . $j, $val['class'] . '班'); // $j++; } } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //按照指定格式生成excel文件 //$objWriter->save($dir.'/file/demo3.xlsx'); //保存文件 browser_export('Excel2007', 'demo4.xlsx'); //输出到浏览器 $objWriter->save("php://output"); function browser_export($type, $filename) { if ($type == "Excel5") { header('Content-Type: application/vnd.ms-excel'); //告诉浏览器将要输出excel03文件 } else { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //告诉浏览器数据excel07文件 } header('Content-Disposition: attachment;filename="' . $filename . '"'); //告诉浏览器将输出文件的名称 header('Cache-Control: max-age=0'); //禁止缓存
$data = $db->getDataByGrade($i); //查询每个年级的学生数据 //填充数据 $objSheet->setCellValue("A1", "姓名")->setCellValue("B1", "分数")->setCellValue("C1", "班级"); $j = 2; foreach ($data as $key => $val) { $objSheet->setCellValue("A" . $j, $val['username'])->setCellValue("B" . $j, $val['score'])->setCellValue("C" . $j, $val['class'] . "班"); $j++; } } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5"); //生成excel文件 //保存excel至指定目录 //$objWriter -> save($dir."/esport_1.xls"); //输出到浏览器 browser_export("Excel6", "browser_excel03.xls"); $objWriter->save("php://output"); //输出至浏览器函数 function browser_export($type, $filename) { if ($type == "Excel5") { //输出03文件 //ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲 ob_end_clean(); header('Content-Type: application/vnd.ms-excel'); } else { //输出07文件 ob_end_clean(); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); } //告诉浏览器将输出文件的名称
} } else { echo "no resource"; } $data = $selectRes; $objSheet->setCellValue("A1", $taskName); $objSheet->setCellValue("A2", "姓名")->setCellValue("B2", "工号")->setCellValue("C2", "部门")->setCellValue("D2", "微信账号")->setCellValue("E2", "性别")->setCellValue("F2", "状态")->setCellValue("G2", "签到状态")->setCellValue("H2", "签到时间")->setCellValue("I2", "抽奖情况"); $startLine = 3; foreach ($data as $key => $val) { $objSheet->setCellValue("A" . $startLine, $val['name'])->setCellValue("B" . $startLine, $val['huaweiId'])->setCellValue("C" . $startLine, $val['department'])->setCellValue("D" . $startLine, $val['weixinId'])->setCellValue("E" . $startLine, $val['gender'])->setCellValue("F" . $startLine, $val['status'])->setCellValue("G" . $startLine, $val['regisStatus'])->setCellValue("H" . $startLine, $val['regisTime'])->setCellValue("I" . $startLine, $val['lottery']); $startLine++; } //执行输出下载操作 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //生成Excel文件 browser_export('Excel2007', $taskName . '.xls'); //输出到浏览器 $objWriter->save("php://output"); function browser_export($type, $filename) { if ($type == "Excel2007") { ob_end_clean(); //清除缓冲区,避免乱码 header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //告诉浏览器输出excel07文件 } else { ob_end_clean(); //清除缓冲区,避免乱码 header('Content-Type:application/vnd.ms-excel'); //告诉浏览器将要输出excel03文件 }
if($i>1){ $objPHPExcel->createSheet();//创建新的内置表 } $objPHPExcel->setActiveSheetIndex($i-1);//把新创建的sheet设定为当前活动sheet $objSheet=$objPHPExcel->getActiveSheet();//获取当前活动sheet $objSheet->setTitle($i."年级");//给当前活动sheet起个名称 $data=$db->getDataByGrade($i);//查询每个年级的学生数据 $objSheet->setCellValue("A1","姓名")->setCellValue("B1","分数")->setCellValue("C1","班级");//填充数据 $j=2; foreach($data as $key=>$val){ $objSheet->setCellValue("A".$j,$val['username'])->setCellValue("B".$j,$val['score'])->setCellValue("C".$j,$val['class']."班"); $j++; } } $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');//生成excel文件 //$objWriter->save($dir."/export_1.xls");//保存文件 browser_export('Excel5','browser_excel03.xls');//输出到浏览器 $objWriter->save("php://output"); function browser_export($type,$filename){ if($type=="Excel5"){ header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件 }else{ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件 } header('Content-Disposition: attachment;filename="'.$filename.'"');//告诉浏览器将输出文件的名称 header('Cache-Control: max-age=0');//禁止缓存 } ?>
$legend, $areas, true, false, null, $ytitle );//生成一个图标 $chart->setTopLeftPosition("A7")->setBottomRightPosition("K25");//给定图表所在表格中的位置 $objSheet->addChart($chart);//将chart添加到表格中 $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');//生成excel文件 $objWriter->setIncludeCharts(true); //$objWriter->save($dir."/export_1.xls");//保存文件 browser_export('Excel2007','browser_chart.xlsx');//输出到浏览器 $objWriter->save("php://output"); function browser_export($type,$filename){ if($type=="Excel5"){ header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件 }else{ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件 } header('Content-Disposition: attachment;filename="'.$filename.'"');//告诉浏览器将输出文件的名称 header('Cache-Control: max-age=0');//禁止缓存 } ?>
$in = getCells($i); // echo $in."$j"; // echo $v.' '; $objSheet->setCellValue($in . "{$j}", $v); $i++; } // echo "<br/>"; $j++; } function getCells($value) { $arr = range('A', 'Z'); return $arr[$value]; } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5"); browser_export('Excel5', '订单.xls'); //输出到浏览器 $objWriter->save("php://output"); function browser_export($type, $filename) { if ($type == "Excel5") { header('Content-Type: application/vnd.ms-excel'); //告诉浏览器将要输出excel03文件 } else { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //告诉浏览器数据excel07文件 } header('Content-Disposition: attachment;filename="' . $filename . '"'); //告诉浏览器将输出文件的名称 header('Cache-Control: max-age=0'); //禁止缓存