一鍵匯出excel,匯出excel

來源:互聯網
上載者:User

一鍵匯出excel,匯出excel

怎樣一鍵匯出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>

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.