dongxie7683 2016-04-13 17:26
浏览 29
已采纳

如何导出到具有多个工作表的excel文件,其中数据来自使用mysqli的数据库

I am a newbie to oops concept of phpexcel.

I am trying to do something like I have data which I am trying to export based on from date, to date and session like breakfast, lunch, and dinner in multiple sheets.

I have almost reached but unable to iterate the rows.

Any help would great to complete it.

Below is the code that I tried.

<?php

error_reporting(E_ALL);
ini_set("display_errors", "ON");
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
include 'db.php';

$xls_filename = 'Data' . date('d-m-Y') . '.xls'; // Define Excel (.xls) file name
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

$heading = array(
    'id' => 'S.No',
    'date' => 'Date',
    'item_name' => 'Item',
    'qty_in' => 'Quantity (dine in)',
    'qty_out' => 'Quantity (parcel)',
    'price' => 'Total Quantity',
    'total' => 'Total Price',
);

$no_of_cols = count($heading);
$rowNumberH = 1;
$colH = 'A';
$columns = array('0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D', '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H', '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L', '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P', '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T', '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X', '24' => 'Y', '25' => 'Z');

$q = $conn->query("SELECT * FROM main");
if (mysqli_num_rows($q) > 0) {
    foreach ($heading as $h) {
        $objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
        $objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
        $colH++;
    }
    $row = 2;
    while ($row_q = mysqli_fetch_assoc($q)) {
        $i = 0;
        foreach ($row_q as $key => $value) {
            if ($key == 'location')
                continue;
            $objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
            $i++;
        }
    }
}

header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>

The code is printing only one row.

  • 写回答

2条回答 默认 最新

  • doushijiao0679 2016-04-13 17:36
    关注

    Increment $row at the while loop. I hope it will work.

    Edited: I am going to mention your slice of code where I have added $row++.

    I have added $row++ at the end of while loop and it is working for me as I tested it at my computer. I implemented your code by creating table like your table. I also tested different type of table in this code and that worked perfect.

    $q = $conn->query("SELECT * FROM student");
    if (mysqli_num_rows($q) > 0) {
        foreach ($heading as $h) {
            $objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
            $objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
            $colH++;
        }
        $row = 2;
        while ($row_q = mysqli_fetch_assoc($q)) {
            $i = 0;
            foreach ($row_q as $key => $value) {
                if ($key == 'location')
                    continue;
                $objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
                $i++;
            }
            $row++;  // $row is added only in your code.
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器