使用 PHPExcel 导入时用数据映射 Table 列
Mapping Table columns with data while Importing using PHPExcel
我有一个 php 应用程序,我将数据从 excel sheet 导入数据库。我为此使用 phpExcel。
我已经为此实现了一个简单的导入功能。
现在,我的问题是当我以特定格式将数据形式 excel sheet 导入数据库时。喜欢,
Table姓名:-我table
编号 |姓名 |姓 |电邮 |日期 |
和我用来导入的 excel 文件具有相同的格式。但是格式可以改变 Like,
编号 |姓 |姓名 |日期 |电子邮件 |
如何将 excel 文件中的列映射到数据库 table 以便正确的数据输入到数据 table。
首先阅读 headers,然后遍历每个 header 条目以获取列名,然后在循环行时可以构建 StdClass [=14 的关联数组=] 该行的适当映射
$headers = $objPHPExcel->getActiveSheet()->toArray('A1');
$highestRow = $sheet->getHighestRow();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row);
// Map the row data array to the headers array
$dataMapArray = array_combine(
$header,
$rowData
);
// If you prefer objects, then convert that associative array to an object
$dataMapObject = (object) $dataMapArray;
}
我有一个 php 应用程序,我将数据从 excel sheet 导入数据库。我为此使用 phpExcel。
我已经为此实现了一个简单的导入功能。
现在,我的问题是当我以特定格式将数据形式 excel sheet 导入数据库时。喜欢,
Table姓名:-我table
编号 |姓名 |姓 |电邮 |日期 |
和我用来导入的 excel 文件具有相同的格式。但是格式可以改变 Like,
编号 |姓 |姓名 |日期 |电子邮件 |
如何将 excel 文件中的列映射到数据库 table 以便正确的数据输入到数据 table。
首先阅读 headers,然后遍历每个 header 条目以获取列名,然后在循环行时可以构建 StdClass [=14 的关联数组=] 该行的适当映射
$headers = $objPHPExcel->getActiveSheet()->toArray('A1');
$highestRow = $sheet->getHighestRow();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row);
// Map the row data array to the headers array
$dataMapArray = array_combine(
$header,
$rowData
);
// If you prefer objects, then convert that associative array to an object
$dataMapObject = (object) $dataMapArray;
}