<?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;
?>
大家好.....我正在使用上面的代码将数据从数据库导出到电子表格....我能够正确地提取数据,但它只显示在加载的页面内。我可以从我的localhost下载.xls文件。帮助
答案 0 :(得分:0)
您可以尝试添加这两个标题,但我不确定它会有所帮助(我没有测试过):
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: Binary");
答案 1 :(得分:0)
除了已经指出的内容之外,您还可以尝试将这些内容添加到标题中:
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
即使提示下载文件似乎不需要它们(我尝试的示例只有您的代码显示的相同行,并且在IE 10和Chrome中都没有出现问题)
答案 2 :(得分:0)
Php Code
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}
);
}
);