【问题标题】:Error Date Format Import Excel to Mysql using PHPExcel Codeigniter错误日期格式使用 PHPExcel Codeigniter 将 Excel 导入 Mysql
【发布时间】:2018-03-05 02:20:44
【问题描述】:

我想使用 Codeigniter 上的库 PHPExcel 将文件 Excel 导入 Mysql,我的代码成功地将数据插入数据库,但如果我将类型数据格式更改为整数,则日期格式仅为 0000-00-00,字段日期插入编号 31248。

这是我的控制器功能

public function upload_excel()
{
  $fileName = $this->input->post('file', TRUE);

  $config['upload_path'] = './assets/excel/'; 
  $config['file_name'] = $fileName;
  $config['allowed_types'] = '*';
  $config['encrypt_name']= TRUE;
  $config['max_size'] = 0;

  $this->load->library('Upload', $config);
  $this->upload->initialize($config); 

  if (!$this->upload->do_upload('file')) {
   $error = $this->upload->display_errors();
   $this->session->set_flashdata('pesan',$error); 
   redirect('sio'); 
 } else {
   $media = $this->upload->data();
   $inputFileName = './assets/excel/'.$media['file_name'];

   try {
    $inputFileType = IOFactory::identify($inputFileName);
    $objReader = IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
  } catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
  }

  $sheet = $objPHPExcel->getSheet(0);
  $highestRow = $sheet->getHighestRow();
  $highestColumn = $sheet->getHighestColumn();
  for ($row = 2; $row <= $highestRow; $row++){  
   $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
     NULL,
     TRUE,
     FALSE);
   $data = array(
     "kd_pekerja"=> trim(preg_replace("/[^a-zA-Z0-9]/", "", $rowData[0][1])), // opsional hapus spasi depan
                "no_ktp"=> $rowData[0][2],
                "nm_karyawan"=> $rowData[0][3],
                "tgl_lahir"=> $rowData[0][4],
                "no_sio"=> $rowData[0][5],
                "tgl_buat"=> $rowData[0][6],
                "ms_berlaku"=> $rowData[0][7],
                "ket"=> $rowData[0][8]);


   $this->db->insert("tbl_sio",$data);
 } 
   unlink($inputFileName); // hapus file temp
   $count = $highestRow;
   $this->session->set_flashdata('pesan','Upload berhasil, Total: <b>'.$count.'</b> data.'); 
   redirect('sio');

 }
}

【问题讨论】:

    标签: codeigniter phpexcel


    【解决方案1】:

    你可以使用PHPExcel_Shared_Date

     $worksheet=$objPHPExcel->getActiveSheet();
     foreach ($worksheet->getRowIterator() as $row) {
        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(FALSE); // Loop all cells, even if it is not set
        foreach ($cellIterator as $cell) {
          if (PHPExcel_Shared_Date::isDateTime($cell)) {
             $date=date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($cell->getValue()));
          }
        }
     }
    

    【讨论】:

    • 仍然是数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2016-08-17
    • 1970-01-01
    • 2016-07-05
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多