Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php COM在word中创建两个表(“word.application”)_Php_Ms Word_Com - Fatal编程技术网

使用php COM在word中创建两个表(“word.application”)

使用php COM在word中创建两个表(“word.application”),php,ms-word,com,Php,Ms Word,Com,我写了一个创建word文档的代码。 我的意图是创建两个独立的表,但我不能这样做:第一个表是在第二个表中创建的。 代码如下: <?php $word= new COM("word.application") or die("Unable to create Word document"); // Hide MS Word application window $word->Visible = 0; //Create new document $WrdDoc = $word-&g

我写了一个创建word文档的代码。 我的意图是创建两个独立的表,但我不能这样做:第一个表是在第二个表中创建的。 代码如下:

<?php   
$word= new COM("word.application") or die("Unable to create Word document"); 
// Hide MS Word application window
$word->Visible = 0;
//Create new document
$WrdDoc = $word->Documents->Add();
// Define page margins 
$word->Selection->PageSetup->LeftMargin = '2';
$word->Selection->PageSetup->RightMargin = '2';
// Define font settings
$word->Selection->Font->Name = 'Times New Roman';
$word->Selection->Font->Size = 18;
// Add text
$word->Selection->ParagraphFormat->Alignment = 1;
$word->Selection->TypeText("Questo e' il PDP!\n");

$materia[0] = "Italiano";
$materia[1] = "Storia";
$strumenti_compensativi[0] = "Mappe concettuali";
$strumenti_compensativi[1] = "Calcolatrice";
$misure_dispensative[0] = "Lettura ad alta voce";
$misure_dispensative[1] = "Scrittura sotto dettatura";

for( $i=0; $i<2; $i++ ) {
    $word->Selection->Font->Name = 'Times New Roman';
    $word->Selection->Font->Size = 16;
    $word->Selection->TypeText("$materia[$i]\n");

    $WTable = $WrdDoc->Tables->Add($word->Selection->Range, 2, 2); // Colums, Rows
    $WrdDoc->Tables[1]->Borders->InsideLineStyle=1; 
    $WrdDoc->Tables[1]->Borders->OutsideLineStyle = 1;

    $WTable->Cell(1,1)->Width = "3"; 
    $WTable->Cell(1,2)->Width = "12";   
    $WTable->Cell(1,1)->Range->Font->Name = "Times New Roman";
    $WTable->Cell(1,1)->Range->Shading->BackgroundPatternColor = hexdec ( "00ff00" );
    $WTable->Cell(1,1)->Range->Font->Size = 12;
    $WTable->Cell(1,1)->Range->Bold = False;
    $WTable->Cell(1,1)->Range->Font->Italic = False;
    $WTable->Cell(1,1)->Range->Text = "Strumenti compensativi";
    $WTable->Cell(1,2)->Range->Font->Size = 12;
    $WTable->Cell(1,2)->Range->Bold = False;
    $WTable->Cell(1,2)->Range->Font->Italic = False;
    $WTable->Cell(1,2)->Range->Text = "$strumenti_compensativi[$i]"; 

    $WTable->Cell(2,1)->Width = "3"; 
    $WTable->Cell(2,2)->Width = "12";   
    $WTable->Cell(2,1)->Range->Font->Name = "Times New Roman";
    $WTable->Cell(2,1)->Range->Shading->BackgroundPatternColor = hexdec ( "00ff00" );
    $WTable->Cell(1,1)->Range->Font->Size = 12;
    $WTable->Cell(1,1)->Range->Bold = False;
    $WTable->Cell(1,1)->Range->Font->Italic = False;
    $WTable->Cell(2,1)->Range->Text = "Misure dispensative";
    $WTable->Cell(2,2)->Range->Font->Size = 12;
    $WTable->Cell(2,2)->Range->Bold = False;
    $WTable->Cell(2,2)->Range->Font->Italic = False;
    $WTable->Cell(2,2)->Range->Text = "$misure_dispensative[$i]";       

}

// Save document
$filename = tempnam(sys_get_temp_dir(), "word");
$word->Documents[1]->SaveAs($filename);

$file="ilMiofile.doc";

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment; filename="ilMiofile.doc"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
unlink($filename);

// Close and quit
$word->quit();
unset($word);

echo "done"; 
?>

我不使用php,但该语言似乎使用了我熟悉的Word对象模型。。。由于您还没有收到回复,我将尝试描述一个方法,但我展示的代码是基于您发布的内容的理论扩展-我无法测试它

如果您使用
范围
对象,这会有所帮助。将表的
范围
指定给范围对象。在范围末尾插入一段(每个单词表后面必须有一段)。然后将范围折叠到其端点,为插入第二个表提供“目标”

如果我正确地解释了php语法,那么会是这样的:

$tableRange = $WTable->Range;
$tableRange->InsertAfter(ANSI-13); //Substitute php code for ANSI 13 character
$tableRange->Collapse(0);          // Word.WdCollapseDirection.wdCollapseEnd
$WTable2 = $WrdDoc->Tables->Add($tableRange, 2, 2); 

首先谢谢。然后我可以告诉你,你的直觉是正确的,因为这是一个word命令的问题,没有绑定到php。。。所以我通过健康指挥部和收尾键决定。。。还是谢谢你,不客气@Federick。。。FWIW模仿用户动作(按键)通常不是实现单词自动化的最佳方式。使用
范围
通常是首选。