Phpexcel reading Excel files

Source: Internet
Author: User
It is recommended that the Web only upload function, read, processing or backstage bar. Phpexcel is still more time-consuming and memory-intensive.

Instance code:

error_reporting (0);
Require_once ' phpexcel_1.8.0/classes/phpexcel.php '; Modify the directory for yourself
Echo '

TEST phpexcel 1.8.0:read xlsx file

';

$filePath = "Cdkey.xlsx";

Create a Reader object
$PHPReader = new phpexcel_reader_excel2007 ();
if (! $PHPReader->canread ($filePath)) {
$PHPReader = new Phpexcel_reader_excel5 ();
if (! $PHPReader->canread ($filePath)) {
Echo ' file is not Excel ';
return;
}
}

Create an Excel object where you can read the file from an Excel object, or write it to a file
$PHPExcel = $PHPReader->load ($filePath);

/** reading the first worksheet in an Excel file */
$currentSheet = $PHPExcel->getsheet (0);
/** get the largest column number */
$allColumn = $currentSheet->gethighestcolumn ();
How many lines does the/** get altogether? */
$allRow = $currentSheet->gethighestrow ();

Iterates through the contents of each cell. Note that the row starts at 1 and the column starts from a
for ($rowIndex =1; $rowIndex <= $allRow; $rowIndex + +) {
for ($colIndex = ' A '; $colIndex <= $allColumn; $colIndex + +) {
$addr = $colIndex. $rowIndex;
$cell = $currentSheet->getcell ($addr)->getvalue ();
if ($cell instanceof Phpexcel_richtext)//Rich Text conversion string
$cell = $cell->__tostring ();
$arrExcel [$rowIndex] [$colIndex]= $cell;

}

}

Print_r ($arrExcel); exit;


Phpexcel_richtext Object (
[_richtextelements:private] = = Array
(
[0] = = phpexcel_richtext_textelement Object ([_text:private] = test)
[1] = = Phpexcel_richtext_run Object
(
[_font:private] = Phpexcel_style_font Object
(
[_name:private] = Calibri
[_size:private] = 11
[_bold:private] =
[_italic:private] =
[_superscript:private] =
[_subscript:private] =
[_underline:private] = None
[_strikethrough:private] =
[_color:private] = Phpexcel_style_color Object
(
[_argb:private] = FF000000
[_issupervisor:private] =
[_parent:private] =
[_parentpropertyname:private] =
)
[_parentpropertyname:private] =
[_issupervisor:private] =
[_parent:private] =
[ColorIndex] = 8
)
[_text:private] = 1
)
)
)

You can see that the text contents of the cell cannot be read directly to such a cell. (Note: The rich text here is my own translation, do not know to no).

In addition, the functions of reading cells are:

Column starting from 0, line starting from 1

$currentSheet->getcellbycolumnandrow ($colIndex, $rowIndex)->getvalue ();

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.