问题描述
我想从我的 codeigniter 使用 PHP excel 从 excel 导入,当前单元格包含当前文本
cell1 | cell2 |
---|---|
A | 1 |
B | 2 |
C | 3 |
我只想要cell1中除B之外的cell插入数据库
我的控制器
public function import_excel(){
if (isset($_FILES["fileExcel"]["name"])) {
$path = $_FILES["fileExcel"]["tmp_name"];
$object = IOFactory::load($path);
foreach($object->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
for($row=2; $row<=$highestRow; $row++)
{
$cell1= $worksheet->getCellByColumnAndRow(0,$row)->getValue();
$cell2= $worksheet->getCellByColumnAndRow(1,$row)->getValue();
$temp_data[] = array(
'cell1' => $cell1,'cell2' => $cell2,);
}
}
if($cell1 == 'B'){
}else{
$this->load->model('M_Admin');
$insert = $this->M_Admin->insert($temp_data);
}
但这失败了,数据仍然插入。请帮助我,非常感谢您的帮助。
解决方法
我以前从未使用过 phpexcel,但如果它是数组并考虑 $cell1 是字符串/文本,我想您可以尝试使用它:
for($row=2; $row<=$highestRow; $row++)
{
$cell1= $worksheet->getCellByColumnAndRow(0,$row)->getValue();
$cell2= $worksheet->getCellByColumnAndRow(1,$row)->getValue();
//cell value you want to hide
if ($cell1 == 'B') {
$cell1 = 'xxx'; //overwrite that value into something
}
$temp_data[] = array(
'cell1' => $cell1,'cell2' => $cell2,);
}
注意:我使用的是 codeigniter 3