一尘不染

如何使用phpexcel读取数据并将其插入数据库?

php

我有一个php应用程序,我想从excel读取数据,将其插入数据库,然后为特定用户生成pdf报告。我进行了很多搜索,但没有具体说明这两种情况。


阅读 303

收藏
2020-05-26

共1个答案

一尘不染

使用PHPExcel库读取Excel文件并将数据传输到数据库中

//  Include PHPExcel_IOFactory
include 'PHPExcel/IOFactory.php';

$inputFileName = './sampleData/example1.xls';

//  Read your Excel workbook
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                    NULL,
                                    TRUE,
                                    FALSE);
    //  Insert row data array into your database of choice here
}

一切都变得非常取决于您的数据库以及如何在其中构造数据

2020-05-26