每当我使用PHPExcel将excel文件导入MySQL时,我都没有得到一些值,并且值也被插入到不相关的列中。
但是当我使用echo显示excel文件时,它会显示确切的报告。
另外,我只想从10-15列中导入少数选定的列。
提前致谢
我的代码
<?php
//include the following 2 files
require 'C:\xampp\PHPExcel_1.7.9_doc\Classes\PHPExcel.php';
require_once 'C:\xampp\PHPExcel_1.7.9_doc\Classes\PHPExcel\IOFactory.php';
$conn = mysql_connect("localhost","root","");
mysql_select_db("invoice",$conn);
$file=$_POST['file'];
$srow=$_POST['srow'];
$objPHPExcel = PHPExcel_IOFactory::load($file);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
echo "<br>The worksheet ".$worksheetTitle." has ";
echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
echo ' and ' . $highestRow . ' row.';
}
echo 'Data: <table width="100%" cellpadding="3" cellspacing="0"><tr>';
for ($row = $srow; $row <= $highestRow-1; ++ $row) {
echo '<tr>';
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
if($row === 1)
echo '<td style="background:#000; color:#fff;">' . $val . '</td>';
else
echo '<td>' . $val . '</td>';
}
echo '</tr>';
}
echo '</table>';
for ($row = $srow; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col <= $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $srow);
$val[] = $cell->getValue();
}
$dropdownval=$_POST['menu'];
if ($dropdownval!='none')
{
$sql="INSERT INTO $dropdownval(`Bkng Date`,`ID Number`,`Pax Name`,`Ticket Number`,`PNR`,`Sector`,`Travel Date`,`Basic`,`Tax`,`Net Payable`)
VALUES('".$val[1]."','".$val[2]."','".$val[3]."','" .$val[4]."','".$val[5]."','".$val[6]."','".$val[7]."','".$val[8]."','".$val[9]."','".$val[10]."')";
mysql_query($sql)or die('Invalid query: ' . mysql_error());
if(mysql_query($sql))
echo "Successfully Uploaded ".$file;
else
echo "Error while uploading ".$file;
//echo $sql."\n";
}
else
{
$tname=$_POST['tname'];
$createTable="CREATE TABLE $tname(
`Bkng Date` DATE NOT NULL ,
`ID Number` INT NOT NULL ,
`Pax Name` VARCHAR( 100 ) NOT NULL ,
`Ticket Number` VARCHAR( 100 ) NOT NULL ,
`PNR` VARCHAR( 50 ) NOT NULL ,
`Sector` VARCHAR( 50 ) NOT NULL ,
`Travel Date` DATE NOT NULL ,
`Basic` DECIMAL NOT NULL ,
`Tax` DECIMAL NOT NULL ,
`Net Payable` DECIMAL NOT NULL)";
mysql_query($createTable);
$sql1="INSERT INTO $tname(`Bkng Date`,`ID Number`,`Pax Name`,`Ticket Number`,`PNR`,`Sector`,`Travel Date`,`Basic`,`Tax`,`Net Payable`)
VALUES('".$val[1]."','".$val[2]."','".$val[3]."','" .$val[4]."','".$val[5]."','".$val[6]."','".$val[7]."','".$val[8]."','".$val[9]."','".$val[10]."')";
mysql_query($sql1)or die('Invalid query: ' . mysql_error());
if(mysql_query($sql1))
echo "Table $tname has been created...Successfully Uploaded ".$file;
else
echo "Error while uploading ".$file;
}
}
?>