PHP ออกรายงาน เป็น MS Word ยังไงค่ะ ไม่รู้จิงๆ ใครรู้ช่วยตอบหน่อยนะค่ะ
ขอบคุณค่ะ
Date :
2011-01-25 17:03:25
By :
คนไม่รู้
ยังทำไม่ได้เลยค่ะ มันฟ้องขึ้นมาว่า
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> This command is not available because no document is open.' in C:\AppServ\www\QA\test2.php:41 Stack trace: #0 C:\AppServ\www\QA\test2.php(41): unknown() #1 {main} thrown in C:\AppServ\www\QA\test2.php on line 41
คืออะไรหรอค่ะ
Date :
2011-02-02 17:31:42
By :
คนไม่รู้
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>รายงาน</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("QA");
$cs1 = "SET character_set_results=utf8";
mysql_query($cs1) or die('Error query ' . mysql_error());
$cs2 = "SET character_set_client =utf8";
mysql_query($cs2) or die('Error query ' . mysql_error());
$cs3 = "SET character_set_connection =utf8";
mysql_query($cs3) or die('Error query ' . mysql_error());
$strSQL = "SELECT * FROM indicator";
$objQuery = mysql_query($strSQL);
$intRows = mysql_num_rows($objQuery);
if($intRows)
{
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
//*** Excel Document Root ***//
$DocName = "QA/MyWord.doc";
//*** Connect to Excel.Application ***//
$Wrd = new COM("Word.Application");
$Wrd->Application->Visible = False;
$objTable = $Wrd->ActiveDocument->Tables->Add($MyRange2,$intRows+1,6,1,2); //** Range,Rows,Column **//
//$Wrd->Documents->Add();
//*** Header ***//
$objTable->Cell(1,1)->Range->InsertAfter("ID_ind");
$objTable->Cell(1,1)->Range->Bold = True;
$objTable->Cell(1,1)->Range->ParagraphFormat->Alignment = 1;
$objTable->Cell(1,2)->Range->InsertAfter("Name_ind");
$objTable->Cell(1,2)->Range->Bold = True;
$objTable->Cell(1,2)->Range->ParagraphFormat->Alignment = 1;
$objTable->Cell(1,3)->Range->InsertAfter("Description");
$objTable->Cell(1,3)->Range->Bold = True;
$objTable->Cell(1,3)->Range->ParagraphFormat->Alignment = 1;
//*** Detail ***//
$intRows = 1;
while($result = mysql_fetch_array($objQuery))
{
$intRows++;
$objTable->Cell($intRows,1)->Range->InsertAfter($result["ID_ind"]);
$objTable->Cell($intRows,1)->Range->ParagraphFormat->Alignment = 1;
$objTable->Cell($intRows,2)->Range->InsertAfter($result["Name_ind"]);
$objTable->Cell($intRows,2)->Range->ParagraphFormat->Alignment = 0;
$objTable->Cell($intRows,3)->Range->InsertAfter($result["Description"]);
$objTable->Cell($intRows,3)->Range->ParagraphFormat->Alignment = 0;
}
@unlink($DocName ); //*** Delete old files ***//
$xlBook->SaveAs($strPath."/".$DocName ); //*** Save to Path ***//
//$xlBook->SaveAs(realpath($strFileName)); //*** Save to Path ***//
//*** Close & Quit ***//
$xlApp->Application->Quit();
$xlApp = null;
$xlBook = null;
}
mysql_close($objConnect);
?>
Excel Created <a href="<?= $DocName ?>">Click here</a> to Download.
</body>
</html>
Date :
2011-02-02 17:40:56
By :
คนไม่รู้
ไม่มีใครช่วยตอบเลย
ช่วยหน่อยนะค่ะ แก้ไม่ได้แล้ว
Date :
2011-02-04 14:44:36
By :
คนไม่รู้
เพิ่มไฟล์เอกสารในโฟเดอร์แล้ว
และก็เอาโค้ดจาก ลิงค์
https://www.thaicreate.com/php/php-word.application-sample-report.html
นี้มาแก้ค่ะ แต่ก็ยังทำไมได้เหมือนเดิมคะหรือว่าต้องแก้จุดไหนอีก
มันฟ้อง error ขึ้นมาอย่างนี้ค่ะ
Fatal error: Uncaught exception 'com_exception' with message 'Parameter 0: Type mismatch. ' in C:\AppServ\www\QA\test.php:29 Stack trace: #0 C:\AppServ\www\QA\test.php(29): variant->Open(false) #1 {main} thrown in C:\AppServ\www\QA\test.php on line 29
ช่วยหน่อยนะค่ะ
Date :
2011-02-06 14:28:46
By :
คนไม่รู้
อันนี้คือโค้ดที่นำมาแก้ใหม่นะค่ะ
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>รายงาน</title>
</head>
<body>
<?
$wdAlignParagraphCenter = "1";
$wdAlignParagraphRight = "2";
$Wrd = new COM("Word.Application");
$DocName = "QA/MyWord.doc";
$Wrd->Application->Visible = False;
//$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$WrdDoc = $Wrd->Documents->Open(realpath("thaicreate.dot"));
$MyRange1 = $WrdDoc->Paragraphs->Add->Range;
$MyRange1->ParagraphFormat->Alignment = $wdAlignParagraphCenter;
$MyRange1->Font->Name = "Verdana";
$MyRange1->Font->Size = "20";
$MyRange1->Font->Bold = True;
$MyRange1->InsertBefore("indicator Report".chr(13));
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("qa");
$strSQL = "SELECT * FROM indiactor";
$objQuery = mysql_query($strSQL);
$intRows = mysql_num_rows($objQuery);
$MyRange2 = $WrdDoc->Paragraphs->Add->Range;
$MyRange2->Font->Size = "10";
$objTable = $Wrd->ActiveDocument->Tables->Add($MyRange2,$intRows+1,6,1,2); //** Range,Rows,Column **//
//*** Header ***//
$objTable->Cell(1,1)->Range->InsertAfter("ID_ind");
$objTable->Cell(1,1)->Range->Bold = True;
$objTable->Cell(1,1)->Range->ParagraphFormat->Alignment = 1;
$objTable->Cell(1,2)->Range->InsertAfter("Name_ind");
$objTable->Cell(1,2)->Range->Bold = True;
$objTable->Cell(1,2)->Range->ParagraphFormat->Alignment = 1;
$objTable->Cell(1,3)->Range->InsertAfter("Description");
$objTable->Cell(1,3)->Range->Bold = True;
$objTable->Cell(1,3)->Range->ParagraphFormat->Alignment = 1;
//*** Detail ***//
$intRows = 1;
while($result = mysql_fetch_array($objQuery))
{
$intRows++;
$objTable->Cell($intRows,1)->Range->InsertAfter($result["ID_ind"]);
$objTable->Cell($intRows,1)->Range->ParagraphFormat->Alignment = 1;
$objTable->Cell($intRows,2)->Range->InsertAfter($result["Name_ind"]);
$objTable->Cell($intRows,2)->Range->ParagraphFormat->Alignment = 0;
$objTable->Cell($intRows,3)->Range->InsertAfter($result["Description"]);
$objTable->Cell($intRows,3)->Range->ParagraphFormat->Alignment = 0;
}
$MyRange3 = $WrdDoc->Paragraphs->Add->Range;
$MyRange3->ParagraphFormat->Alignment = $wdAlignParagraphRight;
$MyRange3->Font->Name = "Verdana";
$MyRange3->Font->Size = "10";
$MyRange3->InsertBefore(chr(13).chr(13).chr(13)." ................................Manager".chr(13).date("Y-m-d H:i:s"));
//$WrdDoc->SaveAs($strPath."/".$DocName);
$WrdDoc->SaveAs(realpath($DocName));
$Wrd->Application->Quit;
$Wrd = null;
?>
Word Created <a href="<?=$DocName?>">Click here</a> to Download.
</body>
</html>
Date :
2011-02-06 14:30:58
By :
คนไม่รู้
แล้วตรงส่วนนี้
$WrdDoc = $Wrd->Documents->Open(realpath("thaicreate.dot"));
คืออะไรหรอค่ะ
Date :
2011-02-06 14:44:06
By :
คนไม่รู้
ลองดูบรรทัดที่ 29 สิ
Date :
2011-02-15 14:42:55
By :
maylex
Load balance : Server 02