Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel to PHP array, possible and how?

Tags:

I have a big excel file that looks like this:

enter image description here

I would like to put each row into an array.

Is this possible to access the first row's order id like this?

$result[0][2] // returns 7432

Assuming the actual first row that gives prefix for the columns' name is not present.

How could I do that?

like image 868
Karem Avatar asked Nov 17 '11 16:11

Karem


People also ask

How do I convert Excel data to array?

Copy the table below and paste it into Excel in cell A1. Be sure to select cells E2:E11, enter the formula =C2:C11*D2:D11, and then press Ctrl+Shift+Enter to make it an array formula.

How connect Excel to PHP?

Establish a ConnectionOpen the connection to Excel by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all. $conn = odbc_connect("CData ODBC Excel Source","user","password"); Connections opened with odbc_connect are closed when the script ends.

How can read data from Excel file in PHP?

Loading a Spreadsheet File $inputFileName = './sampleData/example1. xls'; /** Load $inputFileName to a Spreadsheet Object **/ $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName); See samples/Reader/01_Simple_file_reader_using_IOFactory. php for a working example of this code.

What are the 3 types of PHP arrays?

Create an Array in PHP In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.


2 Answers

I recommended to use PHPEXCEL library

https://github.com/PHPOffice/PHPExcel

you can see an example

Update: Now the alternative to this library is phpspreadsheet

like image 95
Haim Evgi Avatar answered Oct 09 '22 20:10

Haim Evgi


Save the spreadsheet as a CSV, then use PHP's built-in CSV functions. See the sample code here:

http://php.net/manual/en/function.fgetcsv.php

like image 37
Dan Avatar answered Oct 09 '22 20:10

Dan