PHP导入导出Excel文件的方法
借助PHP-ExcelReader这个开源类,我们可以轻松地导入Excel文件数据,示例代码如下:
PHP-ExcelReader下载地址:http://sourceforge.net/projects/phpexcelreader/
例.导入Excel文件
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1978')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1978><?php
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('gbk');
$data->read('test.xls');
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i ) {
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j ) {
echo """.$data->sheets[0]['cells'][$i][$j]."",";
}
echo "n";
}
?>
例.phpexcel导到excel
1. test.php
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1577')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1577>require_once 'reader.php';
// ExcelFile($filename, $encoding);
$data = new Spreadsheet_Excel_Reader();
// Set output Encoding.
$data->setOutputEncoding('gbk');
//”data.xls”是指要导入到mysql中的excel文件
$data->read('data.xls');
@ $db = mysql_connect('localhost', 'root', '123456') or
die("Could not connect to database.");//连接数据库
mysql_query("set names 'gbk'");//输出中文
mysql_select_db('mydb'); //选择数据库
error_reporting(E_ALL ^ E_NOTICE);
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i ) {
//以下注释的for循环打印excel表数据
/*
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j ) {
echo """.$data->sheets[0]['cells'][$i][$j]."",";
}
echo "n";
//PHP开源代码
*/
//以下代码是将excel表数据【3个字段】插入到mysql中,根据你的excel表字段的多少,改写以下代码吧!
$sql = "INSERT INTO test VALUES('".
$data->sheets[0]['cells'][$i][1]."','".
$data->sheets[0]['cells'][$i][2]."','".
$data->sheets[0]['cells'][$i][3]."')";
echo $sql.'
';
$res = mysql_query($sql);
}
?>
例.导出excel文件
比如我需要一个做php导出 excel的的程序,只需要把相关的数据导出到excel表就可以了,这么简单的操作就不需要用那些类库什么的了。直接用header的方式就可以了:header("Content-type:application/vnd.ms-excel");
看看代码:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4928')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4928><?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=Export_test.xls");
$tab="t"; $br="n";
$head="编号".$tab."备注".$br;
//输出内容如下:
echo $head.$br;
echo "test321318312".$tab;
echo "string1";
echo $br;
echo "330181199006061234".$tab; //直接输出会被Excel识别为数字类型
echo "number";
echo $br;
echo "="330181199006061234"".$tab; //原样输出需要处理
echo "string2";
echo $br;
?>
在导出后会发现一个问题,如果数据是数字会出现一些意想不到的情况。比如,"012345",在excel中会变成"12345";如果输入身份证号码这样的长数字,在excel中会用科学计数法表示出来,并且最后的四位数字会出现偏差,变位0000等情况。这就需要把单元格设置为文本格式,方法是
echo "="330181199006061234""如果程序是utf-8编码的,还需要用iconv函数去转码,不然是会乱码的,乱码的。
另word格式导入类似,,指定header就可以了:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7767')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7767>header("Content-Type: application/msword");
header("Content-Disposition: attachment; filename=doc.doc");
您可能感兴趣的文章:
php框架laravel excel包使用教程介绍
PHP导入与导出Excel文件的方法
php导入excel php使用phpexcel导入excel文件
php导出word文档与excel表格文件
php导出excel时科学计数法的处理方法
phpexcel导入excel到数据库的代码
phpexcel导出数据的实例代码
解决php下载excel无法打开的问题
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
php导出数据为execel文件的实例分享