Javascript 将表字段名从php保存到excel

Javascript 将表字段名从php保存到excel,javascript,php,html,mysql,phpexcel,Javascript,Php,Html,Mysql,Phpexcel,我想在我的代码中寻求帮助,将过滤后的数据保存到excel中…到目前为止,我已经成功地将php中过滤后的mysql数据保存到excel中,但我在按照我需要的格式排列时遇到了一些问题…你们能帮我吗 我想把表格字段名放在excel文件的第一行…有人能帮我吗 当前代码: <!DOCTYPE html> <html> <head> <title>test</title> </head> <body> <?php r

我想在我的代码中寻求帮助,将过滤后的数据保存到excel中…到目前为止,我已经成功地将php中过滤后的mysql数据保存到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");
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'])){

$row = 1;
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>

测试

这是新代码吗?如果是这样,你真的不应该使用过时的
mysql\u查询接口
$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++;
}