我试图从包含多个工作表的excel文件中获取数据,并将该数据添加到mysql数据库中。这是我陷入困境的地方:
set_include_path(get_include_path() .PATH_SEPARATOR .'modules/phpexcel/Classes/');
include 'PHPExcel/IOFactory.php';
if(isset($_POST['submit'])){
$target_dir = 'uploads/';
$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
$inputFileType = 'Excel5';
$inputFileName = 'uploads/PIE_Date.xls';
class chunkReadFilter implements PHPExcel_Reader_IReadFilter{
private $_startRow = 0;
private $_endRow = 0;
public function setRows($startRow, $chunkSize){
$this->_startRow = $startRow;
$this->_endRow = $startRow + $chunkSize;
}
public function readCell($column, $row, $worksheetName = ''){
if(($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)){
return true;
}
return false;
}
}
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$chunkSize = 2048;
$chunkFilter = new chunkReadFilter();
$objReader->setReadFilter($chunkFilter);
for($startRow = 2; $startRow <= 65536; $startRow += $chunkSize){
$chunkFilter->setRows($startRow, $chunkSize);
$objPHPExcel = $objReader->load($inputFileName);
}
}
从这里我不知道如何从细胞中获取数据。如果我可以从单元格中获取数据,我想我可以处理剩下的代码(将其添加到数据库中)。
答案 0 :(得分:0)
if (isset($_FILES['uploadFile']['name']) && $_FILES['uploadFile']['name'] != "") {
$allowedExtensions = array("xls", "xlsx");
// Returns the extension of the file
$ext = pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION);
if (in_array($ext, $allowedExtensions)) {
$file = "uploads/".$_FILES['uploadFile']['name'];
$isUploaded = copy($_FILES['uploadFile']['tmp_name'], $file);
if ($isUploaded) {
include 'dbconnect.php'; //Connection file
include "Classes/PHPExcel/IOFactory.php";
try{
$objPHPExcel = PHPExcel_IOFactory::load($file);
} catch(Exception $e){
die('Erroe loading File "' . pathinfo($inputFileName, PATHINFO_BASENAME).'" '.$e->getMessge() );
}
// An ecel file may contains many sheets so you have to specify which one you need to read or work with
$sheet = $objPHPExcel->getSheet(0);
// It returns the highest number of rows
$total_rows = $sheet->getHighestRow();
// It returns the highest number of columns
$highest_column = $sheet->getHighestColumn();
// Table used to display the contents of the file
echo '<h4>Data from excel file</h4>';
echo '<table cellpadding="5" cellspacing="1" border="1" class="responsive">';
// Loop through each row of the worksheet in turn
for ($row=2; $row <=$total_rows; $row++) {
// Read a row of data into an rray
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highest_column . $row, NULL, TRUE, FALSE);
$sql = "write your sql query here";
if (mysqli_query($con, $sql)) {
// if success show the excel table
echo "<tr>";
foreach($rowData[0] as $key=>$value) {
echo "<td>".$value."</td>";
}
echo "</tr>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
} //end for
echo '</table>';
} //end
else{
echo '<span class="msg">File not uploaded!</span>';
}
} // end if (in_array($ext, $allowedExtensions))
else {
echo '<span class="msg">This type of file not allowed!</span>';
}
} // end if (isset($_FILES['uploadFile']['name']) && $_FILES['uploadFile']['name'] != "")
else {
echo '<span class="msg">Select an excel file first!</span>';
}
希望这会对你有所帮助。