เรียนท่านเทพ ทุกคนครับ รบกวนช่วยดูให้หน่อยครับ PHPExcel กับ Reader การอ่านไฟล์ Excel (.xls,.xlsx) และการนำเข้าฐานข้อมูล MySQL
PHPExcel กับ Reader การอ่านไฟล์ Excel (.xls,.xlsx) และการนำเข้าฐานข้อมูล MySQL
มันเกิด error อย่างนี้ครับ
Code
Fatal error: Uncaught exception 'Exception' with message 'Could not open 2.xls for reading! File does not exist.' in C:\AppServ\www\tsis\Classes\PHPExcel\Reader\Excel5.php:509 Stack trace: #0 C:\AppServ\www\tsis\Classes\PHPExcel\IOFactory.php(257): PHPExcel_Reader_Excel5->canRead('2.xls') #1 C:\AppServ\www\tsis\Classes\PHPExcel\IOFactory.php(206): PHPExcel_IOFactory::createReaderForFile('2.xls') #2 C:\AppServ\www\tsis\PHPExcelReadToMySQL.php(11): PHPExcel_IOFactory::identify('2.xls') #3 {main} thrown in C:\AppServ\www\tsis\Classes\PHPExcel\Reader\Excel5.php on line 509
Code (PHP)
<?php
/** PHPExcel */
require_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory - Reader */
include 'Classes/PHPExcel/IOFactory.php';
$inputFileName = $_FILES['filUpload']['name'];
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
$headingsArray = $headingsArray[1];
$r = -1;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
++$r;
foreach($headingsArray as $columnKey => $columnHeading) {
$namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
}
}
}
//echo '<pre>';
//var_dump($namedDataArray);
//echo '</pre><hr />';
//*** Connect to MySQL Database ***//
$objConnect = mysql_connect("localhost","root","1234") or die(mysql_error());
$objDB = mysql_select_db("db_tsis");
$i = 0;
foreach ($namedDataArray as $result) {
$i++;
$strSQL = "";
$strSQL .= "INSERT INTO tb_formular ";
$strSQL .= "(formID,class,sub_class,rmcode,tradeName,inci_name,distributor,storage_place,manufature,price,pck_size) ";
$strSQL .= "VALUES ";
$strSQL .= "('".$result["formID"]."','".$result["class"]."' ";
$strSQL .= ",'".$result["sub_class"]."','".$result["rmcode"]."' ";
$strSQL .= ",'".$result["tradeName"]."','".$result["inci_name"]."' ";
$strSQL .= ",'".$result["distributor"]."','".$result["manufature"]."' ";
$strSQL .= ",'".$result["storage_place"]."','".$result["price"]."','".$result["pck_size"]."') ";
mysql_query($strSQL) or die(mysql_error());
echo "Row $i Inserted...<br>";
}
mysql_close($objConnect);
?>
Tag : PHP, MySQL, CakePHP, Excel (Excel.Application)
Date :
2014-03-08 07:06:40
By :
algorithm
View :
2286
Reply :
2
ขอบคุณครับ
Date :
2014-04-18 17:09:24
By :
algorithm
Load balance : Server 00