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 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改