douweng7233 2012-11-05 12:20
浏览 118

使用PHPExcel下载时获取损坏的xls文件

In my current project, I used PHPExcel 1.7.8 for exporting data into excel. As per suggested I successfully configured it in my project and successfully generate xls file and stored it but when I tried to download xls file then I gor corrupted xls file without getting any error message. I have following code for the same.

/** Error reporting */
if (PHP_SAPI == 'cli')
     die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once '../Classes/PHPExcel.php';

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

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

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
         ->setCellValue('A1', 'Hello')
         ->setCellValue('B1', 'world!')
         ->setCellValue('C1', 'Hello')
         ->setCellValue('D1', 'world!');

// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A2', 'Miscellaneous glyphs')
            ->setCellValue('B2', 'This is test text by me');

// 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=\"filename.xls\"");
header("Cache-Control: max-age=0");

// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
$objWriter->save("php://output");
exit;

Thank you.

  • 写回答

1条回答 默认 最新

  • dongzhong8834 2015-07-02 08:16
    关注

    Can you try this? Just before your

    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=\"filename.xls\"");
    header("Cache-Control: max-age=0");
    

    put ob_end_clean();.

    There might be something wrong about your output buffer.

    评论

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决