将excel导入到mysql时,删除逗号。

问题描述 投票:0回答:1

当我从excel文件导入数据到数据库时,我想删除逗号,我使用phpexcel库导入数据。

我有一个这样的控制器,用于导入excel文件。

M_excel.php

 public function import(){
    $user = $this->ion_auth->user()->row();
    include APPPATH.'third_party/PHPExcel/PHPExcel.php';

$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('excel/'.$this->filename.'.xlsx'); 
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);

$data = array();

$numrow = 4;
foreach($sheet as $row){
  if($numrow > 4){
    array_push($data, array(
      'NO' => $row['A'],
      'APIID' => $row['B'],
      'Jan19' => $row['C'],
      'Feb19' => $row['D'],
      'Mar19' => $row['E'],
      'Apr19' => $row['F'],
      'May19' => $row['G'],
      'Jun19' => $row['H'],
      'Jul19' => $row['I'],
      'Aug19' => $row['J'],
      'Sep19' => $row['K'],
      'Oct19' => $row['L'],
    ));
  }

  $numrow++;
}

$this->m_excel->insert_multiple($data);

redirect("excel_import"); 
}

Excel文件Excel

mysql codeigniter phpexcel
1个回答
1
投票

只需使用str_replace(",","", "string here")来替换逗号,就像下面这样。

$data = array();
$i=0;
foreach($sheet as $row){
if($numrow > 4){
    $data[$i]['NO'] = $row['A'];
    $data[$i]['APIID'] = $row['B'];
    $data[$i]['Jan19'] = str_replace(",", "", $row['C']) // use str_replace where you want to replace comma
    $data[$i]['Feb19'] = $row['D'];
    $data[$i]['Mar19'] = $row['E'];
    $data[$i]['Apr19'] = $row['F'];
    $data[$i]['May19'] = $row['G'];
    $data[$i]['Jun19'] = $row['H'];
    $data[$i]['Jul19'] = $row['I'];
    $data[$i]['Aug19'] = $row['J'];
    $data[$i]['Sep19'] = $row['K'];
    $data[$i]['Oct19'] = $row['L'];
}
$i++;
$numrow++;
}
$this->m_excel->insert_multiple($data);
redirect("excel_import");
© www.soinside.com 2019 - 2024. All rights reserved.