По ссылке на JS, а на ПХП примерно так:
// Вызываем Ворд
$word = new COM("word.application") or die("Невозможно загрузить Word!");;
// Делаем что бы он был виден
$word->Visible = true;
// Открываем в Ворде документ
$doc = $word->Documents->open('путь к документу/contract.doc');
$word->Activate();
// Ищем в Вордовском шаблоне закладки (Меню:Вставка/Закладка
if ($word->ActiveDocument->Bookmarks->Exists("CONTRACT_NUMBER")) {
// Если искомая закладка надена, то
//отмечаем ее
$word->ActiveDocument->Bookmarks["CONTRACT_NUMBER"]->Select();
// заменяем выбранное на необходимый текст
$word->ActiveDocument->Bookmarks["CONTRACT_NUMBER"]->Range->Text = $this->input->post('number');
}
// Другая закладка...
if ($word->ActiveDocument->Bookmarks->Exists("CONTRACT_CONCLUDE")) {
$word->ActiveDocument->Bookmarks["CONTRACT_CONCLUDE"]->Select();
$word->ActiveDocument->Bookmarks["CONTRACT_CONCLUDE"]->Range->Text =
$this->contracts_model->convert_date($this->input->post('conclude'));
}
//и т.д.
// Если нужно вместо закладки вставить таблицу, то примерно так:
if ($word->ActiveDocument->Bookmarks->Exists("CLIENT_EQUIPMENT")) {
$word->ActiveDocument->Bookmarks["CLIENT_EQUIPMENT"]->Select();
$table = $doc->Tables->Add($word->Selection->Range, $result + 1, 4);
// рисуем Шапку
$table->Cell(1,1)->Range->Text = "Название оборудования";
// форматируем ее внешний вид
$table->Cell(1,1)->Range->Bold = True;
$table->Cell(1,1)->Range->ParagraphFormat->Alignment = 1; // 0= Left, 1=Center, 2=Right
$table->Cell(1,1)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,1)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,1)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,1)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,2)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,2)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,2)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,2)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,3)->Range->Text = "Количество";
$table->Cell(1,3)->Range->Bold = True;
$table->Cell(1,3)->Range->ParagraphFormat->Alignment = 1; // 0= Left, 1=Center, 2=Right
$table->Cell(1,3)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,3)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,3)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,3)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,4)->Range->Text = "Ед. измерения";
$table->Cell(1,4)->Range->Bold = True;
$table->Cell(1,4)->Range->ParagraphFormat->Alignment = 1; // 0= Left, 1=Center, 2=Right
$table->Cell(1,4)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,4)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,4)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell(1,4)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
// Вставляем данные
$i=1;
foreach($cur_equipment as $item):
$table->Cell($i+1,1)->Range->Text = "Еще чегг-то";
$table->Cell($i+1,1)->Range->ParagraphFormat->Alignment = 0; // 0= Left, 1=Center, 2=Right
// -1, либо -2, -3, -4, что соответственно означает - верхний, левый, правый, нижний бордер
$table->Cell($i+1,1)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,1)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,1)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,1)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,2)->Range->Text = $item['name_e']; // можно из массива
$table->Cell($i+1,2)->Range->ParagraphFormat->Alignment = 0; // 0= Left, 1=Center, 2=Right
$table->Cell($i+1,2)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,2)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,2)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,2)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,3)->Range->Text = sprintf('%.3f',$item['amount']);
$table->Cell($i+1,3)->Range->ParagraphFormat->Alignment = 2; // 0= Left, 1=Center, 2=Right
$table->Cell($i+1,3)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,3)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,3)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,3)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,4)->Range->Text = $item['legend'];
$table->Cell($i+1,4)->Range->ParagraphFormat->Alignment = 1; // 0= Left, 1=Center, 2=Right
$table->Cell($i+1,4)->Borders->Item(-1)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,4)->Borders->Item(-2)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,4)->Borders->Item(-3)->LineStyle=$word->Options->DefaultBorderLineStyle;
$table->Cell($i+1,4)->Borders->Item(-4)->LineStyle=$word->Options->DefaultBorderLineStyle;
$i++;
endforeach;
}
$filename = "c:/TEMP/temp.doc";
// сохраняем в указанній файл
$word->ActiveDocument->SaveAs($filename);
Пример рабочий.
(Отредактировано автором: 11 Августа, 2011 - 14:58:18)