将过滤后的数据从php保存到excel

时间:2014-02-05 19:34:13

标签: javascript php html mysql phpexcel

我想问一下,在我的代码中搜索帮助,将过滤后的数据保存到excel中...到目前为止,我已成功将php中已过滤的mysql数据保存到excel中,但是我在根据我需要的格式....你能不能帮助我们。

enter image description here

我想在excel外观中保存数据,或者像在index.php页面中那样格式化...有谁知道怎么做?

当前代码:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<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");
echo '<table colspan="2">';
echo '<tr>';
echo '<th>NAME </th>';
echo '<th>SCORE_1 </th>';
echo '<th>SCORE_2 </th>';
echo '<th>OTHER QUALITIES </th>';
echo '<th>INTERVIEW </th>';
echo '<th>TOTAL </th>';
echo '</tr>';

while($row = mysql_fetch_assoc($results)){
    echo '<tr align="center">';
    echo '<td>'.$name = $row['name'].'</td>';
    echo '<td>'.$score1 = $row['score1'].'</td>';
    echo '<td>'.$score2 = $row['score2'].'</td>';
    echo '<td>'.$other_qual = $row['other_qual'].'</td>';
    echo '<td>'.$interview = $row['interview'].'</td>';
    echo '<td>'.$total = $row['total'].'</td>';
}
echo '</tr>';
echo '</table>';

$result = mysql_query("SELECT * FROM score");
if(isset($_POST['send'])){

$col = 0;
while( $rows = mysql_fetch_row($result)){
$row = 0;
foreach ($rows as $value){
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        $row++;
    }
    $col++;
}
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" />
</body>
</head>

1 个答案:

答案 0 :(得分:1)

$row = 1;
while( $rows = mysql_fetch_row($result)){
    $col = 0;
    foreach ($rows as $value){
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
        $col++;
    }
    $row++;
}

$row = 1;
while( $rows = mysql_fetch_row($result)){
   $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
   $row++;
}

修改

并使用

for ($col = 'A'; $col !== 'G'; $col++) {
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
}

设置所有行值后

编辑#2

$headings = array(
    'NAME', 
    'SCORE_1',
    'SCORE_2',
    'OTHER QUALITIES',
    'INTERVIEW',
    'TOTAL'
);
$objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
$row = 2;
while( $rows = mysql_fetch_row($result)){
   $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
   $row++;
}