使用PhpSpreadsheet将数据数组导出到xlsx文件中的问题

时间:2019-07-19 07:01:19

标签: php phpoffice

项目中的每个人都好,我正在尝试将数据库中的数据导出到xlsx文件,但是我没有得到正确的数据。我已附加了数据图像。enter image description here

我正在使用以下代码。

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    use PhpOffice\PhpSpreadsheet\Writer\Xls;

    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    $exportArray = array();
    $query = mysqli_query($conn, "select * from table");

    if(mysqli_num_rows($query) > 0){

            while ($row = mysqli_fetch_assoc($query)) {

                    $exportArray[$exp]['id'] = $row['id'];
                    $exportArray[$exp]['name'] = $row['name'];
                    $exportArray[$exp]['address'] = $row['address'];

                    $exp++;

            }

    }
    $array = array();

    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();

    $sheet->setCellValue('A1', 'id');
    $sheet->setCellValue('B1', 'name');
    $sheet->setCellValue('C1', 'address');

    $rowCount = 2;
    foreach ($exportArray as $value) {

        $sheet->setCellValue('A' . $rowCount, $value['id']);
        $sheet->setCellValue('B' . $rowCount, $value['name']);
        $sheet->setCellValue('C' . $rowCount, $value['address']);
        $rowCount++;
    }

    $fileName = 'test123.xls';
    $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'. $fileName .'.xlsx"'); 
    header('Cache-Control: max-age=0');
    $writer->save('php://output');
    exit();

当我使用以下代码查看工作表数据时

$sheetData = $sheet->toArray(null, true, true, true);

print_r($sheetData);

我得到正确的输出。一切看起来都很好,但是我不明白,为什么我在工作表中获取格式错误的数据?

2 个答案:

答案 0 :(得分:1)

尝试一下:

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$conn = new mysqli("localhost","root","root","test");
$sql = "SELECT * FROM customers";
$result = $conn->query($sql);
$write_array = array();
$fileName = "excel.xlsx";
$write_array[] = array("id","name","address");
if($result->num_rows > 0) 
{
    while($row = $result->fetch_assoc()) 
    {
        $write_array[] = array($row["id"],$row["name"],$row["address"]);
    }
}
$conn->close();
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->fromArray($write_array,NULL,'A1');
$spreadsheet->getActiveSheet()->setTitle("My Excel");

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$fileName.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

答案 1 :(得分:1)

添加此use PhpOffice\PhpSpreadsheet\IOFactory;

现在使用以下代码以Xlsx格式导出数据

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');