- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to open an Excel file with PHPExcel for both reading and writing?
There is no concept of opening a file for read and write in PHPExcel since it is not aware of the source of the PHPExcel object. Irrespective of the source from where the file was loaded or the type of file, the file can be read based on its named and saved with the same name. This way, the file will be overwritten, and new changes will be reflected in the file.
error_reporting(E_ALL); set_time_limit(0); date_default_timezone_set('Europe/London'); set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/'); include 'PHPExcel/IOFactory.php'; $fileType = 'Excel5'; $fileName = name_of_file.xls'; // Read the file $objReader = PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = $objReader->load($fileName); // Change the file $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B1', 'World!'); // Write the file $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType); $objWriter->save($fileName);
This will produce the following output −
Changes to cell A1, B1 are reflected in the name_of_file.xls file.
The time limit is set to 0 and the timezone is set to Europe/London. The filetype is known to be Excel and filename is assigned to the variable ‘fileName’. The ‘PHPExcel_IOFactory’ class ‘createReader’ is used to create the object and it is loaded using the ‘load’ function. Two cell values of the ‘xls’ sheet are changed and it is saved with the same named.
- Reading and Writing CSV File using Python
- Python - Writing to an excel file using openpyxl module
- Reading and writing binary file in C/C++
- Reading and writing Excel files using the openpyxl module in Python
- Reading/Writing a MS Word file in PHP
- Reading and Writing Files in Perl
- Reading and Writing Files in Python
- Reading and Writing to text files in C#
- Reading and Writing to text files in Python
- Reading and Writing to text files in Python Program
- Uploading an excel file in Web Dynpro for ABAP
- How to open an Excel Spreadsheet in Treeview widget in Tkinter?
- What are reading and writing characters in C language?
- Python Pandas - Create an open time interval and check for existence of both the endpoints
- Opening and reading a file with askopenfilename in Tkinter?