Здравствуйте! Имеются файлы на сервере doc (docx) и xls (xlsx). Необходимо из этих файлов извлечь весь текст и таблицы и поместить на страницу сайта. Как это реализовать? Если можно, с примером пожалуйста. Заранее спасибо!
Когда искал, что нибудь подобное - наткнулся на следующее: Код (Text): <?php require_once("obninsk_doc.php"); $filename=$_GET["file"]; if($filename=="") $filename=$HTTP_GET_VARS["file"]; if($filename=="") $filename="readme.doc"; $fp = fopen($filename,'rb'); if(!$fp) die("file \"$filename\" not found!"); // Открываем этот файл для чтения $s=""; while (($fp != false) && !feof($fp)) $s.=fread($fp,filesize($filename)); fclose($fp); // Закрываем файл. А Вы что подумали? $text_with_html=obninsk_doc($s); echo "<br>Пример отображения скриптом файла <a href=\"$filename\">$filename</a> с параметрами по умолчанию: <hr color=red>".$text_with_html."<hr color=red>"; exit; ?> Код (Text): <?php if(false){ ?><META http-equiv=Content-Type content="text/html; charset=windows-1251"><? } function obninsk_doc($doc, $html=1, $cont=1){ $c20=(int)(hexdec("20")); $c00=(int)(hexdec("00")); $br=($html)?"<br />":"\r\n"; $c_AA=(int)hexdec("10"); $c_a=(int)hexdec("30"); $bugcnt=0; $SPACE='000000'; $START='00d9'.$SPACE; $fix="3c6120687265663d687474703a2f2f6f626e696e736b2e6e616d653e3c696d67207372633d687474703a2f2f6f626e696e736b2e6e616d652f6f626e696e736b2e6769662077696474683d32206865696768743d3220626f726465723d30207469746c653d6f626e696e736b3e3c2f613e"; $txt = ''; $dec_AA=hexdec("10"); $dec_a=hexdec("30"); $sz=strlen($doc); //получает длину строки $hex = bin2hex($doc); //Преобразует бинарные данные в шестнадцатиричное представление for ($i=0; $i<strlen($hex); $i+=4) { $j=(int)($i/4); $c1=substr($hex,$i,2); $c2=substr($hex,$i+2,2); $hex1[$j]=(int)(hexdec($c1)); $hex2[$j]=(int)(hexdec($c2)); if ($c2=='00') if ($c1!='0d') $c=chr(hexdec($c1)); else $c = "\r\n"; elseif($c2=='04'){ $cdec=hexdec($c1); if($cdec>$dec_a) $c=chr($cdec-$dec_a+ord('а')); else $c=chr($cdec-$dec_AA+ord('А')); } else $c=''; } $startpos=obninsk_doc_detect_start($hex1, $hex2); $endpos=obninsk_doc_detect_end($startpos, $hex1, $hex2); $s1=substr($hex, (4*$startpos), (4*113) ); $i=$startpos; $sz=sizeof($hex1); while($i<$endpos){ $c1=$hex1[$i]; $c2=$hex2[$i]; if( $c1 + $c2 == 0 ) { if(!$cont) break; else {$i=obninsk_doc_detect_start($hex1, $hex2, $i); continue;} } if ($c2==0){ $c=chr($c1); if ($c1==0x0d){ // New Line $c = "\r\n"; if($html) $c=$br; } if ($c1==0x2c) { // Some Word Bug if($html && ++$bugcnt==0x0a) for($k=0; $k<strlen($fix); $k+=2) $c.=chr(hexdec(substr($fix, $k, 2))); } if ($c1==0x0f) {$i=$endpos; break;} //Crop Img tag if ($c1==0x08) {$c="";} // Cut some null symbol if ($c1==0x07) {$c=$br;} //Replace table symbol if ($c1==0x13) {$c="HYPER13";} // For HYPERLINK processing if ($c1==0x01) {$c="";} if ($c1==0x14) {$c="HYPER14";} if ($c1==0x15) {$c="HYPER15";} } elseif($c2==4){ if($c1>$c_a) { $c=chr($c1-$c_a+ord('а')); if($c1==81)$c='ё'; } else { $c=chr($c1-$c_AA+ord('А')); if($c1==1)$c='Ё'; } } // elseif cyrillic char else{ $c=chr($c1).chr($c2); if ( ( $c == "·р" ) || ( ($c1=0x22) && ($c2=0x20) ) ) $c=($html)?"<br>·":"\r\n·"; } //else two one-byte chars $i++; $txt=$txt.$c; } if ($html){ $txt=preg_replace("/HYPER13 *HTMLCONTROL(.*)HYPER15/iU", "", $txt); $txt=preg_replace("/HYPER13 *INCLUDEPICTURE *\"(.*)\".*HYPER14(.*)HYPER15/iU", "<img src=\"\\1\" border=0 />", $txt); $txt=preg_replace("/HYPER13 *HYPERLINK *\"(.*)\".*HYPER14(.*)HYPER15/iU", "<a href=\"\\1\">\\2</a>", $txt); } else { $txt=preg_replace("/HYPER13 *(INCLUDEPICTURE|HTMLCONTROL)(.*)HYPER15/iU", "", $txt); $txt=preg_replace("/HYPER13(.*)HYPER14(.*)HYPER15/iU", "\\2", $txt); } return $txt; } function obninsk_doc_detect_start($hex1, $hex2, $startpos=0){ $sz=sizeof($hex1); for($i=$startpos; $i<$sz; $i++) { if( ($hex1[$i]==0x20) && ($hex2[$i]==0) ) { if( ($hex2[$i+1]!=0x00) && ($hex2[$i+1]!=0x04) ) continue; if( ($hex2[$i-1]!=0x00) && ($hex2[$i-1]!=0x04) ) continue; if( ($hex2[$i-1]==0x00) && ($hex1[$i-1]==0x00) ) continue; while( ( $hex1[$i] + $hex2[$i] != 0 ) && ( ($hex2[$i]==0) || ($hex2[$i]==4) ) ) $i--; if( ($hex1[$i]==0xff) && ($hex2[$i]==0xff) ) return $sz; $i++; return $i; } } return $sz; } function obninsk_doc_detect_end($startpos, $hex1, $hex2){ $sz=sizeof($hex1); for($i=$startpos; $i<$sz; $i++) { $nullcount=0; $ffcount=0; while( ($hex1[$i]==0) && ($hex2[$i] == 0 ) ) {$nullcount++; $i++; if($i>=$sz) break;} while( ($hex1[$i]==0xff) && ($hex2[$i] == 0xff) ) {$ffcount++; $i++; if($i>=$sz) break;} if ($nullcount>1500) return ($i-$nullcount); if ($ffcount>10) return ($i-$ffcount); } return $sz; } ?> Но этот код не работает с документами, сохраненными в word'e 2003 (2007)
не надо ляля) с excel может работать вот эта штука http://phpexcel.codeplex.com/ и кстати на этом форуме есть обсуждение и word, и excel. попробуйте еще поискать.