javascript - 如何手动保存数据到excel

标签 javascript php html mysql phpexcel

如何让我的代码手动保存在 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/

相关文章:

android - 使用 HTML5 访问 iOS 和 Android 手机中的相机

javascript - 如何暂时禁用javascript中的log4js登录?

javascript - 将多个图像(文件)从一个文件输入分配到另一个文件输入 - 上传

javascript - 如何在AngularJS中全屏显示谷歌地图

php - 如何在mysql数据库连接中使用throw exception

php - CodeIgniter UPDATE 适用于第一次调用,但不适用于连续的 AJAX 调用

javascript - JQuery onclick 覆盖 div

javascript - 使用构造函数的参数值查找对象的名称

javascript - 在 javascript 中 sleep - 没有 setTimeout

php - 我怎样才能使用php中的prepare语句获取insert_id?