1
<?php                      
if($_POST['export'])
{

require_once (dirname(__FILE__) . '/../Classes/PHPExcel.php');
$qry="select accessed_Menus as menus,phone_number as phone,date_Accessed as timeAxed,sessionid as sessiondetails from access_trails where DATEADD(dd, 0, DATEDIFF(dd, 0 ,date_Accessed)) >= DATEADD(dd, -10, DATEDIFF(dd, 0, {fn NOW()})) order by date_Accessed desc";
$result=mssql_query($qry);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowCount = 1;
while($row =mssql_fetch_assoc($result)){
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['menus']);
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['phone']);
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['timeAxed']);
    $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $row['sessiondetails']);
    $rowCount++;
}
$objPHPExcel->getActiveSheet()->setTitle('Access Logs');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Access logs.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;


?>

大家好.....我使用上面的代码将数据从数据库导出到电子表格......我能够正确检索数据,但它只显示在加载的页面内。我能够从我的本地主机下载一个 .xls 文件。帮助

4

3 回答 3

0

您可以尝试添加这两个标题,但我不确定它是否会有所帮助(我没有测试过):

header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: Binary");
于 2013-09-04T08:56:43.520 回答
0

除了已经指出的内容之外,您还可以尝试将这些添加到标题中:

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

即使提示下载文件似乎不需要它们(我尝试的示例仅与您的代码显示的行相同,并且在 IE 10 和 Chrome 中下载都没有问题)

于 2013-09-04T13:52:22.943 回答
0

php代码

 header("Pragma: public");
 header("Expires: 0");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Content-Type: application/force-download");
 header("Content-Type: application/octet-stream");
 header("Content-Type: application/download");
 header("Content-Disposition: attachment;filename=export.xls");
 header("Content-Transfer-Encoding: binary ");

echo strip_tags($_POST['tableData'],'<table><th><tr><td>');

然后使用下面的javascript导出

 $(function()      
        {
      $("#exportToExcel").click
         (function()                                   
          {
          var data='<table>'+$("#ReportTable").html().replace(/<a\/?[^>]+>/gi, '')+'</table>';
        $('body').prepend("<form method='post' action='php/exporttoexcel.php' style='display:none' id='ReportTableData'><input type='text' name='tableData' value='"+data+"' ></form>");
        $('#ReportTableData').submit().remove();
        return false;
       d}
      );
       }
     );
于 2015-12-01T17:11:37.903 回答