Warning when the user Opens the written xls file using PHP Excel - php

The file you're trying to open [fileName] is in different format than
specified by the file extension
I get that message whenever I open the xls files I've written using PHPExcel .. Is there anyway to get rid of that warning
,, the used code
<?php
require_once ('C:\wamp\www\phpexcel sample\Classes\PHPExcel\IOFactory.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("7asobate")->setDescription("PHPExcel tryout");
$objPHPExcel->setActiveSheetIndex(0)->setTitle("7asobate worksheet")->setCellValue('A1' , 'someValue1')
->setCellValue('A2' , 'someValue2');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel , 'Excel2007');
$objWriter->save('F:\myFile.xls');
?>

Related

Copy charts from (.xls or .xlsx) using PHP

I am working on a template excel file for some projects and my goal is to populate it with PHP. I create an excel document with two sheets, one is a table with all of the information that I am going to plot, and the second sheet is just a graph that is based on the table from the previous sheet.
Whenever I use (https://github.com/PHPOffice/PhpSpreadsheet) PHPSpreadsheet (since PHPExcel is deprecated) I am able to read/write/copy/etc the first sheet, but whenever I try to save the sheet without touching the graph sheet at all, nothing happens. I save the file successfully but when I try to open it I get an error stating that Excel found unreadable content in the file and to repair it. I repair it and the second sheet with the graph is empty.
https://github.com/PHPOffice/PhpSpreadsheet/issues/382
This is the closest thing I found to my issue, and I did add the setIncludeChart functions, but nothing seems to work.
I also feel that I have exhausted online searches and am now hoping that there is an alternative to PHPSpreadsheet that will allow me to use a chart in a template (supplied) excel file.
$xls = 'KPI_ReadinessTemplate.xls';
$xlsxTarget = 'NEWX_KPI_ReadinessTemplate.xlsx';
$xlsTarget = 'NEW_KPI_ReadinessTemplate.xls';
$inputFileType = 'Xlsx';
$inputFileName = $xlsx;
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setIncludeCharts(true);
$spreadsheet = $reader->load($inputFileName);
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('A1')->setValue('Help me');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setIncludeCharts(true);
$fileData = $writer->save($xlsxTarget);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setIncludeCharts(TRUE);
$workbook = $reader->load($xls);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($workbook);
$writer->setIncludeCharts(TRUE);
$writer->save($xlsTarget);
I am expecting the chart to be copied, in the code from above there is no modification of the file, it should be an exact one-to-one copy of the supplied file but it isn't working at all as it seems the graph gets corrupted and I have to repair the excel the next time I open it.
EDIT: I also installed the archived PHPExcel and tried it using Graph disapear read and write excel file using PHPExcel and I still had the same issue.

Include images in excel from a folder

I need to add the images from a windows folder to the excel sheet such that each image is placed in front of the row entry containing the name of the file.
Something like this- (the name of the file is same as the id column entry in that row)
What kind of code/language could i use to do this?
I also have this database in Mysql, is it possible to include images in excel using php.
Thanks a lot
Let's suppose you are including your phpExcel library in your project ,
include 'PHPExcel.php';
// you create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Yassine CHABLI");
$objPHPExcel->getProperties()->setLastModifiedBy("Yassine CHABLI");
$objPHPExcel->getProperties()->setTitle("make whatever you want");
$objPHPExcel->getProperties()->setSubject("whatever you want");
$objPHPExcel->getProperties()->setDescription("including images test (example)");
// Add some data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('image example');
$Image = imagecreatefromjpeg('yassine.jpg');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('image');
$objDrawing->setDescription('image');
$objDrawing->setImageResource($Image);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
For more information , visit the link bellow:
https://packagist.org/packages/phpoffice/phpexcel
notice: That will create an “xlsx” formatted file because it uses
2007 excel classes. if you want “xls” format just try with 2005 class
and do not forget to change the file format to “xls” while using 2005.

Php excel import is not working on new server

I am using php excel reader to read excel file but while importing
the file on new server it is showing this page is not working.I have also given the 0777 permission to my folder but then also it is not importing
the excel file data.But on my old server this code is working fine.Please find the below code and let me know is there any code mistake or server setting is required to read phpexcel file.
<?php
$file_path = 'clientupload/'.$upload_data["file_name"];
// ----------------------------------------------------
$this->load->library('PHPExcel');
//Here i used microsoft excel 2007
$objReader= PHPExcel_IOFactory::createReader('Excel2007');
//Set to read only
$objReader->setReadDataOnly(true);
//Load excel file
$objexcel=$objReader->load($file_path); // error in this line
// echo "hello";die;
$objWorksheet = $objexcel->getActiveSheet(0);
$highestRow = $objWorksheet->getHighestRow();
?>
It is showing below error:
This page isn’t working
216:xx:xx is currently unable to handle this request.
HTTP ERROR 500

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

Excel 97-2003 file incompatible after PHPExcel reads and writes

I have an excel file that has been created and saved as an Excel 97-2003 file. I am using the following code to read and then write using PHPExcel.
$objReader = new \PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load(storage_path() . "/exports/file.xls");
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Test Message');
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter-> save(storage_path() . "/exports/file.xls");
I'm using SamApp ExcelSMS Android app that will then read the file. The only problem is that the file is now incompatible when overwritten with new content. Also, the file size decreases from 18KB to 5KB.
Any help on this is greatly appreciated.

Categories