php中使用phpexcel操作excel(xls)文件,读取中文的xls、csv文件会有问题,网上找了下资料,发现phpexcel类库好用,官网地址:http://phpexcel.codeplex.com/
1、读取xls文件内容
代码如下 复制代码
getproperties()->setcreator(云舒)
// ->setlastmodifiedby(云舒)
// ->settitle(产品url导出)
// ->setsubject(产品url导出)
// ->setdescription(产品url导出)
// ->setkeywords(产品url导出);
$objphpexcel->setactivesheetindex(0);
$cols = 'abcdefghijklmnopqrstuvwxyz';
//设置www.111cn.net标题
for($i=0,$length=count($title); $i //echo $cols{$i}.'1';
$objphpexcel->getactivesheet()->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'
)
)
)
);
$i = 0;
foreach($data as $d) { //这里用foreach,支持关联数组和数字索引数组
$j = 0;
foreach($d as $v) { //这里用foreach,支持关联数组和数字索引数组
$objphpexcel->getactivesheet()->setcellvalue($cols{$j}.($i+2), $v);
$j++;
}
$i++;
}
// 生成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');
?>
2、向xls文件写内容
代码如下 复制代码
'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; //得到的是gbk码(同数据库编码)
}
$id = getproductidbyname('%伊奈卫浴伊奈分体座便器%');
var_dump($id);
?>
ok...
相关内容2014.05.28phpexcel导入excel表格生成数组2014.04.16thinkphp调用phpexcel导出excel文件到本地的例子2014.04.11thinkphp利用phpexcel实现导入excel2014.04.09thinkphp中用phpexcel导入导出excel文件2014.03.19php通过phpexcel类导入导出excel2013.11.24php中利用phpexcel导出excel示例2013.08.13phpexcel生成和读取excel文件实例程序2013.08.13yii中使用phpexcel导出excel实例代码2013.05.06phpexcel读取excel并导入mysql数据库代码2013.05.03php excel操作类phpexcel用法介绍
http://www.bkjia.com/phpjc/878340.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/878340.htmltecharticlephp中使用phpexcel操作excel(xls)文件, 读取中文的xls、csv文件会有问题,网上找了下资料,发现phpexcel类库好用,官网地址:http://phpexcel.codep...