phpexcel操作xls文件,读取中文的xls、csv文件会有问题,网上找了下资料,发现phpexcel类库好用,官网地址:http://phpexcel.codeplex.com/
1、读取xls文件内容setreaddataonly(true); $xlsreader->setloadsheetsonly(true); $sheets = $xlsreader->load($file); $content = $sheets->getsheet(0)->toarray(); //读取第一个工作表(注意编号从0开始) 如果读取多个可以做一个循环0,1,2,3.... //得到二维数组,每个小数组是excel表格内容的一行 里面包含此行的每列的数据 return $content; } //$type = 'excel2007'; //设置要解析的excel类型 excel5(2003或以下版本)或excel2007 $type = 'excel5'; $content = readxls('data.xls', $type); echo ''; var_dump($content); echo '
'; ?>2、向xls文件写内容
getproperties()->setcreator(云舒) // ->setlastmodifiedby(云舒) // ->settitle(产品url导出) // ->setsubject(产品url导出) // ->setdescription(产品url导出) // ->setkeywords(产品url导出); $objphpexcel->setactivesheetindex(0); $cols = 'abcdefghijklmnopqrstuvwxyz'; //设置标题 for($i=0,$length=count($title); $igetactivesheet()->setcellvalue($cols{$i}.'1', $title[$i]); } //设置标题样式 $titlecount = count($title); $r = $cols{0}.'1'; $c = $cols{$titlecount}.'1'; $objphpexcel->getactivesheet()->getstyle($r:$c)->applyfromarray( array( 'font' => array( 'bold' => true ), 'alignment' => array( 'horizontal' => phpexcel_style_alignment::horizontal_right, ), 'borders' => array( 'top' => array( 'style' => phpexcel_style_border::border_thin ) ), 'fill' => array( 'type' => phpexcel_style_fill::fill_gradient_linear, 'rotation' => 90, 'startcolor' => array( 'argb' => 'ffa0a0a0' ), 'endcolor' => array( 'argb' => 'ffffffff' ) ) ) ); for($i=0,$length=count($data); $igetactivesheet()->setcellvalue($cols{$j}.($i+2), $v); $j++; } } // 生成2003excel格式的xls文件 header('content-type: application/vnd.ms-excel'); header('content-disposition: attachment;filename='.$filename.'.xls'); header('cache-control: max-age=0'); $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); $objwriter->save('php://output'); } $array = array( array(1111,'名称','品牌','商品名','http://www.baidu.com'), array(1111,'名称','品牌','商品名','http://www.baidu.com'), array(1111,'名称','品牌','商品名','http://www.baidu.com'), array(1111,'名称','品牌','商品名','http://www.baidu.com'), array(1111,'名称','品牌','商品名','http://www.baidu.com'), ); write_xls($array,array('商品id','供应商名称','品牌','商品名','url'),'report'); ?>
3、操作数据库获取要写入的内容举个使用mysqli的预处理获取内容的例子:'mysql', 'db_host'=>'localhost', 'db_name'=>'test', 'db_user'=>'root', 'db_pwd'=>'root', 'db_port'=>'3306', ); function getproductidbyname($name) { global $config; $id = false; $mysqli = new mysqli($config['db_host'], $config['db_user'], $config['db_pwd'], $config['db_name']); if(mysqli_connect_error()) { //兼容 connect_error die(连接失败,错误码:.mysqli_connect_errno().错误信息:.mysqli_connect_error()); } //设置连接数据库的编码,不要忘了设置 $mysqli->set_charset(gbk); //中文字符的编码要与数据库一致,若没设置,结果为null $name = iconv(utf-8, gbk//ignore, $name); if($mysqli_stmt = $mysqli->prepare(select id from 137_product where name like ?)) { $mysqli_stmt->bind_param(s, $name); $mysqli_stmt->execute(); $mysqli_stmt->bind_result($id); $mysqli_stmt->fetch(); $mysqli_stmt->close(); } $mysqli->close(); return $id; } $id = getproductidbyname('%伊奈卫浴伊奈分体座便器%'); var_dump($id);?>
ok...
参考资料:http://blog.sina.com.cn/s/blog_44b3f96d0101cczo.htmlhttp://phpexcel.codeplex.com/
http://www.bkjia.com/phpjc/860057.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/860057.htmltecharticlephpexcel操作xls文件, 读取中文的xls、csv文件会有问题,网上找了下资料,发现phpexcel类库好用,官网地址:http://phpexcel.codeplex.com/ 1、读取...