Read xlsx file with php

Can anyone recommend a good library for reading xlsx files using php?

ive looked at phpexcel, but from the examples it seems like it only supports writing?

+4
source share
2 answers

I will look again at PHPExcel. PHPExcel has entries for Excel5 (xls), Excel2007 (xlsx), CSV, HTML and PDF; and readers for Excel5 (xls), Excel2007 (xlsx), Excel 2003 XML, CSV, SYLK, and Open Office Calc

All this is perfectly clear in the documentation.

EDIT (quoted in manual)

There are two methods of reading in a file in PHPExcel: using automatic file type detection or explicitly.

Automatic file type detection is checked by another PHPExcel_Reader_IReader distributed with PHPExcel. If one of them can load the specified file name, the file is loaded using this PHPExcel_Reader_IReader. In explicit mode, you need to specify which PHPExcel_Reader_IReader to use.

You can create an instance of PHPExcel_Reader_IReader using PHPExcel_IOFactory in automatic file type resolution mode using the following code example:

$objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx"); 

A typical use of this feature is when you need to read files uploaded by your users and you don’t know if they download xls or xlsx files.

If you need to set some properties on a reader (for example, read-only data, more on this in more detail), you can use this option instead:

 $objReader = PHPExcel_IOFactory::createReaderForFile("05featuredemo.xlsx"); $objReader->setReadDataOnly(true); $objReader->load("05featuredemo.xlsx"); 

You can instantiate PHPExcel_Reader_IReader using PHPExcel_IOFactory in explicit mode using the following code example:

 $objReader = PHPExcel_IOFactory::createReader("Excel2007"); $objPHPExcel = $objReader->load("05featuredemo.xlsx"); 

EDIT (Personal Preferences)

It is also useful to wrap your bootloader in try / catch

 $fileName = '01simple.xlsx'; try { $objPHPExcel = PHPExcel_IOFactory::load($fileName); } catch (Exception $e) { die("Error loading file: ".$e->getMessage()."<br />\n"); } 
+11
source

It depends on what you mean by "reading."

Office XML files are just zip files containing one or more XML documents. Any old XML parser should be able to read them.

Doing something useful, interesting, and productive with data, on the other hand, can bring some effort.

+1
source

Source: https://habr.com/ru/post/1315746/


All Articles