public function readexcel()
{
$res = array();
$file_path = ‘C:\Users\Administrator\Desktop\ecword_2017-07-24.xls’;
if (file_exists($file_path)) {
$fp = fopen($file_path, “r”);
$str = fread($fp, filesize($file_path)); //指定读取大小,这里把整个文件内容读取出来
$str = str_replace(“\r\n”, “<br />”, $str);
}
$data = $this->get_td_array($str);
unset($data[0]);
foreach ($data as $k => $v) {
// dump($v);die;
$arr[‘apid’] = $v[1];
$arr[‘english’] = $v[2];
$arr[‘chinese’] = $v[3];
$arr[‘add_time’] = strtotime($v[4]);
$arr[‘status’] = $v[5];
db(‘ecword_copy’)->insert($arr);
}
}
public function get_td_array($table)
{
$table = preg_replace(“‘<table[^>]*?>’si”, “”, $table);
$table = preg_replace(“‘<tr[^>]*?>’si”, “”, $table);
$table = preg_replace(“‘<td[^>]*?>’si”, “”, $table);
$table = str_replace(“</tr>”, “{tr}”, $table);
$table = str_replace(“</td>”, “{td}”, $table);
//去掉 HTML 标记
$table = preg_replace(“‘<[/!]*?[^<>]*?>’si”, “”, $table);
//去掉空白字符
$table = preg_replace(“‘([rn])[s]+’”, “”, $table);
// $table = str_replace(” “, “”, $table);
// $table = str_replace(” “, “”, $table);
$table = explode(‘{tr}’, $table);
array_pop($table);
foreach ($table as $key => $tr) {
$td = explode(‘{td}’, $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
上面那个方法实现table导入excel到数据库,下面那个是在网上找到的一个函数,是将读取到的table数据进行格式化处理,
得到正常的PHP数组格式数据,然后进行数据库插入操作。
导入和导出数据中间存在了数据长度的细小差异
发表回复