PHP用Excel模拟表单上传数据保存到MYSQL

PHP 1

黑科技.png

1、用EXCEL来展开多少的传输,首先必要下载一个插件PHPExcel。可以到官网上下载。
2、思路总计:

用Excel进行上传,再把数量保存到数据库,一般意况下没有大的标题,但是有些表可能牵涉到七个表和无数算法,不难严酷的第一手导入,会让洋洋数目没发衔接,没有实际的职能。那里还要进行的操作是开展模拟提交。思路大致就是如此。

3、代码操作如下:

PHP:
<code>
header(“content-type:text/html;charset=utf-8”);
set_time_limit(0);
function read($filename,$encode=’utf-8′){
include_once(‘./PHPexcel/PHPExcel.php’);
$objReader = PHPExcel_IOFactory::createReader(‘Excel5’);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex =
PHPExcel_Cell::columnIndexFromString($highestColumn);
$temp = array();
for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行初叶
for ($column = 0; $column < $highestColumnIndex; $column++)
{//列数是以A列始发
$temp[$row][]
=$objWorksheet->getCellByColumnAndRow($column,$row)->getValue();
}
}
return $temp;
}
</code>

HTML:
<code>
<html>
<form action=”./index.php” method=”post”>
<input type=”text” name=”filename” placeholder=”请输入文件名称”
value=”<?php if(!empty($_PHP,POST[‘filename’])) echo
$_POST[‘filename’]; ?>” />
<input type=”submit” value=”提交”/>
</form>
</html>
</code>

PHP:

<code>
if(!empty($_POST[‘filename’])){
$filename = trim($_POST[‘filename’]);
$extension = substr(strrchr($filename, ‘.’), 1);
if($extension != ‘xls’){
exit(‘文件格式错误’);
}
if(!file_exists($filename)){
exit(‘文件不存在’);
}
$conn = mysqli_connect(‘localhost’,’root’,”) or die(‘连接战败’);
mysqli_select_db($conn,’wgw2′);
mysqli_set_charset($conn,’utf8′);
$a = read($filename);
$str = ”;
$i = 0;
$j = 0;
foreach($a as $k=>$v){
if($k==1) continue;
$arr = array();
$arr[‘cateid’] = $v[0];
$arr[‘brand’] = $v[1];
$arr[‘sgid’] = $v[2];
$arr[‘title’] = $v[3];
$arr[‘title2’] = empty($v[4]) ? ” : $v[4];
$arr[‘title_style_color’] = empty($v[5]) ? ” : $v[5];
$arr[‘title_style_bold’] = empty($v[6]) ? ” : $v[6];
$arr[‘keywords’] = empty($v[7]) ? ” : $v[7];
$arr[‘description’] = empty($v[8]) ? ” : $v[8];
$arr[‘money’] = $v[9];
$arr[‘yunjiage’] = $v[10];
$arr[‘maxqishu’] = $v[11];
$arr[‘thumb’] = $v[12];
$arr[‘uppicarr’] = empty($v[13])? null :
json_encode(explode(‘|’,$v[13]));
$arr[‘goods_key’] = json_encode(array(‘pos’ => $v[14],’renqi’
=> $v[15]));
$arr[‘xsjx_time’] = $v[16];
$arr[‘xsjx_time_h’] = $v[17];
$arr[‘content’] = str_replace(“‘”,'”‘,$v[18]);
if ($v[9]< 30) {
}else{
if ($v[9] >50) {
$arr[‘money’] = $v[9] + 5;
}else{
$arr[‘money’] = $v[9] * 1.2;
}
$bb =
http_request(‘http://yungou.d8qb.com/index.php/admin/content/goods\_add’,$arr);
$reg = “/商品丰盛成功/”;
if(preg_match($reg,$bb)){
$j++;
}else{
$str .= $k . ‘,’;
}
$i++;
}
}
echo ‘导入完结’;
echo ‘
‘;
echo ‘共’.$i.’个’;
echo ‘
‘;
echo ‘成功’.$j.’个’;
echo ‘
‘;
echo ‘失败编号’.$str;
</code>

相关文章