首页 > 开发 > PHP > 正文

php ExcelReader 读取Excel类

2024-05-04 21:59:08
字体:大 中 小
来源:转载
供稿:网友

这是一款开源的php代码,ExcelReader是专业地excel表进行读写操作的,下面我们来看一款ExcelReader读取Vevb.com.xls工作表实例吧,代码如下:

  1. $allow_url_override = 1; // set to 0 to not allow changed via post or get 
  2. if(!$allow_url_override || !isset($file_to_include)) 
  3. { 
  4.  $file_to_include = "111cn.net.xls"; 
  5. } 
  6. if(!$allow_url_override || !isset($max_rows)) 
  7. { 
  8.  $max_rows = 0; //use 0 for no max 
  9. } 
  10. if(!$allow_url_override || !isset($max_cols)) 
  11. { 
  12.  $max_cols = 5; //use 0 for no max 
  13. } 
  14. if(!$allow_url_override || !isset($debug)) 
  15. { 
  16.  $debug = 0;  //1 for on 0 for off 
  17. } 
  18. if(!$allow_url_override || !isset($force_nobr)) 
  19. { 
  20.  $force_nobr = 1;  //force the info in cells not to wrap unless stated explicitly (newline) 
  21. } 
  22. require_once 'www.Vevb.com/reader.php'; 
  23. $data = new spreadsheet_excel_reader(); 
  24. $data->setoutputencoding('cpa25a'); 
  25. $data->read($file_to_include); 
  26. error_reporting(e_all ^ e_notice); 
  27. echo " 
  28. <style> 
  29. .table_data 
  30. { 
  31.  border-style:ridge; 
  32.  border-width:1; 
  33. } 
  34. .tab_base 
  35. { 
  36.  background:#c5d0dd; 
  37.  font-weight:bold; 
  38.  border-style:ridge; 
  39.  border-width:1; 
  40.  cursor:pointer; 
  41. } 
  42. .table_sub_heading 
  43. { 
  44.  background:#cccccc; 
  45.  font-weight:bold; 
  46.  border-style:ridge; 
  47.  border-width:1; 
  48. } 
  49. .table_body 
  50. { 
  51.  background:#f0f0f0; 
  52.  font-wieght:normal; 
  53.  font-size:12; 
  54.  font-family:sans-serif; 
  55.  border-style:ridge; 
  56.  border-width:1; 
  57.  border-spacing: 0px; 
  58.  border-collaps教程e: collapse; 
  59. } 
  60. .tab_loaded 
  61. { 
  62.  background:#222222; 
  63.  color:white; 
  64.  font-weight:bold; 
  65.  border-style:groove; 
  66.  border-width:1; 
  67.  cursor:pointer; 
  68. } 
  69. </style> 
  70. "; 
  71. function make_alpha_from_numbers($number) 
  72. { 
  73.  $numeric = "abcdefghijklmnopqrstuvwxyz"; 
  74.  if($number<strlen($numeric)) 
  75.  { 
  76.   return $numeric[$number]; 
  77.  } 
  78.  else 
  79.  { 
  80.   $dev_by = floor($number/strlen($numeric)); 
  81.   return "" . make_alpha_from_numbers($dev_by-1) . make_alpha_from_numbers($number-($dev_by*strlen($numeric))); 
  82.  } 
  83. } 
  84. echo "<script language='javascript'> 
  85. var sheet_html = array(); "; 
  86. for($sheet=0;$sheet<count($data->sheets);$sheet++) 
  87. { 
  88.  $table_output[$sheet] .= "<table class='table_body'> 
  89.  <tr> 
  90.   <td>&nbsp;</td>"; 
  91.  for($i=0;$i<$data->sheets[$sheet]['numcols']&&($i<=$max_cols||$max_cols==0);$i++) 
  92.  { 
  93.   $table_output[$sheet] .= "<td class='table_sub_heading' align=center>" . make_alpha_from_numbers($i) . "</td>"; 
  94.  } 
  95.  for($row=1;$row<=$data->sheets[$sheet]['numrows']&&($row<=$max_rows||$max_rows==0);$row++) 
  96.  { 
  97.   $table_output[$sheet] .= "<tr><td class='table_sub_heading'>" . $row . "</td>"; 
  98.   for($col=1;$col<=$data->sheets[$sheet]['numcols']&&($col<=$max_cols||$max_cols==0);$col++) 
  99.   { 
  100.    if($data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'] >=1 && $data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'] >=1) 
  101.    { 
  102.     $this_cell_colspan = " colspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan']; 
  103.     $this_cell_rowspan = " rowspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan']; 
  104.     for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];$i++) 
  105.     { 
  106.      $data->sheets[$sheet]['cellsinfo'][$row][$col+$i]['dontprint']=1; 
  107.     } 
  108.     for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'];$i++) 
  109.     { 
  110.      for($j=0;$j<$data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];$j++) 
  111.      { 
  112.       $data->sheets[$sheet]['cellsinfo'][$row+$i][$col+$j]['dontprint']=1; 
  113.      } 
  114.     } 
  115.    } 
  116.    else if($data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'] >=1) 
  117.    { 
  118.     $this_cell_colspan = " colspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan']; 
  119.     $this_cell_rowspan = ""; 
  120.     for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];$i++) 
  121.     { 
  122.      $data->sheets[$sheet]['cellsinfo'][$row][$col+$i]['dontprint']=1; 
  123.     } 
  124.    } 
  125.    else if($data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'] >=1) 
  126.    { 
  127.     $this_cell_colspan = ""; 
  128.     $this_cell_rowspan = " rowspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan']; 
  129.     for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'];$i++) 
  130.     { 
  131.      $data->sheets[$sheet]['cellsinfo'][$row+$i][$col]['dontprint']=1; 
  132.     } 
  133.    } 
  134.    else 
  135.    { 
  136.     $this_cell_colspan = ""; 
  137.     $this_cell_rowspan = ""; 
  138.    } 
  139.    if(!($data->sheets[$sheet]['cellsinfo'][$row][$col]['dontprint'])) 
  140.    { 
  141.     $table_output[$sheet] .= "<td class='table_data' $this_cell_colspan $this_cell_rowspan>&nbsp;"; 
  142.     if($force_nobr) 
  143.     { 
  144.      $table_output[$sheet] .= "<nobr>"; 
  145.     } 
  146.     $table_output[$sheet] .= nl2br(htmlentities($data->sheets[$sheet]['cells'][$row][$col])); 
  147.     if($force_nobr) 
  148.     { 
  149.      $table_output[$sheet] .= "</nobr>"; 
  150.     } 
  151.     $table_output[$sheet] .= "</td>"; 
  152.    } 
  153.   } 
  154.   $table_output[$sheet] .= "</tr>"; 
  155.  } 
  156.  $table_output[$sheet] .= "</table>"; 
  157.  $table_output[$sheet] = str_replace(" ","",$table_output[$sheet]); 
  158.  $table_output[$sheet] = str_replace(" ","",$table_output[$sheet]); 
  159.  $table_output[$sheet] = str_replace(" "," ",$table_output[$sheet]); 
  160.  if($debug) 
  161.  { 
  162.   $debug_output = print_r($data->sheets[$sheet],true); 
  163.   $debug_output = str_replace(" ","/n",$debug_output); 
  164.   $debug_output = str_replace(" ","/r",$debug_output); 
  165.   $table_output[$sheet] .= "<pre>$debug_output</pre>"; 
  166.  } 
  167.  echo "sheet_html[$sheet] = "$table_output[$sheet]"; "; 
  168. } 
  169. echo " 
  170. function change_tabs(sheet) 
  171. { 
  172.  //alert('sheet_tab_' + sheet); 
  173.  for(i=0;i<" , count($data->sheets) , ";i++) 
  174.  { 
  175.   document.getelementbyid('sheet_tab_' + i).classname = 'tab_base'; 
  176.  } 
  177.  document.getelementbyid('table_loader_div').innerhtml=sheet_html[sheet]; 
  178.  document.getelementbyid('sheet_tab_' + sheet).classname = 'tab_loaded'; 
  179. } 
  180. </script>"; 
  181. echo " 
  182. <table class='table_body' name='tab_table'> 
  183. <tr>"; 
  184. for($sheet=0;$sheet<count($data->sheets);$sheet++) 
  185. { 
  186.  echo "<td class='tab_base' id='sheet_tab_$sheet' align=center 
  187.   onmousedown="change_tabs($sheet);">", $data->boundsheets[$sheet]['name'] , "</td>"; 
  188. } 
  189. echo  
  190. "<tr>"; 
  191. echo "</table> 
  192. <div id=table_loader_div>www.Vevb.com</div> 
  193. <script language='javascript'>//开源代码Vevb.com 
  194. change_tabs(0); 
  195. </script>"; 
  196. //echo "<iframe name=table_loader_iframe src='about:blank' width=100 height=100></iframe>"; 
  197. /* 
  198. echo "<pre>"; 
  199. print_r($data); 
  200. echo "</pre>"; 
  201. */ 

excelreader这个类你可以到官网去下载.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表