如何让我的代码手动保存在 Excel 中?因为到目前为止,在我的代码中,它将把我过滤后的数据从 mysql 数据库传输到 Excel 文件...我怎样才能手动保存而不在我的程序文件夹中创建 excel 文件,以便在我的数据中生成 excel 报告。
我的代码:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php';
$filename = 'file.xlsx';
mysql_connect("localhost","root","") or die ("cant connect!");
mysql_select_db("test") or die ("cant find database!");
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$results = mysql_query("SELECT * FROM score");
while($row = mysql_fetch_assoc($results)){
}
$result = mysql_query("SELECT * FROM score");
if(isset($_POST['send'])){
$headings = array(
'ID',
'NAME',
'SCORE 1',
'SCORE 2',
'OTHER QUALITIES',
'INTERVIEW',
'TOTAL',
'AIC',
'BATCHCODE',
);
$objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
$row = 2;
while( $rows = mysql_fetch_row($result)){
$objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
$row++;
}
echo 'saved';
header('Location: Index.php');
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename);
?>
<form id="form1" name="form1" method="post" action="" >
<input type="submit" name="send" value="send to excel" id="send" />
</form>
</body>
</html>
我想做的是,如果我单击“发送到 Excel”(按钮)...将会弹出一个 Excel 保存对话框,以便将过滤后的数据保存到 Excel。
最佳答案
<?php
if (!isset($_POST['send']) { ?>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php } else {
require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php';
$filename = 'file.xlsx';
mysql_connect("localhost","root","") or die ("cant connect!");
mysql_select_db("test") or die ("cant find database!");
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$result = mysql_query("SELECT * FROM score");
if(isset($_POST['send'])){
$headings = array(
'ID',
'NAME',
'SCORE 1',
'SCORE 2',
'OTHER QUALITIES',
'INTERVIEW',
'TOTAL',
'AIC',
'BATCHCODE',
);
$objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
$row = 2;
while( $rows = mysql_fetch_row($result)){
$objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
$row++;
}
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
}
if (!isset($_POST['send']) { ?>
<form id="form1" name="form1" method="post" action="" >
<input type="submit" name="send" value="send to excel" id="send" />
</form>
</body>
</html>
<?php }
关于javascript - 如何手动保存数据到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611469/