dsfdsf23423 2014-02-05 19:34
浏览 58
已采纳

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

i would like to ask and search for help in my code in saving the filtered data into excel...so far i have succes in saving the filtered mysql datas in php into excel but i got some problems in arraging it according to the format i need....can you help me guys please.

enter image description here

I want to make the saved datas in the excel look or formatted just like in the index.php page...can anyone know how to do this??

current code:

<!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条回答 默认 最新

  • dongxia2068 2014-02-05 19:41
    关注
    $row = 1;
    while( $rows = mysql_fetch_row($result)){
        $col = 0;
        foreach ($rows as $value){
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
            $col++;
        }
        $row++;
    }
    

    or

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

    EDIT

    and use

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

    after setting all the row values

    EDIT #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++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)