 |
|
โจทย์มีอยู่ว่า เมื่อกดปุ่ม export excel แล้วจะมี alert ขึ้นมาว่า กำลังโหลดอยู่นะ แล้วพอทำการสร้างไฟล์ excel เสร็จ popup ก็จะปิดไป พร้อม
กับเว็บบราวเซอร์ที่จะขึ้น popup ถามว่าจะเซฟไฟล์ excel นี้ที่ไหน พอจะมีวิธีมั้ยครับ เพราะ ajax ก็ใช้ไม่ได้ ขอแนวหน่อยครับ โค้ดประมาณนี้
ครับ
Code (JavaScript)
<input type="button" id="excel" value="Export excel">
$(document).ready(function(){
$('#excel').one("click", function(){
document.location = "exportexcel.php";
});
});
exportexcel.php
Code (PHP)
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("")
->setLastModifiedBy("")
->setTitle("Report Authen Log By User")
->setSubject("Report Authen Log By User")
->setDescription("Report Authen Log By User")
->setKeywords("Report Authen Log By User")
->setCategory("Report Authen Log By User");
// Add some data
$objPHPExcel->getActiveSheet()->setTitle('Report-1');
$data = $objPHPExcel->setActiveSheetIndex(0);
$data->setCellValue('A1', 'Login Date');
$data->setCellValue('B1', 'Node Name');
$data->setCellValue('C1', 'Node IP');
$data->setCellValue('D1', 'User Name');
$data->setCellValue('E1', 'User IP');
$data->setCellValue('F1', 'Command');
$k = $j = 1;
$n = 0;
$sql = "SELECT * FROM network";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){
++$k;
if(is_integer($n/10000) && $n != 0){
$objPHPExcel->createSheet();
$data = $objPHPExcel->setActiveSheetIndex($j);
$objPHPExcel->getActiveSheet()->setTitle('Report-'.($j+1));
$data->setCellValue('A1', 'Login Date');
$data->setCellValue('B1', 'Node Name');
$data->setCellValue('C1', 'Node IP');
$data->setCellValue('D1', 'User Name');
$data->setCellValue('E1', 'User IP');
$data->setCellValue('F1', 'Command');
$j++;
$k = 2;
$n - 1;
}
$data->setCellValue('A'.$k, (empty($row['login_date']))?'-':$row['login_date']);
$data->setCellValue('B'.$k, (empty($row['node_name']))?'-':$row['node_name']);
$data->setCellValue('C'.$k, (empty($row['node_ip']))?'-':$row['node_ip']);
$data->setCellValue('D'.$k, (empty($row['user_name']))?'-':$row['user_name']);
$data->setCellValue('E'.$k, (empty($row['user_ip']))?'-':$row['user_ip']);
$data->setCellValue('F'.$k, (empty($row['cmd']))?'-':$row['cmd']);
$n++;
}
// Rename worksheet
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
$filename = "Report_user_".date("Y-m-d_H-i",time());
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'".xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
ขอบคุณครับ
Tag : PHP, MySQL, JavaScript, Ajax, jQuery
|
|
 |
 |
 |
 |
Date :
2013-12-10 21:49:47 |
By :
เด็กโง่ |
View :
1107 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |