html按钮不允许我下载我的excel文件

时间:2016-10-04 00:20:04

标签: php html

我想使用html和php下载excel文件。 PHP工作正常...但通过HTML HTML按钮不起作用。我不知道如何解决它... 我试图在Google上搜索,但我找不到解决方案...... 这是我的html源代码

<!DOCTYPE html>
<html>

<head>
    <title>
        phpDownloadTest
    </title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="jquery-ui-1.11.4.custom/jquery-ui.css" />
    <script src="jquery-ui-1.11.4.custom/jquery-1.12.0.js"></script>
    <script src="jquery-ui-1.11.4.custom/jquery-ui.js"></script>
</head>
<script>
    $(function() {
        $('#bt1').click(function() {
            var msg = " ";
            msg += '&sample3=' + $('#sample3').val();


            $.post("test11.php", msg).done(function() {
                var xmlHttp = new XMLHttpRequest();
                xmlHttp.open("POST", "test11.php", true);
                xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlHttp.send(msg);

                xmlHttp.onreadystatechange = function() {
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

                    }
                }
            });

        });

    });
</script>

这是我的php源码

<?php
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="sample.xls"');
    header('Cache-Control: max-age=0');
    require_once 'PHPExcel_1.8.0_doc/Classes/PHPExcel.php';

    $objPHPExcel = new PHPExcel();
    $file = "PhpDownload" . date('Ymd') . ".csv";;
    $objReader = PHPExcel_IOFactory::createReaderForFile($file);
    $objPHPExcel = $objReader->load($file);
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue("B11", "abc")
                ->setCellValue("B12", "test")

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    echo "Download Complete!!";
    exit;
?>

0 个答案:

没有答案