我有一段用于创建Excel Import (.xlsx)的代码。我有一个关于插入迭代数组和更改数组头的问题。
控制器:
$this->load->library('Excel');
$file = 'upload/keuangan.xlsx';
//read file from path
$objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel->setReadDataOnly(true);
$objPHPExcel = $objPHPExcel->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); //dari 0
$data['nomor'] = $this->participant_model->get_dummy(false)->result_array();
$num = $this->participant_model->get_dummy(false)->num_rows();
for($row=2; $row <= $highestRow; ++$row){
for($col=0; $col < $highestColumnIndex; ++$col){
$ExcelData[$col] = $objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
}
echo "<pre>";print_r($ExcelData);echo "</pre>";
$this->session->set_flashdata('message_alert','<div class="alert alert-success">Data berhasil dimasukkan</div>');
$this->participant_model->save($ExcelData);
}
型号:
public function save($data_participant){
$this->db->insert('t_keuangan',$data_participant);
}
这是keuangan.xlsx,它由一个字符串头组成。我从第二行选择。该列可以被展开。
No Ijazah Jumlah Keluar 1 Keluar 2 Keluar 3
1234/SH 100000000 21000000 19000000 18000000
2345/SK 120000000 16000000 19000000 13000000
1245/SA 140000000 20000000 15000000 25000000
$ExcelData
的结果是:
Array
(
[0] => 1234/SH
[1] => 100000000
[2] => 21000000
[3] => 19000000
[4] => 18000000
)
Array
(
[0] => 2345/SK
[1] => 120000000
[2] => 16000000
[3] => 19000000
[4] => 13000000
)
Array
(
[0] => 1245/SA
[1] => 140000000
[2] => 20000000
[3] => 15000000
[4] => 25000000
)
我正在尝试将此$ExcelData
放入我的数据库MySQL中。我可以将$ExcelData
插入到我的数据库中,但标题仍然是数字的([0]
、[1]
、[2]
、[3]
),因为$getCellByColumnAndRow
使用的$row
和$col
都是数字的。
如何在标题变为后将这些数组插入到数据库中
Array
(
[no_ijazah] => 1245/SA
[jumlah] => 140000000
[keluar_1] => 20000000
[keluar_3] => 15000000
[keluar_4] => 25000000
)
发布于 2018-06-22 08:28:07
希望这能对你有所帮助:
您可以像这样更改$ExcelData
数据以插入到数据库中;
请参阅工作示例:https://eval.in/1026123
使用array_combine
添加键值对,如下所示:
$value1 = Array
(
'0' => '1234/SH',
'1' => 100000000,
'2' => 21000000,
'3' => 19000000,
'4' => 18000000
);
$value2 = Array
(
'0' => '2345/SK',
'1' => 120000000,
'2' => 16000000,
'3' => 19000000,
'4' => 13000000
);
$key = array('no_ijazah','jumlah','keluar_1','keluar_3','keluar_4');
$combined[] = array_combine($key, $value1);
$combined[] = array_combine($key, $value2);.
print_r($combined);
$this->participant_model->save($combined);
输出
Array
(
[0] => Array
(
[no_ijazah] => 1234/SH
[jumlah] => 100000000
[keluar_1] => 21000000
[keluar_3] => 19000000
[keluar_4] => 18000000
)
[1] => Array
(
[no_ijazah] => 2345/SK
[jumlah] => 120000000
[keluar_1] => 16000000
[keluar_3] => 19000000
[keluar_4] => 13000000
)
)
使用insert_batch
将数据插入到表中
$this->db->insert_batch('t_keuangan',$combined);
整个代码应该是这样的:
$key = array('no_ijazah','jumlah','keluar_1','keluar_3','keluar_4');
for($row=2; $row <= $highestRow; ++$row)
{
for($col=0; $col < $highestColumnIndex; ++$col)
{
$ExcelData[$col] = $objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
}
$combined[] = array_combine($key, $ExcelData);
}
$this->session->set_flashdata('message_alert','<div class="alert alert-success">Data berhasil dimasukkan</div>');
$this->participant_model->save($combined);
模型函数应如下所示:
public function save($data_participant)
{
$this->db->insert_batch('t_keuangan',$data_participant);
}
https://stackoverflow.com/questions/50978912
复制相似问题