2014-10-10 60 views
0

我尝试获得excel文件并将其覆盖到pdf。我更迭,但我没有看到这些数据,并告诉我不能将数据加载到PDFphpExcel in codeigniter

这里我的代码:

 //load our new PHPExcel library 
    $this->load->library('excel'); 

    $inputFileName = 'nameoffile'; 
    $excel2 = PHPExcel_IOFactory::createReader('Excel5'); 
    $excel2 = $excel2->load($inputFileName.'.xls'); 
    $excel2->setActiveSheetIndex(0); 

    header('Pragma: public'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Content-Type: application/force-download'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Type: application/download'); 
    header("Content-Disposition: attachment;filename=from-template.pdf"); 
    header('Content-Transfer-Encoding: binary'); 

    $objWriter = PHPExcel_IOFactory::createWriter($excel2, 'pdf'); 
    $objWriter->save('php://output'); 
+0

什么是您收到的实际错误消息? – 2014-10-10 18:23:41

+0

致命错误:未找到'PHPExcel_Writer_xls'类 – idan003 2014-10-10 19:25:39

+0

您发布的代码应该会生成该错误;你正在尝试创建一个'pdf' Writer,而不是'xls' Writer – 2014-10-10 20:02:22

回答

1

或许是因为没有一个PHPExcel_Writer_xls,然而,有一个PHPExcel_Writer_Excel5如果您试图编写BIFF格式.xls文件

但是,您的代码显示您正在尝试创建PDF编写器。文件名是“区分大小写”,所以打造一个PDF作家,你需要用大写来指定它

$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'PDF'); 

编辑

不要使用相同的变量为你的读者为您电子表格

$reader = PHPExcel_IOFactory::createReader('Excel5'); 
$spreadsheet = $reader->load($inputFileName . '.xls'); 

header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment;filename="from-template.pdf"'); 
header('Cache-Control: max-age=0'); 
// If you're serving to IE 9, then the following may be needed 
header('Cache-Control: max-age=1'); 

// If you're serving to IE over SSL, then the following may be needed 
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified 
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
header ('Pragma: public'); // HTTP/1.0 

$writer = PHPExcel_IOFactory::createWriter($spreadsheet, 'PDF'); 
$writer->save('php://output'); 
die(); 
+0

如何从excel转换为pdf? – idan003 2014-10-10 20:22:16

+0

您使用'Excel5'阅读器完全加载文件,然后使用'PDF' Writer保存它 – 2014-10-10 22:59:41

+0

致命错误:在F:\ wamp \ www \ project3 \ application \中找不到类'PHPExcel_Writer_xls' third_party \ PHPExcel \ IOFactory.php on line 141 – idan003 2014-10-10 23:22:28

0

这个工作很有代码。所以我如何隐瞒这一点。

 //load our new PHPExcel library 
    $this->load->library('excel'); 

    $inputFileName = 'nameoffile'; 
    $reader = PHPExcel_IOFactory::createReader('Excel5'); 
    $spreadsheet = $reader->load($inputFileName . '.xls'); 

    header('Content-Type: application/xls'); 
    header('Content-Disposition: attachment;filename="from-template.xls"'); 
    header('Cache-Control: max-age=0'); 

//如果您服务到IE 9,那么下面可能需要 报头(“缓存控制:最大年龄= 1”);

//如果您通过SSL服务于IE,则可能需要以下内容: header('Expires:Mon,1997年7月26日05:00:00 GMT'); (过去的日期) header('Last-Modified:'。gmdate('D,d M Y H:i:s')。'GMT'); //总是修改 header('Cache-Control:cache,must-revalidate'); // HTTP/1.1 header('Pragma:public'); // HTTP/1.0

$writer = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel5'); 
    $writer->save('php://output'); 
    die();