2010-05-19 154 views
0
  1. 我想打開一個Word文檔並對其進行編輯
  2. 我從服務器打開Word文檔,在那個時候它與垃圾值開(也許它沒有被正確地轉換爲UTF-8)。
  3. 當我刪除這些垃圾值,並從textarea插入到該文件的東西,它將插入,然後在它打開正確。
  4. 我想用文檔中的英文單詞而不是垃圾值打開文檔 - 這只是目前打破的第一個開放。
<? 

$filename = 'test.doc'; 

if(isset($_REQUEST['Submit'])){ 
    $somecontent = stripslashes($_POST['somecontent']); 
    // Let's make sure the file exists and is writable first. 
    if (is_writable($filename)) { 

     // In our example we're opening $filename in append mode. 
     // The file pointer is at the bottom of the file hence 
     // that's where $somecontent will go when we fwrite() it. 
     if (!$handle = fopen($filename, 'w')) { 
      echo "Cannot open file ($filename)"; 
      exit; 
     } 

     // Write $somecontent to our opened fi<form action="" method="get"></form>le. 
     if (fwrite($handle, $somecontent) === FALSE) { 
      echo "Cannot write to file ($filename)"; 
      exit; 
     } 

     echo "Success, wrote ($somecontent) to file ($filename) <a href=".$_SERVER['PHP_SELF']."> - Continue - "; 

     fclose($handle); 

    } else { 
     echo "The file $filename is not writable"; 
    } 
} else { 
    // get contents of a file into a string 

    $handle = fopen($filename, 'r'); 

    $somecontent = fread($handle, filesize($filename)); 


    ?> 
<h1>Edit file <? echo $filename ;?></h1> 
<form name="form1" method="post" action=""> 
<p> 
<textarea name="somecontent" cols="80" rows="10"><? echo $somecontent ;?></textarea> 
</p> 
<p> 
<input type="submit" name="Submit" value="Submit"> 
</p> 
</form> 

<? 
    fclose($handle); 
} 
?> 

回答

4

Word文檔不僅僅是一個文本文件,因此您不能像寫入文本文件一樣寫入它。

您也可以看看codeplex上的PHPWord項目,它是一個用於在純PHP中編寫Word文檔的庫,不需要COM,儘管目前它只支持創建新的Word文檔。