How to use phpexcel to import a database in PHP _ PHP Tutorial

Source: Internet
Author: User
PHP uses phpexcel to import data to a database. If you don't talk about it, simply copy the code as follows :? Phperror_reporting (E_ALL); enable set_time_limit (0); script does not time out date_default_timezone_set

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'
';
}
?>

The pipeline code is as follows :? Php error_reporting (E_ALL); // The error set_time_limit (0) is enabled; // The script does not time out. date_default_timezone_set ('Europe/Lo...

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.