{"id":49,"date":"2014-08-04T10:00:48","date_gmt":"2014-08-04T15:00:48","guid":{"rendered":"http:\/\/razonartificial.com\/themes\/openmind\/wordpress\/?p=49"},"modified":"2021-12-06T12:21:15","modified_gmt":"2021-12-06T17:21:15","slug":"generar-archivos-microsoft-office-rapidamente-desde-symfony2","status":"publish","type":"post","link":"https:\/\/www.apdi.co\/blog\/generar-archivos-microsoft-office-rapidamente-desde-symfony2\/","title":{"rendered":"Generar Archivos Microsoft Office Rapidamente desde Symfony2"},"content":{"rendered":"<p>En una aplicaci\u00f3n empresarial usualmente se deben generar archivos Ms Excel o Word, la librer\u00eda PHPOffice (link: https:\/\/github.com\/PHPOffice\/) nos ayuda a generar archivos Excel, Word,Power Point, Project y Visio desde symfony2 o PHP cl\u00e1sico.<br \/>\n<!--more--><br \/>\nDependiendo el archivo que queramos generar se necesita tener instalado una serie de librer\u00edas o extensiones para esto es necesario tener en cuenta el apartado de requisitos para cada uno.<\/p>\n<p>Para generar archivos Excel y Word desde symfony vamos a nuestro composer.json y en require<\/p>\n<p>agregamos esto:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&quot;phpoffice\/phpword&quot;: &quot;dev-master&quot;,<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&quot;phpoffice\/phpexcel&quot;: &quot;dev-master&quot;<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Seguido a esto nos ubicamos en la carpeta de nuestro proyecto por la consola y actualizamos<\/p>\n<p>nuestro composer as\u00ed:<\/p>\n<p>composer.phar update<\/p>\n<p>Esperamos que termine de ejecutarse la actualizaci\u00f3n, luego para generar un archivo de Word en nuestro controlador agregamos las siguientes l\u00edneas de ejemplo:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">...<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$phpWord<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> \\PhpOffice\\PhpWord\\PhpWord<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$section<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$phpWord<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">addSection<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$section<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">addText<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'esta es una prueba de contenido'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$WordWriter<\/span> <span style=\"color: #339933\">=<\/span> \\PhpOffice\\PhpWord\\IOFactory<span style=\"color: #339933\">::<\/span><span style=\"color: #004000\">createWriter<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$phpWord<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'Word2007'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$WordWriter<\/span><span style=\"color: #339933\">-&gt;;<\/span>save<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'prueba1.docx'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Al ejecutar el controlador vemos que en la carpeta web aparece un archivo que se llama<\/p>\n<p>prueba1.docx.<\/p>\n<p>Puedes ver los ejemplos de la librer\u00eda phpword en tu carpeta<\/p>\n<p>vendor\\phpoffice\\phpword\\samples<\/p>\n<p>Ahora para generar un archivo Excel, con un c\u00e1lculo simple de ejemplo en el controlador<\/p>\n<p>agregamos estas l\u00edneas:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">...<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$phpExcel<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> \\PHPExcel<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$phpExcel<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">getActiveSheet<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">-&amp;<\/span>gt<span style=\"color: #339933\">;<\/span>setCellValue<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'A1'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'datos'<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">setCellValue<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'A2'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">setCellValue<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'A3'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #cc66cc\">2<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">setCellValue<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'A4'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #cc66cc\">3<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">setCellValue<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'A5'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'=SUM(A2:A4)'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$ExcelWriter<\/span> <span style=\"color: #339933\">=<\/span> \\PHPExcel_IOFactory<span style=\"color: #339933\">::<\/span><span style=\"color: #004000\">createWriter<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$phpExcel<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'Excel2007'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$ExcelWriter<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">save<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'prueba1.xlsx'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Al ejecutar el controlador vemos que en la carpeta web aparece un archivo que se llama<\/p>\n<p>prueba1.xslx.<\/p>\n<p>Puedes ver los ejemplos de la librer\u00eda phpExcel en tu carpeta<\/p>\n<p>vendor\\phpoffice\\phpexcel\\Examples<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En una aplicaci\u00f3n empresarial usualmente se deben generar archivos Ms Excel o Word, la librer\u00eda PHPOffice (link: https:\/\/github.com\/PHPOffice\/) nos ayuda a generar archivos Excel, Word,Power Point, Project y Visio desde symfony2 o PHP cl\u00e1sico.<\/p>\n","protected":false},"author":1,"featured_media":847,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-symfony"],"_links":{"self":[{"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":2,"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":838,"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions\/838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/media\/847"}],"wp:attachment":[{"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.apdi.co\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}