PHPExcel输出不可读,不会抛出任何错误

时间:2014-01-22 18:00:52

标签: php sql phpexcel

您好我有以下代码

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                         ->setLastModifiedBy("Maarten Balliauw")
                         ->setTitle("Office 2007 XLSX Test Document")
                         ->setSubject("Office 2007 XLSX Test Document")
                         ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                         ->setKeywords("office 2007 openxml php")
                         ->setCategory("Test result file");

    $exchange = $_POST['exchange'];
    $jobchange = $_POST['estimate'];
    $wpchange = $_POST['wp'];

    $username = "----";
    $password = "----";
    $hostname = "----";

    $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
    $selected = mysql_select_db("----", $dbhandle) or die("Could not select examples");

    $query = "SELECT * FROM btsec WHERE WP='$wpchange' AND Exchange='$exchange' AND Estimate='$jobchange'";

    $result = mysql_query($query);

    $acellnum = "3";
    $bcellnum = "3";
    $ccellnum = "3";
    $dcellnum = "3";

$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Section ID')
        ->setCellValue('B1', 'Length')
        ->setCellValue('C1', 'Status')
        ->setCellValue('D1', 'TM');

    while ($row = mysql_fetch_array($result)) {     
    // Query 
    // Add some data
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue("A".$acellnum, $row['SectionID'])
        ->setCellValue("B".$bcellnum, $row['Length'])
        ->setCellValue("C".$ccellnum, $row['Status'])
        ->setCellValue("D".$dcellnum, $row['TM']);

        $acellnum++;
        $bcellnum++;
        $ccellnum++;
        $dcellnum++;
}       

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
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

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

它正在生成excel文件,但它输出的所有文本都与此类似

ÐÏࡱá;þÿ

在文本编辑器中打开显示没有明显错误,脚本也没​​有返回任何错误。我正在尝试将其输出为Excel 2007兼容格式。有谁知道为什么会这样?

编辑:可能不相关,但Excel会抛出格式与扩展名

不匹配的错误

4 个答案:

答案 0 :(得分:5)

// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="sectionlist.xlsx"');
$objWriter->save('php://output');

这解决了它并使它工作! ob_end_clean是解决方案。

答案 1 :(得分:1)

尝试更改此内容:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

到此:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

此外,excel 2007期望该文件为.xlsx,因此您可能应该在头语句中更改该文件。

答案 2 :(得分:0)

我的PhpExcel Library刚刚停止正常工作。 结果excel文件都是用非常奇怪的字符写的。

此:

ob_end_clean();

也是我的解决方案。

答案 3 :(得分:0)

在最近的PHP版本中,PHPExcel停止运行且没有任何错误。在第576行的PHPExcel / Calculations / Functions.php中查找:有一个break命令,PHP不允许。如果删除该行,则一切正常。