-1

我是 PHP 代码函数,现在我希望该函数会在单击按钮时出现。

这是我的 PHP 代码函数:

function prints()
{
    header('Content-type: application/excel');
    $filename = 'LEGO_PRODUCTION_RESULT_NG.xls';
    header('Content-Disposition: attachment; filename='.$filename);
    $data = "<html xmlns:x='urn:schemas-microsoft-com:office:excel'>
    <head>
        <!--[if gte mso 9]>
        <xml>
            <x:ExcelWorkbook>
                <x:ExcelWorksheets>
                    <x:ExcelWorksheet>
                        <x:Name>Sheet 1</x:Name>
                        <x:WorksheetOptions>
                            <x:Print>
                                <x:ValidPrinterInfo/>
                            </x:Print>
                        </x:WorksheetOptions>
                    </x:ExcelWorksheet>
                </x:ExcelWorksheets>
            </x:ExcelWorkbook>
        </xml>
        <![endif]-->
    </head>";

    echo $data;
}

HTML 按钮:

<input type="button" value="Export to Excel"/>

那么如何添加 JS 函数以在单击按钮上调用 PHP 函数呢?

4

1 回答 1

0

因为您正在输出带有附件配置的文件,所以您可以简单地链接到 PHP 脚本:

<input type="button" value="Export to Excel" onclick="window.location='print.php'" />

print.php必须调用该prints()函数,仅此而已。

单击时,浏览器不会重定向页面,它将保持当前页面不变,并仅显示文件的下载提示。

于 2013-06-10T07:39:34.580 回答