PHPExcel不能很好地工作

时间:2017-09-05 12:17:50

标签: php mysql phpexcel

我使用here中的PHPExcel库来创建Excel文件扩展名.xlsx 为此,我使用phpmysql此excel文件的数据来自mysql数据库。

创建代码后,我收到此消息:

  

Excel无法打开文件,因为文件格式或文件扩展名为   无效。验证文件是否已损坏,并确认   文件扩展名匹配文件的格式

为了解决这个问题,我使用:

ob_start(); ob_end_clean();

之后我下载文件并尝试打开excel文件我有这条消息:

  

我们发现您的文件中存在某些内容问题。您想要我们吗?   试着尽可能多地恢复?如果你相信它的来源   工作簿,单击是。

当我点击是时,我有这样的信息:

  

Excel完成了文件级验证和修复。这部分内容   工作簿可能已被修复或丢弃。

我点击关闭excel文件打开但在这个excel文件中我有一些空行和我的数据。

这是我使用PHPExcel

下载excel文件的代码
ob_start();
include("includes/connect.php");
require_once'PHPExcel/Classes/PHPExcel.php';
//Create PHPExcel object
$excel=new PHPExcel();
//selecting active sheet
$excel->setActiveSheetIndex(0);
$row=21;
while($data=mysqli_fetch_object($query)){
$excel->getActiveSheet()->setCellvalue('A'.$row,$data->db_maid)
    ->setCellvalue('B'.$row,$data->db_date) 
    ->setCellvalue('C'.$row,$data->db_client)
    ->setCellvalue('D'.$row,$data->db_esid)
    ->setCellvalue('E'.$row,$data->db_type)    
    ->setCellvalue('F'.$row,$data->db_phone)
    ->setCellvalue('G'.$row,$data->db_mobile) 
    ->setCellvalue('H'.$row,$data->db_contactperson)
    ->setCellvalue('I'.$row,$data->db_competetivecompany)
    ->setCellvalue('J'.$row,$data->db_category)    
    ->setCellvalue('K'.$row,$data->db_process)
    ->setCellvalue('L'.$row,$data->db_status) 
    ->setCellvalue('M'.$row,$data->db_rate) 
    ->setCellvalue('N'.$row,$data->db_doc)
    ->setCellvalue('O'.$row,$data->nextDate)
    ->setCellvalue('P'.$row,$data->db_ndstatus)
    ->setCellvalue('Q'.$row,$data->db_pnote) 
    ->setCellvalue('R'.$row,$data->meetingStatus) 
    ->setCellvalue('S'.$row,$data->db_ncam)    
    ->setCellvalue('T'.$row,$data->medit)
    ->setCellvalue('U'.$row,$data->name);
    //incriment the row
$row++;    
}
//set column width
$excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('K')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('L')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('M')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('N')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('O')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('P')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('Q')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('R')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('S')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('T')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('U')->setWidth(20);
//make table headers
$excel->getActiveSheet()
      ->setCellValue('A1','List Of Marketing')
      ->setCellValue('A3','#')
      ->setCellValue('B3','Date')
      ->setCellValue('C3','Client')
      ->setCellValue('D3','ES')
      ->setCellValue('E3','Type')
      ->setCellValue('F3','Phone')
      ->setCellValue('G3','Mobile')
      ->setCellValue('H3','Contact Person')
      ->setCellValue('I3','Competetive Company')
      ->setCellValue('J3','Categoty')
      ->setCellValue('K3','Process')
      ->setCellValue('L3','Status')
      ->setCellValue('M3','Rate')
      ->setCellValue('N3','Date Of Calling')
      ->setCellValue('O3','Next Date')
      ->setCellValue('P3','Next Date Status')
      ->setCellValue('K3','Phone Note')
      ->setCellValue('R3','Meeting Status')
      ->setCellValue('S3','Next Call After Meeting')
      ->setCellValue('T3','Edit Date')
      ->setCellValue('U3','Staff');
//Margin The title
$excel->getActiveSheet()->mergeCells('A1:U1');
//aligning
$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal('center');
//styling
$excel->getActiveSheet()->getStyle('A1')->applyFromArray(
array(
      'font'=>array(
          'size'=>24,
      )
)
);
$excel->getActiveSheet()->getStyle('A3:U3')->applyFromArray(
array(
   'font'=>array(
       'bold'=>true
   ),
    'borders'=>array(
        'allborders'=>array(
            'style'=>PHPExcel_Style_Border::BORDER_THIN
        )
    )
)
);
//give border to data
$excel->getActiveSheet()->getStyle('A4:U'.($row-1))->applyFromArray(
array(
    'borders'=>array(
        'outline'=>array(
            'style'=>PHPExcel_Style_Border::BORDER_THIN
        ),
        'vertical'=>array(
            'style'=>PHPExcel_Style_Border::BORDER_THIN
        )
    )
)
);
//write the result to a file
$file=PHPExcel_IOFactory::createWriter($excel,'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="marketing.xlsx"');
header('Cache-Control:max-age=0');
ob_end_clean();

//output to php output instead of filename
$file->save('php://output');

我的问题是如何解决这个问题我不希望这条消息出现我也不想在我的Excel中有一个空行ob_end_clean()是第一条消息的真正解决方案,因为我看到了互联网上的教程和他的代码正常工作,而不使用ob_end_clean()ob_start()

2 个答案:

答案 0 :(得分:2)

你反过来做这件事。进入缓冲区所需的唯一部分是$file->save("php://output");。如果出于任何原因,仅在保存到输出后exit对您来说不够,请删除第一行ob_start();和最后一行并尝试:

ob_start();
$file->save("php://output");
$content = ob_get_contents();
ob_end_clean();
die($content);

答案 1 :(得分:0)

尝试删除

break;

在library / PHPExcel / Calculation / Functions.php中(第1161行)

在公共静态函数TYPE($ value = NULL){}中

in elseif(is_array($ value)){}

它解决了我的问题。