PHP导入Excel文件技巧讲解

开发 后端
PHP导入Excel文件的实现,首先需要将Excel文件转换为CVS文件,然后在运用PHP语言来将其导入。实际操作步骤本文已经为大家详细列出,供参考。

大家对PHP导入Excel文件这个过程的原理应用时有所了解的。我们现在就以一个代码示例来具体讲解PHP导入Excel文件的实现方法。在单位编写导入excel文件时,原来是把excel文件转为cvs文件,在编写PHP程序导入。#t#

PHP导入Excel文件代码示例如下:

  1. < ?php  
  2. require_once '../Excel/reader.php';  
  3. // ExcelFile($filename, $encoding);  
  4. $data = new Spreadsheet_Excel_Reader();  
  5. // Set output Encoding.指定中文码  
  6. $data->setOutputEncoding('gb2312');  
  7. /***  
  8. * if you want you can change '
    iconv' to mb_convert_encoding:  
  9. * $data->setUTFEncoder('mb');  
  10. **//***  
  11. * By default rows & cols indeces start with 1  
  12. * For change initial index use:  
  13. * $data->setRowColOffset(0);  
  14. **/  
  15. /***  
  16. * Some function for formatting output.  
  17. * $data->setDefaultFormat('%.2f');  
  18. * setDefaultFormat - set format for 
    columns with unknown formatting  
  19. * $data->setColumnFormat(4, '%.3f');  
  20. * setColumnFormat - set format for
     column (apply only to number fields)  
  21. **/  
  22. // 指定读取的excel文件  
  23. $data->read('../uploads/jsxz01.xls');  
  24. /*  
  25. $data->sheets[0]['numRows'] - count rows  
  26. $data->sheets[0]['numCols'] - count columns  
  27. $data->sheets[0]['cells'][$i][$j] - data 
    from $i-row $j-column  
  28. $data->sheets[0]['cellsInfo'][$i][$j] - 
    extended info about cell  
  29. $data->sheets[0]['cellsInfo'][$i][$j]['type']
     = "date" | "number" | "unknown"  
  30. if 'type' == "unknown" - use 'raw' value, 
    because cell contain value with format '0.00';  
  31. $data->sheets[0]['cellsInfo'][$i][$j]['raw'] 
    = value if cell without format   
  32. $data->sheets[0]['cellsInfo'][$i][$j]['colspan']   
  33. $data->sheets[0]['cellsInfo'][$i][$j]['rowspan']   
  34. */  
  35. error_reporting(E_ALL ^ E_NOTICE);  
  36. // 循环读取每一个单元值  
  37. for ($i = 1; $i < = $data->sheets[0]
    ['numRows']; $i++) {  
  38. for ($j = 1; $j < = $data->sheets[0]
    ['numCols']; $j++) {  
  39. echo "\"".$data->sheets[0]['cells']
    [$i][$j]."\",";  
  40. }  
  41. echo "< br>";  
  42. }  
  43. //print_r($data);  
  44. //print_r($data->formatRecords);  
  45. ?> 

希望上面这段PHP导入Excel文件的实现代码可以帮助我们对这一知识点有一个深刻的认识。

责任编辑:曹凯 来源: 网易博客
相关推荐

2009-12-11 17:57:13

PHP应用JSON

2009-12-10 17:37:28

PHP Cookie登

2009-12-09 13:32:08

PHP zend安装

2009-11-27 16:20:22

PHP递归调用

2009-11-27 13:24:20

PHP代码性能优化

2009-11-30 09:21:39

PHP函数rmdir(

2009-12-09 16:53:45

PHP打印任意时间

2009-12-10 14:27:21

PHP处理Word转P

2009-12-01 19:17:48

PHP开发高效WEB系

2009-12-11 14:21:57

PHP获取字段属性

2009-11-30 15:10:46

PHP substr函

2010-02-06 15:15:31

C++ Builder

2009-09-01 14:39:47

C#创建Excel文件

2010-04-27 13:31:31

2009-12-11 14:11:03

PHP获取字段数目

2009-12-21 14:49:27

2009-12-04 16:49:38

PHP导出Excel乱

2009-12-10 10:30:31

PHP删除复制文件

2009-12-10 10:24:24

PHP写入文件

2009-11-30 11:24:49

PHP将EXCEL导入
点赞
收藏

51CTO技术栈公众号