Deparei-me recentemente com a elaboração de um projecto em SC onde a emissão de relatórios tinha de ser em Word.
Inicialmente, fiquei apreensivo pela trabalheira que iria dar, e principalmente sendo uma exportação que ainda não tinha efectuado,
apesar do SC ter a exportação para RTF.
Mas a implicação aqui neste processo teria de ser elaborado um relatório personalizado, onde o SC através de várias tabelas teria de escrever num documento
WORD.
Após alguma pesquisa decobri um produto que me resolveu desde logo o meu problema, e consegui criar documentos com excelente
perfomance em WORD.
O produto é free e chama-se PHPWord e pode ser baixado em http://phpword.codeplex.com/ onde tem um pequeno manual de ajuda em
http://phpword.codeplex.com/releases/view/49543#DownloadId=138036
Possui logo a partida um exemplo de como podemos aplicar o codigo.
No SC a instalação aconselho a colocar dentro do directorio
…/apps/nomedoprojecto/PHPWord
na definição do PATH no dicheiro PHPWord.php
/** PHPWORD_BASE_PATH */
if(!defined(‘PHPWORD_BASE_PATH’)) {
define(‘PHPWORD_BASE_PATH’, ‘…/PHPWord/’);
require PHPWORD_BASE_PATH . ‘PHPWord/Autoloader.php’;
PHPWord_Autoloader::Register();
}
Neste Exemplo cloquei o botão PHP para elaborar um relatorio de varias tabelas:
$wnumero={numero};
// Include the PHPWord.php, all other classes were loaded by an autoloader
chdir(’…/PHPWord’);
require_once ‘PHPWord.php’;
// Create a new PHPWord Object
$PHPWord = new PHPWord();
$PHPWord->setDefaultFontName(‘Arial Narrow’);
$PHPWord->setDefaultFontSize(10);
// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();
$fontStyle = array (‘bold’ => true);
$paraStyle = array (‘align’ => ‘center’);
$header = $section->createHeader();
$imageStyle = array(‘width’=>50, ‘height’=>50, ‘align’=>‘center’);
$header->addImage(‘logocmrb2.jpg’, $imageStyle);
$header->addPreserveText(‘Câmara Municipal da Ribeira Brava’,$fontStyle,$paraStyle);
// report footer
$paraStyle = array(‘align’ => ‘center’);
$testVariable = 'Imp.04|DAS.CMRB ';
$footer = $section->createFooter();
$footer->addPreserveText( $testVariable.’{ DATE @“d, MMMM yyyy” }’, null, $paraStyle );
// After creating a section, you can append elements:
$fontStyle = array (‘bold’ => true);
$paraStyle = array (‘align’ => ‘center’);
$section->addText( ‘RELATÓRIO SOCIAL’, $fontStyle, $paraStyle );
$PHPWord->setDefaultFontSize(8);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’ #F0F0F0’);
$PHPWord->addTableStyle(‘myTable’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable’);
$table->addRow(50);
$table->addCell(20000)->addText(‘I - Identificação do Munícipe’, $fontStyle, $paraStyle);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’#FFFFFF ');
$PHPWord->addTableStyle(‘myTable2’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable2’);
$table->addRow(50);
$table->addCell(2000)->addText(‘Nome:’);
$table->addCell(20000)->addText({nome});
$table->addRow(50);
$table->addCell(2000)->addText(‘Morada:’);
$table->addCell(20000)->addText({morada}.", “.{freguesia}.”, “.{codpostal}.”-".{cpostal});
$table->addRow(50);
$table->addCell(2000)->addText(‘Contactos:’);
$table->addCell(20000)->addText(“Telefones: “.{telefones}.”, EMail:”.{email});
$section->addText(" ",$paraStyle);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’ #F0F0F0’);
$PHPWord->addTableStyle(‘myTable’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable’);
$table->addRow(50);
$table->addCell(20000)->addText(‘II - Composição do Agregado Familiar’, $fontStyle, $paraStyle);
$PHPWord->setDefaultFontSize(8);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’#FFFFFF ');
$PHPWord->addTableStyle(‘myTable2’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable2’);
$table->addRow(50);
$table->addCell(8000)->addText(‘Nome’, $fontStyle, $paraStyle);
$table->addCell(3000)->addText(‘Data Nasc.’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘Parentesco’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘Est.Civil’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘NºB.I.’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘NIF’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘Habilitações’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘Ocupação’, $fontStyle, $paraStyle);
$query = mysql_query(“SELECT nome,datanasc,parentesco,estadocivil,bi,nif,habilitacoes,ocupacao FROM agregadofamiliar where registo=$wnumero”);
while($array = mysql_fetch_array($query))
{
$table->addRow(50);
$table->addCell(8000)->addText( $array[‘nome’]);
$table->addCell(3000)->addText( $array[‘datanasc’]);
$table->addCell(2000)->addText( $array[‘parentesco’]);
$table->addCell(2000)->addText( $array[‘estadocivil’]);
$table->addCell(2000)->addText( $array[‘bi’]);
$table->addCell(2000)->addText( $array[‘nif’]);
$table->addCell(2000)->addText( $array[‘habilitacoes’]);
$table->addCell(2000)->addText( $array[‘ocupacao’]);
}
$section->addText(" ",$paraStyle);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’ #F0F0F0’);
$PHPWord->addTableStyle(‘myTable’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable’);
$table->addRow(50);
$table->addCell(20000)->addText(‘III-Situação Socioeconómica e Familiar’, $fontStyle, $paraStyle);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’#FFFFFF ');
$PHPWord->addTableStyle(‘myTable2’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable2’);
$table->addRow(50);
$table->addCell(2000)->addText(‘Data’, $fontStyle, $paraStyle);
$table->addCell(8000)->addText(‘Nome’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘Rendimento’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘Despesa’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(‘Valor Mensal’, $fontStyle, $paraStyle);
$query = mysql_query(“SELECT data,nome,rendimento,despesa,valormensal FROM rendimentosdespesas where registo=$wnumero”);
while($array = mysql_fetch_array($query))
{
$table->addRow(50);
$table->addCell(2000)->addText( $array[‘data’]);
$table->addCell(8000)->addText( $array[‘nome’]);
$table->addCell(2000)->addText( $array[‘rendimento’]);
$table->addCell(2000)->addText( $array[‘despesa’]);
$table->addCell(2000)->addText( $array[‘valormensal’]);
}
$section->addText(" ");
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’#FFFFFF ');
$PHPWord->addTableStyle(‘myTable2’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable2’);
$table->addRow(50);
$table->addCell(8000)->addText(‘Total dos Rendimentos do Agregado:’, $fontStyle, $paraStyle);
$table->addCell(2000)->addText(’.00€’, $fontStyle, $paraStyle);
$section->addText(" ");
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’#FFFFFF ');
$PHPWord->addTableStyle(‘myTable2’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable2’);
$table->addRow(500);
$table->addCell(20000)->addText(’ descrever dinâmica familiar no que respeita à atitude face ao trabalho, escola e prestação de cuidados a dependentes, organização familiar, apoios de outras entidades, etc.’);
$PHPWord->setDefaultFontSize(8);
$section->addText(" ");
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’ #F0F0F0’);
$PHPWord->addTableStyle(‘myTable’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable’);
$table->addRow(50);
$table->addCell(20000)->addText(‘IV - Situação Habitacional’, $fontStyle, $paraStyle);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’#FFFFFF ');
$PHPWord->addTableStyle(‘myTable2’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable2’);
$query = mysql_query(“SELECT tipohabitacao,reg_ocupacao,desc_tipologia,agua,luz,gas,esgotos,conservacao,acessibilidade,mobi_electro,observacoes FROM situacaohabitacional where registo=$wnumero”);
while($array = mysql_fetch_array($query))
{
$table->addRow(50);
$table->addCell(22000)->addText( $array[‘tipohabitacao’].", “.$array[‘reg_ocupacao’].” Agua: “.$array[‘agua’].” Luz: “.$array[‘luz’].” Gás: “.$array[‘gas’].” Esgotos: “.$array[‘esgotos’].”, “.$array[‘conservacao’].”, “.$array[‘acessibilidade’].”, “.$array[‘mobi_electro’].”, ".$array[‘observacoes’]);
}
$section->addText(" ");
$table = $section->addTable(‘myTable’);
$table->addRow(50);
$table->addCell(20000)->addText(‘V-Conclusão’, $fontStyle, $paraStyle);
$styleTable = array(‘borderColor’=>‘006699’,‘borderSize’=>6, ‘cellMarginTop’=>100);
$styleFirstRow = array(‘bgColor’=>’#FFFFFF ');
$PHPWord->addTableStyle(‘myTable2’, $styleTable, $styleFirstRow);
$table = $section->addTable(‘myTable2’);
$table->addRow(500);
$table->addCell(20000)->addText(’ ', $fontStyle, $paraStyle);
$section->addText(" “);
$section->addText(” ");
// report footer
$paraStyle = array(‘align’ => ‘center’);
$testVariable = 'Ribeira Brava, ';
$xdata=date(‘d-m-Y’);
$section->addText( $testVariable.$xdata, null, $paraStyle );
// After creating a section, you can append elements:
$fontStyle = array (‘bold’ => false);
$paraStyle = array (‘align’ => ‘center’);
$section->addText( ‘A Assistente Social,’, $fontStyle, $paraStyle );
$section->addText(" ");
$section->addText( ‘_______________________’, $fontStyle, $paraStyle );
$section->addText( ‘(Ana Nunes)’, $fontStyle, $paraStyle );
// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, ‘Word2007’);
$objWriter->save(’…/documentos_word/Relatorio.doc’);
// ABrir o DOCUMENTO
header(“Location: http://www.???/???/documentos_word/Relatorio.doc”);