PHPExcel:下载电子表格

时间:2013-01-09 18:32:37

标签: php phpexcel

很长一段时间以来,我一直试图让用户能够使用对话框“保存为”或“打开”创建的电子表格。

这就是我尝试的,我没有错误,只是屏幕上的乱码:

//CREATE SPEADSHEET

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

// 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="file.xls"');

// Write file to the browser
$objWriter->save('php://output');

我非常感谢任何帮助解决这个问题。

2 个答案:

答案 0 :(得分:5)

我刚刚尝试了你的部分代码并且工作正常,如果你只是乱搞,问题实际上是在你生成表的部分,检查那部分是否有任何错误,下面是我用来测试它的代码:

 define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

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

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

    // Set document properties
    $objPHPExcel->getProperties()->setCreator("Lumina")
                         ->setLastModifiedBy("Lumina")
                         ->setTitle("Entry members")
                         ->setSubject("Entry members")
                         ->setDescription("List of members participting.")
                         ->setKeywords("office PHPExcel php")
                         ->setCategory("Entry members");

        // Add some data                 
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', $row_titles['TITLE01'])
        ->setCellValue('B1', $row_titles['TITLE02'])
        ->setCellValue('C1', $row_titles['TITLE03'])
        ->setCellValue('D1', $row_titles['TITLE04'])
        ->setCellValue('E1', $row_titles['TITLE05'])
        ->setCellValue('F1', $row_titles['TITLE06'])
        ->setCellValue('G1', $row_titles['TITLE07'])
        ->setCellValue('H1', $row_titles['TITLE08'])
        ->setCellValue('I1', $row_titles['TITLE09'])
        ->setCellValue('J1', $row_titles['TITLE10'])
        ->setCellValue('K1', $row_titles['TITLE11'])
        ->setCellValue('L1', $row_titles['TITLE12'])
        ->setCellValue('M1', $row_titles['TITLE13'])
        ->setCellValue('N1', $row_titles['TITLE14'])
        ->setCellValue('O1', $row_titles['TITLE15'])
        ->setCellValue('P1', $row_titles['TITLE16'])
        ->setCellValue('Q1', $row_titles['TITLE17'])
        ->setCellValue('R1', $row_titles['TITLE18'])
        ->setCellValue('S1', $row_titles['TITLE19'])
        ->setCellValue('T1', $row_titles['TITLE20'])
        ->setCellValue('U1', $row_titles['TEAM_TX'])
        ->setCellValue('V1', $row_titles['T_SHIRT_SIZE'])
        ->setCellValue('W1', $row_titles['INPUT1_TX'])
        ->setCellValue('X1', $row_titles['INPUT2_TX'])
        ->setCellValue('Y1', $row_titles['INPUT3_TX'])
        ->setCellValue('Z1', $row_titles['INPUT4_TX']);

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

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

        // Save Excel5 file
        //echo date('H:i:s') , " Write to Excel5 format" , EOL;
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save(str_replace('.html', '.xls', __FILE__));

        // 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="file.xls"');

        // Write file to the browser
        $objWriter->save('php://output');

我希望这可以提供任何帮助,因为我也是PHP新手。

答案 1 :(得分:0)

我遇到了同样的问题,我在回复中得到了喋喋不休。并且excel无法打开。 解决方案不是使用表单提交或ajax调用,只使用document.location来转发excel生成器的进程。 例如:

document.location="/php/Controller/YnrAPICall.php?is_string=1&params="+ JSON.stringify(params);