使用php-excel-reader读取excel文件

Love The Way You Lie 2022-09-22 04:58 399阅读 0赞

有时候如果有大量的数据需要导入到数据库,最低级的办法就是,一个一个的手动添加,而日常生活中,常常用表格来记录,能不能让PHP直接读取一个excel表格,然后,将表格中的内容,全部导入数据库呢,这样子,可以节省大量的时间。

php-excel-reader是一个读取excel的类,可以很轻松的使用它读取excel文件。

首先要下载有关的文件:

链接:http://pan.baidu.com/s/1i5990hv 密码:4npd

Center

其余文件为事例文件,请认真分析源码。

表格对应内容:

Center 1

1:引入类,创建对象,设置读取文件的目录

  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. require_once 'excel_reader2.php';
  4. $data = new Spreadsheet_Excel_Reader();//创建对象
  5. $data->setOutputEncoding('UTF-8');//设置编码格式
  6. $data->read("example.xls");//读取excel文档

2:读取完毕后,会将表格有关的信息,全部存到一个大数组中。

  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. require_once 'excel_reader2.php';
  4. $data = new Spreadsheet_Excel_Reader();//创建对象
  5. $data->setOutputEncoding('UTF-8');//设置编码格式
  6. $data->read("example.xls");//读取excel文档
  7. echo "<pre>";
  8. print_r($data->sheets);
  9. echo "</pre>";

运行结果如下
Center 2

3:如果要读取,数组中的详细内容,给出几个例子

  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. require_once 'excel_reader2.php';
  4. $data = new Spreadsheet_Excel_Reader();//创建对象
  5. $data->setOutputEncoding('UTF-8');//设置编码格式
  6. $data->read("example.xls");//读取excel文档
  7. //echo "<pre>";
  8. //print_r($data->sheets);
  9. //echo "</pre>";
  10. echo $data->sheets[0]['numRows']."行<br>";//读出一共几行
  11. echo $data->sheets[0]['numCols']."列<br>";//读出一共几列
  12. echo $data->sheets[0]['cells'][1][1]."<br>";//读出第一行第一列的内容
  13. print_r($data->sheets[0]['cells'][1]);//第一行的数据
  14. echo "<br>";
  15. echo implode(",",$data->sheets[0]['cells'][1])."<br>";//去除第一行的数据,每个中间添加分隔符
  16. for($i=1;$i<=$data->sheets[0]['numCols'];$i++)//一次读出第一行的所有数据
  17. {
  18. echo $data->sheets[0]['cells'][1][$i]." ";
  19. }
  20. echo "<br>";
  21. echo "<br>";
  22. //读出所有数据
  23. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
  24. //$data->sheets[0]['numCols']为Excel列数
  25. for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
  26. //显示每个单元格内容
  27. echo $data->sheets[0]['cells'][$i][$j].' ';
  28. }
  29. echo '<br>';
  30. }

Center 3

注意上述,echo implode(“,”,$data->sheets[0][‘cells’][1]).”
“;//去除第一行的数据,每个中间添加分隔符,,的应用,这样就可以,直接向数据库插入一整行的数据了

注:

dump(),它可以将excel内容以html格式输出:

echo $data->dump(true,true);

  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. require_once 'excel_reader2.php';
  4. $data = new Spreadsheet_Excel_Reader("example.xls");
  5. ?>
  6. <html>
  7. <head>
  8. <style>
  9. table.excel {
  10. border-style:ridge;
  11. border-width:1;
  12. border-collapse:collapse;
  13. font-family:sans-serif;
  14. font-size:12px;
  15. }
  16. table.excel thead th, table.excel tbody th {
  17. background:#CCCCCC;
  18. border-style:ridge;
  19. border-width:1;
  20. text-align: center;
  21. vertical-align:bottom;
  22. }
  23. table.excel tbody th {
  24. text-align:center;
  25. width:20px;
  26. }
  27. table.excel tbody td {
  28. vertical-align:bottom;
  29. }
  30. table.excel tbody td {
  31. padding: 0 3px;
  32. border: 1px solid #EEEEEE;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <?php echo $data->dump(true,true); ?>
  38. </body>
  39. </html>

Center 4

发表评论

表情:
评论列表 (有 0 条评论,399人围观)

还没有评论,来说两句吧...

相关阅读