如何将数据保存为手动优秀

时间:2014-02-06 18:51:33

标签: 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。

1 个答案:

答案 0 :(得分:2)

<?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 }