SimpleXLSXGen: generated excel file is showing to be corrupted - php

I am trying to generate an excel file in PHP using the SimpleXLSXGen library. This is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: text/plain; charset=utf-8');
require __DIR__.'/vendor/shuchkin/simplexlsxgen/src/SimpleXLSXGen.php';
use \Shuchkin\SimpleXLSXGen as SimpleXLSXGen;
$arr = array();
/*some logic here to populate my array*/
$xlsx = new SimpleXLSXGen;
$xlsx::fromArray($arr)
->setDefaultFont('Courier New')
->setDefaultFontSize(12)
->saveAs("..files/file.xlsx");
?>
This code used to work before, but now when I execute it, I keep getting a corrupted excel file generated. Everytime I open the file, it keeps showing:
Excel cannot open the file 'file.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file
I have also debugged the $arr that I am pushing into my excel file, and it has the correct values. So why is the generated excel file showing to be corrupted?

Related

Save xml api as and excel file in php

I have this code which save xml api as an xlsx. But When I save it, i can not open on computer. But txt doc and other formats support it. How can I save it as an xlsx file successfully.
<?php
$xlsx = file_get_contents("http://cbu.uz/uzc/arkhiv-kursov-valyut/xml/");
file_put_contents("currency/kurs.xlsx", $xlsx);
?>

read contents of zipped files through file_get_contents in codeigniter in .srt format and upload to server

I am trying to read contents of zipped file as
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
And trying to upload at my online server as below
$this->load->library('zip');
$data = $subda;
$name = 'myfile.srt';
$this->zip->add_data($name, $data);
$this->zip->archive('assets/subtitles/myzipfile.zip');
But when I check this uploaded file at my server it does not compressed properly.
it does not contain any data.
when I echo $subda it give results like.
Where I am wrong...
through file_get_contents I am already getting contents of zip file.
You cannot do:
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
This will just load the ZIP content into the string rather than uncompressed.
See this on how you can read using a lib in php:
Best way to read zip file in PHP

edit existing excel on web page using php

Does anyone have an idea to create the dynamic form with uploaded excel file.
We need to facilitate users to add data in the pre-uploaded file. Can anyone help with this? We are doing in this way:
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'HTML');
$objPHPExcelWriter->save('php://output');
But this above code only displays structure of excel on the web still we're unable to edit it.
you will need 2 pear packages
PHP-ExcelReader package
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();
?>

Read .xlsx file when the file is opened using php

I need to read the data from the .xlsx file when that file is opened without saving it.
The .xlsx file is frequently updating (in every 1 or 2 seconds), so I need last updated data from the file.
<?php
include 'xls/simplexlsx.class.php';
$xlsx = new SimpleXLSX('export_data.xlsx');
$xlxs_value = $xlsx->rows();
echo $xlxs_value;
?>
But when I executed this code, I get data only when the file is last saved, so please help me to read the data without saving the file and it is opened by MS OFFICE.

xls file php download is in a different format than specified by the file extension

I'm struggling with my XML to XLS code in PHP.
I have PHP code that exports MySql data to XML and I have code that writes XML to XLS.
This all works fine, except that when I open the xls file, I get an error:
"The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
my code:
<?php
$file = 'filename';
$url = "$file.xml"; // from xml file
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=$file.xls" ); // to xls file
if (file_exists($url)) {
$xml = simplexml_load_file($url);
echo 'Voornaam'."\t" . 'Achternaam'."\t" . 'Geslacht'."\t" . 'Instrument'."\t\n";
foreach($xml->lid as $lid)
{
echo $lid->voornaam."\t" . $lid->achternaam."\t" . $lid->geslacht."\t" . $lid->instrument."\n";
}
}
?>
I found that it has something to do with the MIME_Types https://filext.com/faq/office_mime_types.php
The header("Content-Type: application/vnd.ms-excel"); is for the older versions of excel. But when I use the xlsx type, the file won't open at all.
Like I said, this code runs fine, downloads the Excel file in xls format to my computer. But how can I open it without the error?
I would also like to upgrade the file to xlsx format if someonw knows how to open the file in that format.
Well, your header and filename are both saying "this is an XLS file" - but it isn't, it's actually a TSV file (Tab-Separated Values). So yes, the message is accurate: the file extension is misleading.
To write an actual, valid XLS (or XLSX), it is easiest to use a library which does this for you - both these formats are complex and tricky. I've had best results using PhpExcel (example code).
When Excel opens an .xls file, it expects it to be in normal old binary Excel format. You seem to be outputting tab separated text (.tsv format), which Excel understands but doesn't want to open without issuing a warning. I think this warning was introduced with Excel 2007, and that earlier versions opened happily without warnings. Unfortunately I have no sources handy for this info, only personal experience.

Categories