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

问题描述 投票:0回答:1

我写了一个创建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 ms-word com
1个回答
0
投票

我不使用php,但看起来语言使用Word的对象模型,我很熟悉...因为你还没有收到回复,我会尝试描述一个方法,但是我展示的代码是基于你发布的内容的理论扩展 - 我无法测试它。

如果你使用Range对象会有所帮助。将表的Range分配给Range对象。在范围的末尾插入一个段落(每个Word表后面都必须跟一个段落)。然后将Range折叠到其终点,为插入第二个表提供“目标”。

如果我正确解释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); 
© www.soinside.com 2019 - 2024. All rights reserved.