【问题标题】:Error "invalid numeric value for data type numeric in...." while exporting database to excel将数据库导出到 excel 时出现错误“数据类型 numeric in.... 的数值无效”
【发布时间】:2020-07-21 18:52:19
【问题描述】:

我使用 phpspreadsheet 将数据导出到 Excel。 这工作正常,但有时它会将错误作为 c:\xamp\htdocs\phpspreadsheet\vendor\phpoffice\spreadsheet\src\phpspeeadsheet\Cell\Cell.php:221 中的数据类型 numeric 的无效数值 我附上的图像是我得到的错误。请帮帮我。

session_start();
//php_spreadsheet_export.php

include 'C:/xampp/htdocs/phpspreadsheet/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;


$connect = new PDO("mysql:host=localhost;dbname=IDC", "root", " ");

$school=$_SESSION['code'];
$name="name";
$query = "SELECT * FROM SCHOOL WHERE School='".$school."' AND Name!='".$name."'";
$statement = $connect->prepare($query);

$statement->execute();

$result = $statement->fetchAll();

if(isset($_POST["export"]))
{
 $file = new Spreadsheet();

 $active_sheet = $file->getActiveSheet();

 $active_sheet->setCellValue('A1', 'Name');
 $active_sheet->setCellValue('B1', 'Phone');
 $active_sheet->setCellValue('C1', 'DOB');
 $active_sheet->setCellValue('D1', 'Father');
 $active_sheet->setCellValue('E1', 'Mother');
 $active_sheet->setCellValue('F1', 'Address');
 $active_sheet->setCellValue('G1', 'Blood');
 $active_sheet->setCellValue('H1', 'Admission');
 $active_sheet->setCellValue('I1', 'Photo link');
 $active_sheet->setCellValue('J1', 'Class');
 $active_sheet->setCellValue('K1', 'Section');
 
 $count = 2;

 foreach($result as $row)
 {
   $active_sheet->setCellValue('A' . $count, $row["Name"]);
   $active_sheet->setCellValue('B' . $count, $row["Phone"]);
   $active_sheet->setCellValue('C' . $count, $row["DOB"]);
   $active_sheet->setCellValue('D' . $count, $row["Father"]);
   $active_sheet->setCellValue('E' . $count, $row["Mother"]);
   $active_sheet->setCellValue('F' . $count, $row["Address"]);
   $active_sheet->setCellValue('G' . $count, $row["Blood"]);
   $active_sheet->setCellValue('H' . $count, $row["Adm_no"]);
   $active_sheet->setCellValue('I' . $count, $row["Photo_link"]);
   $active_sheet->setCellValue('J' . $count, $row["Class"]);
   $active_sheet->setCellValue('K' . $count, $row["Section"]);
   

   $count = $count + 1;
 }

 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($file, $_POST["file_type"]);

 $file_name = time() . '.' . strtolower($_POST["file_type"]);

 $writer->save($file_name);

 header('Content-Type: application/x-www-form-urlencoded');
 header('Content-Transfer-Encoding: Binary');

 header("Content-disposition: attachment; filename=\"".$file_name."\"");

 readfile($file_name);

 unlink($file_name);

 exit;

}

?> ```

This is the code and it works fine for all data except for this error.

【问题讨论】:

    标签: php excel export numeric phpspreadsheet


    【解决方案1】:

    我解决了错误。错误在于,数字末尾有一个空格,并且与值绑定器检测到的数据类型不匹配。所以我将数据类型设置为字符串。 这是更改后的代码,如果它可以帮助遇到类似问题的任何人。

    $active-sheet->setCellValueExplicit( 'B'. $count, $row["phone"],\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
    

    我得到了这个答案 https://phpspreadsheet.readthedocs.io/en/latest/topics/accessing-cells/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2020-06-02
      相关资源
      最近更新 更多