如何更改数组 header 并将数组插入 PHPExcel 中的 MySQL 数据库?
How to change array header and insert the array into MySQL database in PHPExcel?
我有一段用于制作 Excel 导入 (.xlsx) 的代码。我有一个关于插入迭代数组和更改数组的问题 header.
控制器:
$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,它由一个字符串header组成。我从第二行 select。该列可能会展开。
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
插入我的数据库,但 header 仍然是数字([0]
、[1]
、[2]
、[3]
),因为 [ $getCellByColumnAndRow
使用的=24=]和$col
是数字。
header变成
后如何将这些数组插入数据库
Array
(
[no_ijazah] => 1245/SA
[jumlah] => 140000000
[keluar_1] => 20000000
[keluar_3] => 15000000
[keluar_4] => 25000000
)
?
希望对您有所帮助:
您可以像这样更改 $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
将数据插入table
$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);
}
我有一段用于制作 Excel 导入 (.xlsx) 的代码。我有一个关于插入迭代数组和更改数组的问题 header.
控制器:
$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,它由一个字符串header组成。我从第二行 select。该列可能会展开。
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
插入我的数据库,但 header 仍然是数字([0]
、[1]
、[2]
、[3]
),因为 [ $getCellByColumnAndRow
使用的=24=]和$col
是数字。
header变成
后如何将这些数组插入数据库Array
(
[no_ijazah] => 1245/SA
[jumlah] => 140000000
[keluar_1] => 20000000
[keluar_3] => 15000000
[keluar_4] => 25000000
)
?
希望对您有所帮助:
您可以像这样更改 $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
将数据插入table
$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);
}