I'm currently using PHP Excel to output an excel file in html. I was wondering if it was possible to edit the source code so that instead of outputting a table, it outputs an input or textarea instead. I'm not quite sure where to begin. I'm trying to take a look at all the classes in PHPExcel and can't find where the tables are being outputted so I can change them. If anyone can help me that would be great! Thanks
$inputFileName = "file";
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->writeAllSheets();
$objWriter->save('php://output');
Apparently it's written in /Classes/PHPExcel/Writer/HTML.php.
Maybe you could create your own writer, by extending PHPExcel_Writer_Abstract and implementing the interface IWriter?
I think it would be simpler to create your own method to be able to add all your specific needs (image, layout, etc ..). And then share it ;)
Related
Can anyone let me know if there are any other library choices available for me where I can do what I wanted.. that is open the excel template file (.xlsx or even .xlsm) in php and populate the cells and keep all the charts, pivot table, even macros, forms, buttons, etc. intact. All the php will do is just populate the cells with new data from database. In other words, using the same terminology that you have used.. please point me to the library for which I can use to "edit" workbook files.
When i try the below code.. the chart itself goes missing from the resulting file. After trying various ways.. still failed.
Thank you so much again.
<?php
date_default_timezone_set('Europe/London');
require_once 'excel/PHPExcel/IOFactory.php';
require_once 'excel/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load('WorkingFile_Daily.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('B11', 4)
->setCellValue('B12', 5)
->setCellValue('B13', 6)
->setCellValue('B14', 7);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('WorkingFile_Daily New.xlsx');
?>
I'm currently working on a project that needs to display excel files (xls, xlsx, csv) on the browser. So far, I have tried and used the PHPExcel library and was able to display the excel file (code below)
$opendoc = $userDoc;
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load($opendoc);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->writeAllSheets();
$objw->save('php://output');
The problem I'm encountering is that this code does not support the displaying images (charts , graph , etc) inside an excel file. Any ideas? Thanks in advance!
Errr..... yes it does. Have you read the documentation or looked at the examples? Images are supported directly, and (unless you tell PHPExcel to load data only) should always be loaded.
For charts and graphs, you specifically have to tell PHPExcel to load them when reading a file, and tell PHPExcel to save them when Writing. (Example)
$opendoc = $userDoc;
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($opendoc);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->setIncludeCharts(TRUE);
$objw->writeAllSheets();
$objw->save('php://output');
I am trying to use the PHPExcel class to export reports into excel file. What I really need to do is to go thru every row in the HTML table then place it into cells in an excel file.
I think PHPExcel has enough document to help me with created the excel file. But what I am not sure on how to do is to take an html table and break it so I can take every value and put it in an excel cell.
Thanks for your help
As simple as:
$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load("myHtmlFile.html");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("myExcelFile.xlsx");
but you could use your same basic logic for building the HTML from your report data, and just modify it to set cell values in a PHPExcel object. If you have an MVC, then its only the View that you really need to change.
If you want to mess with HTML but in PHP, i recommend SimpleHTMLDom
If you like jquery, you'll love it.
Example code:
$html = str_get_html('<table id="mytable"><td>cell ??</td><td>cell 2</td></table>');
$html->find("#mytable td:first-child",0)->innertext = "cell 1!";
echo $html;
done.
You loop through it with jQuery
$('td').each(function(){
//do stuff here...
});
jQuery API docs for .each
my problem is that i have an already existing excel file with Conditional Formatting rules in a Column, i read it and save it with the next sentences:
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load('../files/Reportes.xlsx');
//i set a value
$objPHPExcel->getActiveSheet()->SetCellValue('F6', 10);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('../files/Reportes.xlsx');
the output file is my Reportes.xlsx file without any Conditional Formating rules :S
i read in a post(a 2010 post) that this is a bug, im using the 1.7.6 of PHPEXCEL
plz help me, Thanks
It's likely that anything PHPExcel_Writer doesn't support will get wiped out when you have it save a document.
I need to read the contents of a XLS file which has 10 columns and one of which contains the float vales like(10.5, 78.88).
However when I try to read the values I get values like (105, 7888), please help me in this regard with any example code or any valid solution.
You could try to follow the tutorial # http://www.ibm.com/developerworks/opensource/library/os-phpexcel/index.html to see what you are doing differently to get your current code to work.
Hope that helps.
You can use PHPExcel that allows you to both read and write Excel file. Download it here.
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($dirpath .'/'. $fileName);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C3', 'some text here');
// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($dirpath .'/'. $fileName);