PHP manipulating Excel files, importing data from Excel into the database

Source: Internet
Author: User
idea One: use an XML file to save a column in Excel from a field in a datasheet, and then add data through an XML file idea two: The first row in Excel saves the field name, and the other rows save the data Three ideas: the column in Excel corresponds to a field in a data table, saved with a. php file here is the idea of two, using the Phpexcel

1. Create an Object

$objReader = Phpexcel_iofactory::createreader (' Excel5 ');//use excel2007 for
2. Specify an Excel file
$objPHPExcel = $objReader->load ($newFileName);
$sheet = $objPHPExcel->getsheet (0); Gets the data that operates the first table in Excel
$highestRow = $sheet->gethighestrow (); Get total number of rows returned to be int type
$highestColumn = $sheet->gethighestcolumn (); Gets the total number of columns returned is the letter
$allFileds = ";
$filedsSum = ' A '; Set the default maximum value for a valid column
$dataInfo = ";
$tidIndex = 0;
for ($i = ' A '; $i < $highestColumn; + + $i) {
$curFiled = $sheet->getcell ($i. ' 1 ')->getvalue ();
if (!empty ($curFiled)) {//Filter invalid columns
$filedsSum = $i;
$allFileds. = ', '. $curFiled. ' `';
}
}
$INPUTDATASQL = "; Inserting SQL statements
for ($j =2; $j <= $highestRow; $j + +) {
$inputData =array ();
for ($i = ' A '; $i <= $filedsSum; + + $i) {
$inputData []= '. $sheet->getcell ($i. $j)->getvalue (). ' \'';
Because you are not sure whether the field is a string or an int type, adding "will not cause an error
$inputDataSql. = ', ('. Implode (', ', $inputData) ') ';
}
$INPUTDATASQL =substr ($INPUTDATASQL, 1); Remove the front one ', ' comma
$sql = ' INSERT into ('. $allFileds. ') VALUES '. $inputDataSql;

Finally, the corresponding database operation object is called and the insert operation is performed.

The above describes the PHP operation Excel file, the Excel data import into the database, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.