duancongduo4109 2014-01-22 18:00
浏览 24
已采纳

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

Hello I have the following code

// 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;

It is generating the excel file, but all it outputs is text similar to this

ÐÏࡱá;þÿ

Opening in a text editor reveals no obvious errors and the script returns none either. I'm trying to output this to an Excel 2007 compatible format. Does anyone have any idea why this is happening?

EDIT: May not be relevant but Excel throws an error that the format does not match the extension

  • 写回答

5条回答 默认 最新

  • dongpangbu4016 2014-01-22 21:24
    关注
    // Save Excel 2007 file
    #echo date('H:i:s') . " Write to Excel2007 format
    ";
    $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');
    

    This fixed it and made it work! ob_end_clean was the solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存