javascript - 将表字段名从 php 保存到 excel

标签 javascript php html mysql phpexcel

我想在我的代码中寻求帮助,将过滤后的数据保存到Excel中...到目前为止,我已经成功地将PHP中过滤后的MySQL数据保存到Excel中,但我在根据它进行排列时遇到了一些问题我需要的格式......请你们帮帮我吧。

enter image description here

我想将表字段名称放入 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>

最佳答案

$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++;
}

关于javascript - 将表字段名从 php 保存到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21587503/

相关文章:

javascript - 当URL不匹配任何路由时以及当URL包含/#/使用ReactJS时如何显示404

javascript - 从 Chrome 扩展程序到 App Engine 的 POST 请求作为 GET 请求收到

javascript - 单击 anchor 元素后执行操作

php - 我如何获得 LinkedIn 群组成员

c# - 设置页面标题

javascript - 纯 JavaScript 图像抓取缩放?

php - 如何以本地时间格式显示 Unix 时间

php - file_get_contents 是多行吗?

html - 使一个 div 可点击但有另一个 div 出现在它上面

javascript - 在栏外或按钮外单击时隐藏菜单边栏