{"id":7796,"date":"2015-10-27T08:52:29","date_gmt":"2015-10-27T08:52:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/how-to-open-an-excel-file-with-phpexcel-for-both-reading-and-writing-open-source-projects-phpoffice-phpexcel\/"},"modified":"2015-10-27T08:52:29","modified_gmt":"2015-10-27T08:52:29","slug":"how-to-open-an-excel-file-with-phpexcel-for-both-reading-and-writing-open-source-projects-phpoffice-phpexcel","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/how-to-open-an-excel-file-with-phpexcel-for-both-reading-and-writing-open-source-projects-phpoffice-phpexcel\/","title":{"rendered":"How to open an Excel file with PHPExcel for both reading and writing?-open source projects PHPOffice\/PHPExcel"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/rPKTe.jpg?s=128&amp;g=1\" \/> <strong>Mark Baker<\/strong><\/p>\n<p>You load a file into PHPExcel using a reader and the load() method, then save that file using a writer and the save() method&#8230; but PHPExcel itself is unaware of the source of the PHPExcel object&#8230; it doesn&#8217;t care whether you have loaded it from a file (or what type of file) or created it by hand.<\/p>\n<p>As such, there is no concept of &#8220;opening for read\/write&#8221;. You simply read the file by name, and save to the same filename. That will overwrite the original file with any changes that you have made in your script.<\/p>\n<p><strong>EDIT<\/strong><\/p>\n<p>Example<\/p>\n<pre><code>error_reporting(E_ALL);\nset_time_limit(0);\n\ndate_default_timezone_set('Europe\/London');\nset_include_path(get_include_path() . PATH_SEPARATOR . '.\/Classes\/');\n\ninclude 'PHPExcel\/IOFactory.php';\n\n$fileType = 'Excel5';\n$fileName = 'testFile.xls';\n\n\/\/ Read the file\n$objReader = PHPExcel_IOFactory::createReader($fileType);\n$objPHPExcel = $objReader-&gt;load($fileName);\n\n\/\/ Change the file\n$objPHPExcel-&gt;setActiveSheetIndex(0)\n            -&gt;setCellValue('A1', 'Hello')\n            -&gt;setCellValue('B1', 'World!');\n\n\/\/ Write the file\n$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);\n$objWriter-&gt;save($fileName);\n<\/code><\/pre>\n<p>And can I suggest that you read the documentation, and look at the sample code in \/Tests<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mark Baker You load a file into PHPExcel using a reader and the load() method, then save that file using a writer and the save() method&#8230; but PHPExcel itself is unaware of the source of the PHPExcel object&#8230; it doesn&#8217;t care whether you have loaded it from a file (or what type of file) or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7796","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7796"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7796\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}