怎样一键导出excel
public function input() {
ob_end_clean(); //清除缓冲区,避免乱码
header("Content-Typ:text/html;charset=utf-8");
vendor('Excel.PHPExcel.Writer.IWriter');
vendor('Excel.PHPExcel.Writer.Excel5');
vendor('Excel.PHPExcel');
vendor('Excel.PHPExcel.IOFactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setCellValue('A1',"ID"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('B1',"Studentname"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('C1',"IDcard"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('D1',"StudentID"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('E1',"ClassID"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('F1',"Sex"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('G1',"Nation"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('H1',"Birth"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('I1',"Imagepath"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('J1',"Huji"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('K1',"Phone"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('L1',"QQ"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('M1',"Email"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('N1',"Addressnow"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('O1',"Local"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('P1',"Position"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('Q1',"Macrosoft"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('R1',"CoachID"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('S1',"CarID"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('T1',"CartypeID"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('U1',"Introducer"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('V1',"Signdate"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('W1',"Signplace"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('X1',"Signgtype"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('Y1',"Lubiao"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('Z1',"Lubiaodate"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AA1',"Zhika"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AB1',"Zhikadate"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AC1',"Subtabdate"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AD1',"Begindate"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AE1',"Bigtabdate"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AF1', "Weier");//设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AG1',"Lixiao"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AH1',"Fingerprint"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AI1',"Inform"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AJ1',"SchoolID"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AK1',"Setting"); //设置列的值
$objPHPExcel->getActiveSheet()->setCellValue('AL1',"Remarks"); //设置列的值
$sql['Lixiao'] = '否';
$sql['SchoolID'] = "$_SESSION[SchoolID]";
$studentname = I('Studentname');
$idcard = I('IDcard');
if ($studentname != '') {
$sql['Studentname'] ="$studentname";
}
if ($idcard != '') {
$sql['IDcard'] ="$idcard";
};
$sql['_logic'] = 'AND';
$arr = M('studentinfo')->where($where)->select();
$count = count($arr); //求出有多少行;
$i = 2; //注意这是2;因为第一行我们已经设置了表头信息
for ($j = 0; $j < $count; $j++) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A' . $i,$arr[$j]['ID']) //注意这里没有分号结束
->setCellValue('B' . $i,$arr[$j]['Studentname'])
->setCellValue('C' . $i,$arr[$j]['IDcard'])
->setCellValue('D' . $i,$arr[$j]['StudentID'])
->setCellValue('E' . $i,$arr[$j]['ClassID'])
->setCellValue('F' . $i,$arr[$j]['Sex'])
->setCellValue('G' . $i,$arr[$j]['Nation'])
->setCellValue('H' . $i,$arr[$j]['Birth'])
->setCellValue('I' . $i,$arr[$j]['Imagepath'])
->setCellValue('G' . $i,$arr[$j]['Huji'])
->setCellValue('K' . $i,$arr[$j]['Phone'])
->setCellValue('L' . $i,$arr[$j]['QQ'])
->setCellValue('M' . $i,$arr[$j]['Email'])
->setCellValue('N' . $i,$arr[$j]['Addressnow'])
->setCellValue('O' . $i,$arr[$j]['Local'])
->setCellValue('P' . $i,$arr[$j]['Position'])
->setCellValue('Q' . $i,$arr[$j]['Macrosoft'])
->setCellValue('R' . $i,$arr[$j]['CoachID'])
->setCellValue('S' . $i,$arr[$j]['CarID'])
->setCellValue('T' . $i,$arr[$j]['CartypeID'])
->setCellValue('U' . $i,$arr[$j]['Introducer'])
->setCellValue('V' . $i,$arr[$j]['Signdate'])
->setCellValue('W' . $i,$arr[$j]['Signpalce'])
->setCellValue('X' . $i,$arr[$j]['Signgtype'])
->setCellValue('Y' . $i,$arr[$j]['Lubiao'])
->setCellValue('Z' . $i,$arr[$j]['Lubiaodate'])
->setCellValue('AA' .$i, $arr[$j]['Zhika'])
->setCellValue('AB' .$i, $arr[$j]['Zhikadate'])
->setCellValue('AC' .$i, $arr[$j]['Subtabdate'])
->setCellValue('AD' .$i, $arr[$j]['Begindate'])
->setCellValue('AE' .$i, $arr[$j]['Bigtabdate'])
->setCellValue('AF' .$i, $arr[$j]['Weier'])
->setCellValue('AG' .$i, $arr[$j]['Lixiao'])
->setCellValue('AH' . $i,$arr[$j]['Fingerprint'])
->setCellValue('AI' .$i, $arr[$j]['Inform'])
->setCellValue('AJ' .$i, $arr[$j]['SchoolID'])
->setCellValue('AK' .$i, $arr[$j]['Setting'])
->setCellValue('AL' .$i, $arr[$j]['Remarks']);
$i++;
}
$objPHPExcel->getActiveSheet(0)->setTitle('studentinfo');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="studentinfo.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
Html文件中
<div style="float:right"> <formaction="{:U('input')}" > <input type="submit"value="一键导出EXCEL"></form></div>