Read Excel file using PHP - php

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);

Related

PHPExcel converter not working fine

I try to convert csv to xlsx using PHPExcel tools, when i run the script i got error and i got chinese font in excel file after convert:
Error PHP EXECL
here is my code :
<?php
include 'PHPExcel/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('CSV');
// If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader
$objReader->setDelimiter("\t");
// If the files uses an encoding other than UTF-8 or ASCII, then tell the reader
$objReader->setInputEncoding('UTF-16LE');
$objPHPExcel = $objReader->load('myfilebeforeconvert.csv');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('afterconvert.xls');
what my fault ? please tell me
You have provided little information but you can check this example for help.
This might also help, check this link

Load a Excel file with images using PHPExcel

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');

PHPExcel unable to open the saved file

I am trying to open an existing excel file, modify some cells and save it. I am using Excel2007 for reader and writer.
The input file is about 1 MB large and it has few formulas, protected data and hidden rows and columns and worksheets which I do not modify.
I am able to load the data and read and write some values into it, which I check with various var_dumps in the code.
The problem is while saving it. It throws some fatal errors on timing outs and also if it writes the file the file size is bloated to 9.2 MB, which is okay if I can open it.
code snippet - nothing fancy.
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($inputFile);
$objPHPExcel->setActiveSheetIndex(2);
$activeSheet = $objPHPExcel->getActiveSheet();
$currCell = $activeSheet->getCell("O3");
$cellValidation = $currCell->getDataValidation("O3");
$values = array();
if ($cellValidation->getShowDropDown() == true)
{
$values = $cellValidation->getFormula1();
$valArray = explode(",", $values);
$currCell->setValue($valArray[0]);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter -> setPreCalculateFormulas(false);
$objWriter->save($outputFile);
I use MS Excel 2010 to open the resultant file but it just takes forever and has not opened it even once.
Please help me to troubleshoot this by giving me pointers as to where I should be looking.
Any help is greatly appreciated.
Instead of saving it to a file, save it to php://outputĀ­Docs:
$objWriter->save('php://output');
This will send it AS-IS to the browser.
You want to add some headersĀ­Docs first, like it's common with file downloads, so the browser knows which type that file is and how it should be named (the filename):
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');
// Write file to the browser
$objWriter->save('php://output');
First do the headers, then the save. For the excel headers see as well the following question: Setting mime type for excel document.
So the final code would have below lines -
// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xlsx"');
$objWriter->save('php://output');
I think this line:
ob_end_clean();
Should solve your problem.
Thanks!
There's a whole lot of reasons for that "bloat" and it very much depends on the actual data in the worksheet, but MS Excel itself uses a lot of different techniques to keep the filesize small, whereas PHPExcel writes a simple version of the OfficeOpenXML format.
For example, MS Excel looks at the string content of all cells, and stores the individual strings in a string table. If a string is used by two or more cells, there will only be a single entry in the string table. However, there's a performance overhead in checking if a string already exists in the string table, so PHPExcel doesn't perform that check but will duplicate entries in the string table. This means that it will create a large file because of the duplication, but keeps the save speed as fast as possible.
Similarly, MS Excel looks at all formulae, and if the formula is similar to an existing formula (with only a row/column offset difference) it will store it as a shared formula rather than a cell formula, so the actual formula data is only stored once. Again, PHPExcel won't perform this check, because it is a big performance overhead in the save, so it stores every formula as a cell formula rather than a shared formula.
And no, I can't explain why the file doesn't load in MS Excel 2010, nor will I be able to explain it without being able to run the whole thing through debug

PHP Excel Output <inputs> in html

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 ;)

why My Existing Conditional Formatting Rules set by Excel are Lost after using objWriter->PHPEXCEL-save?

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.

Categories