PHPExcel代码中的文件格式不正确

时间:2015-08-14 17:52:57

标签: phpexcel

我正在尝试使用mySQLExlcel数据写入PHPExcel,但我使用PHP在上述PHPExcel代码中获取的文件格式无效。有人可以帮我找出代码中的错误:

$query = "SELECT company_id, userid FROM uam_user";
    $result = mysql_query($query) or die ("Error in Query".mysql_error());
    // Instantiate a new PHPExcel object 
    $objPHPExcel = new PHPExcel();  

    // Set document properties 
        $objPHPExcel->getProperties()->setCreator("Security Team") 
         ->setLastModifiedBy("System") 
         ->setTitle("System") 
         ->setSubject("System Security") 
         ->setDescription("Non Production user Upload Data") 
         ->setKeywords("User Upload") 
         ->setCategory("Security"); 


            // Set the active Excel worksheet to sheet 0 
            $objPHPExcel->setActiveSheetIndex(0); 

                $objPHPExcel->getActiveSheet()->SetCellValue("A1", 'company_id');
         $objPHPExcel->getActiveSheet()->SetCellValue("B2", 'userid');

    //start of printing column names as names of MySQL fields  
    $rowCount = 2;
    //$num_rows = mysql_num_rows($result);
    //echo $num_rows;
    $i = 0;
     while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

        if ($row['userid'] != '')
        {
            //echo "A$rowCount=".$row['company_id']."|B$rowCount=".$row['userid']."<br>";
            $objPHPExcel->getActiveSheet()->SetCellValue("A$rowCount",$row['company_id']);
          $objPHPExcel->getActiveSheet()->SetCellValue("B$rowCount",$row['userid']);
         }
      $rowCount++;
      //echo $rowCount;
    }

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="item_list.xlsx"');
    header('Cache-Control: max-age=0');
    ob_end_clean();
    $objWriter->save ( 'php://output' );

0 个答案:

没有答案