How does PHP use phpexcel to import databases?

Source: Internet
Author: User
The following describes the implementation code of using phpexcel to import a database in PHP. if you need it, you can refer to the code below.

The code is as follows:


Error_reporting (E_ALL); // An error occurred while enabling
Set_time_limit (0); // The script does not time out.

Date_default_timezone_set ('Europe/London '); // Set the time

/** Include path **/
Set_include_path (get_include_path (). PATH_SEPARATOR. 'http: // www.jb51.net/./classes/'); // Set the environment variable

/** PHPExcel_IOFactory */
Include 'phpexcel/IOFactory. php ';

// $ InputFileType = 'excel5'; // read the xls
$ InputFileType = 'excel2007 '; // This is the xlsx
// $ InputFileName = './sampleData/example2.xls ';
$ InputFileName = './sampleData/book.xlsx ';

Echo 'loading file', pathinfo ($ inputFileName, PATHINFO_BASENAME), 'using IOFactory with a defined reader type of ', $ inputFileType ,'
';
$ ObjReader = PHPExcel_IOFactory: createReader ($ inputFileType );
$ ObjPHPExcel = $ objReader-> load ($ inputFileName );
/*
$ Sheet = $ objPHPExcel-> getSheet (0 );
$ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
$ HighestColumn = $ sheet-> getHighestColumn (); // get the total column
*/
$ ObjWorksheet = $ objPHPExcel-> getActiveSheet (); // get the total number of rows
$ HighestRow = $ objWorksheet-> getHighestRow (); // gets the total number of columns

Echo 'hehestrow = '. $ highestRow;
Echo"
";
$ HighestColumn = $ objWorksheet-> getHighestColumn ();
$ HighestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn); // The total number of columns
Echo 'hehestcolumnindex = '. $ highestColumnIndex;
Echo"
";
$ Headtitle = array ();
For ($ row = 1; $ row <= $ highestRow; $ row ++)
{
$ Strs = array ();
// Note that the index of the highestColumnIndex column starts from 0.
For ($ col = 0; $ col <$ highestColumnIndex; $ col ++)
{
$ Strs [$ col] = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row)-> getValue ();
}
$ Info = array (
'Word1 '=> "$ strs [0]",
'Word2 '=> "$ strs [1]",
'Word3 '=> "$ strs [2]",
'Word4' => "$ strs [3]",
);
// You can connect to your database and write it to the database.
Print_r ($ info );
Echo'
';
}
?>

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.