{"id":149,"date":"2009-09-01T12:44:52","date_gmt":"2009-09-01T11:44:52","guid":{"rendered":"http:\/\/www.cyril-grandjean.fr\/articles\/?p=149"},"modified":"2018-03-22T18:46:31","modified_gmt":"2018-03-22T17:46:31","slug":"creer-un-fichier-xlsx-via-php-avec-phpexcel","status":"publish","type":"post","link":"https:\/\/www.cyril-grandjean.fr\/en\/create-excel-file-with-php-excel","title":{"rendered":"Create a file XLSX using PHP with PHP Excel"},"content":{"rendered":"<p>During my work for the company Planet Bourgogne, I had to create an XLS file using PHP and MySQL. I&#8217;ve also discovered a PHP library for reading and writing XLSX files.<\/p>\n<p>The format XLSX is supported by Excel 2007 and later. For the previous versions, it requires the installation of the following module : http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=fr&amp;FamilyID=941b3470-3ae9-4aee-8f43-c6bb74cd1466.<\/p>\n<p>Here are some lines of codes which are going to allow you to create your first file XLS by using PHP.<\/p>\n<ol>\n<li><a href=\"http:\/\/phpexcel.codeplex.com\/Release\/ProjectReleases.aspx?ReleaseId=10717\">Download<\/a> and copy the library PHPExcel in your PHP Project<\/li>\n<li>Create a PHP file by using the following syntax :<\/li>\n<\/ol>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/** Errors report *\/\r\nerror_reporting(E_ALL);\r\n\r\n\/** Include path **\/\r\nset_include_path(get_include_path() . PATH_SEPARATOR . 'PHPExcel\/Classes\/');\r\n\r\n\/** PHPExcel *\/\r\ninclude 'PHPExcel.php';\r\n\r\n\/** PHPExcel_Writer_Excel2007 *\/\r\ninclude 'PHPExcel\/Writer\/Excel2007.php';\r\n\r\n\/\/ Create new PHPExcel object\r\n$objPHPExcel = new PHPExcel();\r\n\r\n\/\/We add contents\r\n\/\/Warning, a utf8_encode() is necessary for the character like '\u00e9', '\u00e8', ..\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;setCellValue('A1', 'Ligne 1 Colonne 1');\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;setCellValue('A2', utf8_encode('Dur\u00e9e'));\r\n\r\n\/\/Manage the size of the column\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getColumnDimension('A')-&amp;gt;setWidth(15);\r\n\r\n\/\/Manage font style\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getFont()-&amp;gt;setSize(10);\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getFont()-&amp;gt;setBold(true);\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getFont()-&amp;gt;setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getAlignment()-&amp;gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);\r\n\r\n\/\/Make a border style\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getBorders()-&amp;gt;getLeft()-&amp;gt;setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getBorders()-&amp;gt;getRight()-&amp;gt;setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getBorders()-&amp;gt;getTop()-&amp;gt;setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);\r\n$objPHPExcel-&amp;gt;getActiveSheet()-&amp;gt;getStyle('A1')-&amp;gt;getBorders()-&amp;gt;getBottom()-&amp;gt;setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);\r\n\r\n\/\/ Save our file xlsx\r\n$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);\r\n$objWriter-&amp;gt;save('..\/data_xls\/myfileXLS.xlsx');\r\n<\/pre>\n<p>This tutorial is only the bases necessary for the creation of a simple file XLSLX. For advanced used : click <a href=\"http:\/\/phpexcel.codeplex.com\/Wiki\/View.aspx?title=Examples\">here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>During my work for the company Planet Bourgogne, I had to create an XLS file using PHP and MySQL. I&#8217;ve also discovered a PHP library for reading and writing XLSX files. The format XLSX is supported by Excel 2007 and later. For the previous versions, it requires the installation of the following module : http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=fr&amp;FamilyID=941b3470-3ae9-4aee-8f43-c6bb74cd1466. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,107],"tags":[30,29],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-developpement-web","category-tous","tag-excel","tag-php"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":0,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}