Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use phpexcel to read data and insert into database?

Tags:

php

phpexcel

I have a php application where I want to read data from excel, Insert into database and then generate pdf reports for specific users. I searched a lot but nothing specific given about both things.

like image 433
coder101 Avatar asked Mar 14 '12 03:03

coder101


People also ask

How do I import Excel data into mysql using PHP?

php use Phppot\DataSource; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; require_once 'DataSource. php'; $db = new DataSource(); $conn = $db->getConnection(); require_once ('./vendor/autoload. php'); if (isset($_POST["import"])) { $allowedFileType = [ 'application/vnd. ms-excel', 'text/xls', 'text/xlsx', 'application/vnd.

What is PHPExcel?

PHPExcel. PHPExcel is a library written in pure PHP and providing a set of classes that allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) . xls, Excel 2007 (OfficeOpenXML) . xlsx, CSV, Libre/OpenOffice Calc .


1 Answers

Using the PHPExcel library to read an Excel file and transfer the data into a database

//  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 } 

Anything more becomes very dependent on your database, and how you want the data structured in it

like image 137
Mark Baker Avatar answered Oct 09 '22 14:10

Mark Baker