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.
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.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With