我正在使用PHPExcel创建Excel工作表,但我遇到了Ajax问题。在Ajax中,当Ajax成功时,我无法生成工作表。在我目前没有Ajax植入表的代码中生成并且每次都要求保存为选项窗口。
没有Ajax的代码(工作):
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');
// Create a new worksheet, after the default sheet
$objPHPExcel->createSheet();
// Add some data to the second sheet, resembling some different data types
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'More data');
// Rename 2nd sheet
$objPHPExcel->getActiveSheet()->setTitle('Second sheet');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file_'.date('Y/m/d H:i:s').'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Ajax代码(不工作):
HTML:
<a href="javascript:void(0);" id="create">Create</a>
jQuery Ajax:
<script type="text/javascript">
$('#create').click(function() {
$.ajax({
type: "POST",
url: "http://localhost/testing/PHPExcel_1.8.0_doc/Examples/create.php",
data: "",
success: function (html) {
}
});
});
</script>
Create.php
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');
// Create a new worksheet, after the default sheet
$objPHPExcel->createSheet();
// Add some data to the second sheet, resembling some different data types
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'More data');
// Rename 2nd sheet
$objPHPExcel->getActiveSheet()->setTitle('Second sheet');
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file_'.date('Y/m/d H:i:s').'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>
获得此回复:
任何想法?为什么工作表没有生成以及为什么保存为窗口没有打开。
感谢。
答案 0 :(得分:1)
如果您使用ajax生成文件,则需要执行以下步骤:
window.location.href='downloadFile.php?filename=abcd.pdf'
答案 1 :(得分:0)
试试这个:
<script type="text/javascript">
$('#create').click(function() {
$.ajax({
url: "",
beforeSend: function() {
location.href = "http://localhost/testing/PHPExcel_1.8.0_doc/Examples/create.php";
},
success: function (html) {
}
});
});
</script>
答案 2 :(得分:0)
试试这个:
PHP代码 - &gt;返回数组
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save("php://output");
$xlsData = ob_get_contents();
ob_end_clean();
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
$response = array(
'status' => true,
'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData),
'log' => $log_mensaje
);
JQUERY CODE
function crear_reporte_turno(id){
var $mensaje = "¿Está seguro de Crear el Reporte del Turno?";
var $form = $('<div></div>');
$form.append('<p>'+$mensaje+'</p>');
BootstrapDialog.show({
title: 'Consulta',
//type: BootstrapDialog.TYPE_DANGER,
size: BootstrapDialog.SIZE_SMALL,
message: $form,
autospin: true,
buttons: [{
label: 'Continuar',
cssClass: 'btn-primary',
autospin: true,
action: function(dialogRef){
dialogRef.enableButtons(false);
dialogRef.setClosable(false);
url = '<?php echo $url_ruta_controlador . 'crear_reporte_turno/'; ?>' + id;
$.post(url,{'id_turno' : id}, function(respuesta){
dialogRef.close();
var titulo = "Reporte Turno";
var mensaje = "";
var tipo_mensaje = "";
var $form = $('<div></div>');
if(respuesta.status){
mensaje = "Se ha creado el reporte del turno correctamente.";
tipo_mensaje = BootstrapDialog.TYPE_SUCCESS;
var $a = $("<a>");
$a.attr("href",respuesta.file);
$form.append('<p>'+mensaje+'</p>');
$form.append($a);
var nombre_archivo = "reporte_turno_" + id + ".xls";
$a.attr("download",nombre_archivo);
$a[0].click();
$a.remove();
console.log(respuesta.log);
} else {
mensaje = "Se ha producido un error al crear el reporte del turno.";
tipo_mensaje = BootstrapDialog.TYPE_DANGER;
$form.append('<p>'+mensaje+'</p>');
}
BootstrapDialog.show({
title: titulo,
type: tipo_mensaje,
message: $form,
buttons: [{
label: 'Cerrar',
action: function(dialogRef){
dialogRef.close();
}
}]
});
},'json').fail(function(){
dialogRef.close();
var titulo = "Reporte Turno";
var mensaje = "";
var tipo_mensaje = "";
var $form = $('<div></div>');
mensaje = "Se ha producido un error al crear el reporte del turno.";
tipo_mensaje = BootstrapDialog.TYPE_DANGER;
$form.append('<p>'+mensaje+'</p>');
BootstrapDialog.show({
title: titulo,
type: tipo_mensaje,
message: $form,
buttons: [{
label: 'Cerrar',
action: function(dialogRef){
dialogRef.close();
}
}]
});
});
return;
}
},
{
label: 'Cancelar',
action: function(dialogRef){
dialogRef.close();
}
}]
});
}