A simple introduction to using Excel in PHP _php tutorial

Source: Internet
Author: User
Tags comparison table
In the work need to deal with multi-lingual translation problems, translation is written in the Excel table. I will keep both the Chinese and English columns for the convenience of processing.

This requires that the data be taken out of Excel, and stored in an array of Excel, by using a looping array to put the corresponding data into the database.

So the first step in the job is to get the data out of Excel. Here I use an open source PHP processing Excel class: Phpexcel. The details of the project are http://phpexcel.codeplex.com/.

I am currently using the phpexcel1.7.3 version, there is a phpexcel and phpexcel.php file after decompression.
We mainly use that PHP file. See File directory structure



This version is said to support excel2007, but I can't get support for this library with the 2007 edition of Xlsx. So I turned it into 2003. Feeling supportive is very good.

Here are some specific uses:
Copy the Code code as follows:
Require_once ('./phpexcel1.7.3/phpexcel.php ');
$php _excel_obj = new Phpexcel ();
$php _reader = newphpexcel_reader_excel2007 ();
if (! $php _reader->canread ($file _name)) {
$php _reader= new Phpexcel_reader_excel5 ();
if (! $php _reader->canread ($file _name)) {
Echo ' NO excel! ';
}
}
$php _excel_obj = $php _reader->load ($file _name);
$current _sheet = $php _excel_obj->getsheet (0);

The main function above is to initialize the relevant Excel class and load Excel first sheet
Copy the Code code as follows:
$all _column = $current _sheet->gethighestcolumn ();
$all _row = $current _sheet->gethighestrow ();

The maximum column values of the table are obtained (the letters are denoted as: ' G '), and the maximum number of rows (numeric representation)

The following will use loops to read the data in Excel in Excel:
Copy CodeThe code is as follows:
$all _arr = Array ();
$c _arr = Array ();
Character comparison table
for ($r _i = 1; $r _i<= $all _row; $r _i++) {
$c _arr= Array ();
for ($c _i= ' A '; $c _i<= ' B '; $c _i++) {
$adr = $c _i. $r _i;
$value = $current _sheet->getcell ($ADR)->getvalue ();
if ($c _i== ' A ' && Empty ($value)) break;
if (Is_object ($value)) $value = $value->__tostring ();
$c _arr[$c _i]= $value;
}
$c _arr&& $all _arr[] = $c _arr;
}

The following is a brief introduction to Phpexcel's write operation, which is often used to import data from a database into Excel for easy presentation and more aesthetic results.
Copy CodeThe code is as follows:
Require_once ('./phpexcel1.7.3/phpexcel.php ');
$excel _obj = new Phpexcel ();
$objWriter = Newphpexcel_writer_excel5 ($excel _obj);
$excel _obj->setactivesheetindex (0);
$act _sheet_obj= $excel _obj->getactivesheet ();
$act _sheet_obj->settitle (' sheet ');
$act _sheet_obj->setcellvalue (' A1 ', ' string content ');
$act _sheet_obj->setcellvalue (' A2 ', 26);

$file _name = "Output.xls";
$objWriter->save ($file _name);

The code is simple, first initializing the relevant Excel write class, then writing the data, and finally saving it as an XLS file.
The effect of the output is shown in figure

http://www.bkjia.com/PHPjc/328088.html www.bkjia.com true http://www.bkjia.com/PHPjc/328088.html techarticle in the work need to deal with multi-lingual translation problems, translation is written in the Excel table. I will keep both the Chinese and English columns for the convenience of processing. This requires that the data be from Excel ...

  • 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.