使用phpspreadsheet将mysql表数据导出为xlsx格式,无法导入

时间:2019-02-14 14:06:54

标签: php mysql

我使用phpspreadsheet将mysql表数据导出为XLSX格式,但是在导出后,当我导入不导入的XLSX文件时,导出的xlsx文件数据就可以了。如果我将数据复制到另一个XLSX文件中,则可以正常工作。 预先感谢!

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

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

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Program');
$sheet->setCellValue('B1', 'FormalCode');
$sheet->setCellValue('C1', 'Title');

$sql = "select * from class_routine";
$result = $conn->query($sql);
if($result->num_rows > 0){
    $n = 1;
    while($row = $result->fetch_assoc()){
        $rowNum = $n + 1;
        $sheet->setCellValue('A' .$rowNum, $row['Program']);
        $sheet->setCellValue('B' .$rowNum, $row['FormalCode']);
        $sheet->setCellValue('C' .$rowNum, $row['Title']);
$n++;


    }
}

$filename = 'ClassRoutine-template-'.time().'.xlsx';
// Redirect output to a client's web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.

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

此处的完整代码https://pastebin.com/imP2QdLm

在导入页面中导入导出的xlsx文件时给出以下警告:

  

警告:XMLReader :: open():在第959行的C:\ xampp \ htdocs \ corap \ vendor \ SpreadsheetReader_XLSX.php中作为输入提供的空字符串

     

警告:XMLReader :: read():在尝试在第995行的C:\ xampp \ htdocs \ corap \ vendor \ SpreadsheetReader_XLSX.php中读取之​​前,先加载数据

     

警告:XMLReader :: read():在尝试在第995行的C:\ xampp \ htdocs \ corap \ vendor \ SpreadsheetReader_XLSX.php中读取之​​前,先加载数据

0 个答案:

没有答案