Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify existing excel file using PHP?

Tags:

php

excel

pear

I have a excel i need to add some more sheets into the excel using PHP, i have used PEAR, there i tried only can write excel and read a file, not able to read and modify the file, guys can you help me in this?

Thanks in advance

Prabu

like image 317
Prabu Avatar asked Jan 27 '10 04:01

Prabu


People also ask

How can I edit XLSX file in php?

You may use the following code: header('Content-Type: application/vnd. ms-excel'); header('Content-Disposition: attachment;filename="file. xlsx"'); header('Cache-Control: max-age=0'); $writer->save('php://output');

How connect Excel to php?

Establish a ConnectionOpen the connection to Excel by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all. $conn = odbc_connect("CData ODBC Excel Source","user","password"); Connections opened with odbc_connect are closed when the script ends.

How do I make an Excel Spreadsheet editable?

Click File > Options > Advanced. , click Excel Options, and then click the Advanced category. Under Editing options, do one of the following: To enable Edit mode, select the Allow editing directly in cells check box.


1 Answers

You will need 2 pear packages

  1. PHP-ExcelReader package
  2. Spreadsheet_Excel_Writer package

What you need to do is read first the excel file use PHP-ExcelReader package It reads the binary format of XLS files directly and can return values and formats from any cell. http://code.google.com/p/php-excel-reader/

read the excel file

$data = new Spreadsheet_Excel_Reader("test.xls");

show the data of the file

$data->dump($row_numbers=false,$col_letters=false,$sheet=0,$table_class='excel')

Once you have stored the data in a variable save the data in another file this time you will use the The Spreadsheet_Excel_Writer package https://github.com/pear/Spreadsheet_Excel_Writer

 <?php
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer('test.xls');
$worksheet =& $workbook->addWorksheet('My first worksheet');
if (PEAR::isError($worksheet)) {
    die($worksheet->getMessage());
}
$workbook->close();
?> 
like image 88
Gerard Banasig Avatar answered Nov 07 '22 18:11

Gerard Banasig