How to convert Xls file with formula through PhpExcel - php

I have an xls file which i was trying to upload using PhpExcel plugin .
My code is like this
Excel::load($file_path, function($sheet) {
$detail = $sheet->get();
var_dump($detail);
});
Since my file had excel formula the Plugin is throwing following error
PHPExcel_Calculation_Exception: 10!I9 -> Formula Error: Unexpected ,
My question is how do i remove the formula and then try uploading it ?
Thanks

Related

Parsing Badly written XLS

I have to parse with php an XLS file that is written by some other code and it seems to be poorly written.
I've tried parsing it with PHPExcel using autorecognition in this way:
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
echo 'filetype: '.$inputFileType.'<br>';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
Which returns:
filetype: CSV
The file is opened but it is not read correctly as the data it's not correctly recognized, content is not in proper cells and some cells give error. I've tried using all other PHPExcel filetypes and all of them return error.
I've tried to open it with a text editor (Notepad++) and the file it's in binary, not a simple CSV. The extension is XLS but since it's written via a script cannot be used as unique identifier of the version.
If i open the file with Excel it's opened and i can saved it in another format (for example as a new xlsx file) and after that i can correctly read it.
Thinking it's encoded in some very old format, I've tried with other library SimpleExcel and i got this error:
File extension XLS doesn't match with xml
Is there a way to "correct" the format before parsing it?

PhpSpreadsheet cannot read specific xls file

The following error
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Reader\Exception: Parameter pos=-12 is invalid
is given when trying to parse a specific xls file.
Code
$inputFileName = "excel.xls";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xls');
$spreadsheet = $reader->load($inputFileName);
The file in question: https://filebin.net/sle19tm0kdgduyne/excel.xls?t=u0itbeue
I have tried using all available readers such as Xlsx, Csv etc and even using the old deprecated PHPExcel library. Nothing can parse this specific file, even though it opens fine with excel on windows.
My end goal is converting this xls file to an array, so i can paste the data into a database.
I think you don't need to use createReader().
Here the example of my worked code.
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$spreadsheet = $reader->load('path/to/file.xls');
Hope this can help you.

issue while reading excel file with large number of cell in codeigniter

I am trying to read excel file in my CodeIgniter application. The function getActiveSheet()->toArray(null,true,true,true); is working fine for an excel file with 14442 x 17 cells, however this function does not works for an excel file with 17590 x 17 cells. In this second case, browser ends-up with a blank page and I am not getting any error. So please tell what can be the issue?
Code:
$objPHPExcel = PHPExcel_IOFactory::load($file_path);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
Probably out of memory. It is a common issue with large excel files.
If you only need to read the data you can use something like
$objReader = PHPExcel_IOFactory::createReaderForFile($file);
$objReader->setReadDataOnly(true);

Export barcode in excel sheet (in Laravel)

I implemented this library to generate barcodes images in my laravel project https://github.com/milon/barcode
For generating the excel sheet i use this library :
https://github.com/Maatwebsite/Laravel-Excel
For the barcode image i just add a value in my database and then i convert that value in a barcode image and know i need to export the barcode in excel
For exporting the barcode i tried many things but none of them seems to work
in my view i tried different methods for exporting the barcode
The first method :
<td><img src="data:storage/app/upload/;base64,{!! DNS1D::getBarcodePNG("$employee->barcode", 'C39')!!}" ></td>
and gives me the following error:
(1/1) PHPExcel_Exception File
data:storage/app/upload/;base64,iVBORw0KGgoAAAANSUhEUgAAAcAAAAAeAQMAAACBqFzbAAAABlBMVEX///8AAABVwtN
AAAAAXRSTlMAQObYZgAAACpJREFUOI1jOHD
/Jk/B878OcN//gyxGKSHYVTjqMZRjaMaRzWOaqSXRgC0qVOpA6XqWgAAAABJRU5ErkJggg==
not found!
when i use this code :
<td>{!! DNS1D::getBarcodeHTML("$employee->barcode", "C39")!!}</td>
it gives me the following error:
This site can’t be reached
The webpage at
http://127.0.0.1:8000/admin/employees/J80302041D/more/excel might be
temporarily down or it may have moved permanently to a new web
address. ERR_INVALID_RESPONSE
the controller that i use to export the barcode:
public function excel($nr_personal,Request $request)
{
$employees = Employee::where('nr_personal',$nr_personal)->get();
$worker = Employee::where('nr_personal',$nr_personal)->first();
Excel::create('Employee Excel', function($excel) use ($employees, $worker) {
$excel->sheet('Excel sheet', function($sheet) use ( $employees, $worker) {
$sheet->loadView('admin.employees.excel')->with('employees', $employees)
->with('worker', $worker);
$sheet->setOrientation('landscape');
});
})->export('xls');
}
How can i export the barcode in a excel sheet??

Generate PDF from .docx generated by PHPWord

I am creating .docx files from a template using PHPWord. It works fine but now I want to convert the generated file to PDF.
First I tried using tcpdf in combination with PHPWord
$wordPdf = \PhpOffice\PhpWord\IOFactory::load($filename.".docx");
\PhpOffice\PhpWord\Settings::setPdfRendererPath(dirname(__FILE__)."/../../Office/tcpdf");
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
if (file_exists($filename.".pdf")) unlink($filename.".pdf");
$pdfWriter->save($filename.".pdf");
but when I try to load the file to convert it to PDF I get the following exception while loading the file
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Section.'
After some research I found that some others also have this bug (phpWord - Cannot add PreserveText in Section)
EDIT
After trying around some more I found out, that the Exception only occurs when I have some mail merge fields in my document. Once I removed them the Exception does not come up anymore, but the converted PDF files look horrible. All style information are gone and I can't use the result, so the need for an alternative stays.
I thought about using another way to generate the PDF, but I could only find 4 ways:
Using OpenOffice - Impossible as I cannot install any software on the Server. Also going the way mentioned here did not work either as my hoster (Strato) uses SunOS as the OS and this needs Linux
Using phpdocx - I do not have any budget to pay for it and the demo cannot create PDF
Using PHPLiveDocx - This works, but has the limitation of 250 documents per day and 20 per hour and I have to convert arround 300 documents at once, maybe even multiple times a day
Using PHP-Digital-Format-Convert - The output looks better than with PHPWord and tcpdf, but still not usable as images are missing, and most (not all!) of the styles
Is there a 5th way to generate the PDF? Or is there any solution to make the generated PDF documents look nice?
I used Gears/pdf to convert the docx file generated by phpword to PDF:
$success = Gears\Pdf::convert(
'file_path/file_name.docx',
'file_path/file_name.pdf');
You're trying to unlink the PDF file before saving it, and you have also to unlink the DOCX document, not the PDF one.
Try this.
$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
$pdfWriter->save($filename.".pdf");
unlink($wordPdf);
I don't think I'm correct..
You save the document as HTML content
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
After than you read the HTML file content and write the content as PDF file with the help of mPDF or tcPdf or fpdf.
Try this:
// get the name of the input PDF
$inputFile = "C:\\PHP\\Test1.docx";
// get the name of the output MS-WORD file
$outputFile = "C:\\PHP\\Test1.pdf";
try
{
$oLoader = new COM("easyPDF.Loader.8");
$oPrinter = $oLoader->LoadObject("easyPDF.Printer.8");
$oPrintJob = $oPrinter->PrintJob;
$oPrintJob->PrintOut ($inputFile, $outputFile);
print "Success";
}
catch(com_exception $e)
{
Print "error code".$e->getcode(). "\n";
print $e->getMessage();
}

Categories