PHPExcel converter not working fine - php

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

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?

Change excel file extension from xls to xlsx while uploading with php

Is it possible and if is how can i change excel file extension while uploading or before saving file on server? I am using php and mysql.
Thankyou
You can do something like this.
move_uploaded_file($_FILES['file']['tmp_name'], upload_PATH.'/'.$_FILES['file']['name'].'x');
But that will only change the file name with the xlsx extension. It will not actually convert the file to xlsx format.
As previously mentioned in a different reply, changing the extension won't actually change the format, and it's not a good idea to serve a .xls file as .xlsx, since this will only confuse anyone trying to read it.
What you could do (disregarding potential problems with converting and verification of the file) is read the uploaded file into a library like PHPExcel (http://phpexcel.codeplex.com) and then use the builtin functions to export it as an .xlsx file. Sample below:
// Create a reader to read .xls format
$reader = PHPExcel_IOFactory::createReader('Excel5');
// Read the .xls file from upload storage
$workbook = $reader->load($_FILES['file']['tmp_name']);
// Create a writer to output in .xlsx format
$writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
// Save file to destination .xlsx path
$writer->save($destination_path);
Keep in mind that although this might work perfectly well, the conversion might mess with the contents of the file. This might not be desirable, as the conversion can cause data loss, formatting changes and all sorts of weirdness.

PHPExcel file not in the proper Microsoft Excel 2003 XLS format

I'm generating a Excell 2003 using PHPExcel 1.8 with this code:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('toUploadFtp.xls');
The file seems ok, and I can read it using MS Office and LibreOffice.
But I have to upload the file to a system that says the format has to be Excell 2003, and he prompts next error:
Oops! Your file is not in the proper Microsoft Excel 2003 XLS format.
If I open the file with LibreOffice and save it again, then I can upload the file correctly to the system.
Any idea?
Thanks,
Create your file by passing the proper version to factory:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
Hope it helps.
Not understanding the exact problem entirely: it seems to be with the additional transfer of the Excel file.
One error is often, that the PHP script outputs spurious data.
Leaving out the final %> is a standard trick to make sure no extra final new line characters are added to the file.
Comparing the files generated and transferred should yield insight on what went wrong.
You may need to set the content to non-text to prevent some text conversion:
header('Content-Type: application/octet'):
Finally I couldn't find a solution. It has to be some kind of bug in the XLS 2003 format.
But I generated a XLSX with $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); and the system recognizes it correctly.
I think you can overcome this problem in changing the filed of Excel5.Since i havnt tried phpexcel i dont know much about this..But i think you can find a solution for this [here].1

PHPExcel create PDF

I am having a problem creating a PDF using PHPExcel. I can create the excel and pdf file fine, but its the characters on the pdf that isn't working properly. Image below.
If I copy any of the text and paste it somewhere it reads as normal text and not gibberish anymore, which is very strange. My code is below.
$newpdf = "reports/risk-bordereaux-".$time.".pdf";
$objPHPExcel = PHPExcel_IOFactory::load($newxlsx);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToWidth(true);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToHeight(true);
$objPHPExcel->getActiveSheet(0)->setShowGridlines(false);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save($newpdf);
I'm wondering is there any way I could set the character encoding or something to stop this happening? Any help would be greatly appreciated!
EDIT
Link to bigger image
I am using TCPDF to generate PDF's

Rows written by PHPExcel cannot be read from other libraries

I know this isn't the right place to ask about this specific vague problem, but maybe someone knows this library well enough to enlighten me. Here is the thing:
I am writting an Excel5 over an existing Excel file with PHPExcel. I need to upload that Excel to the Zoom website, so it can provide me with a list of tracking numbers. However, for some reason the library they are using to read the uploaded Excel files cannot read the rows written by PHPExcel and the only solution I've found so far is to manually copy the contents of my dynamically generated Excel to another document using MS Excel 2007.
In other words, the Zoom website can read the rows written natively by Excel but not rows written by PHPExcel. My file has only one single sheet, and I can open it no problem with Excel 2007.
Even if I manually add some rows to the template and then add more rows with PHPExcel, Zoom will read the rows written manually by me, but not the rows written by PHPExcel.
This is how I'm doing it:
// Starting with the PHPExcel library
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$template_file = 'zoom_tracking_template.xls';
$i = 3;
$objReader = IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($template_file);
$objPHPExcel->setActiveSheetIndex(0);
// Fetching ML payments
foreach($payments as $row)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, 'VANESSA NEISZER');
$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, '02127616116');
$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, '1ER PISO MINITIENDAS 199 BLVD SABANA GRANDE, CRUCE C / CALLE NEGRIN');
$objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $row->mailing_city);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$i, $row->mailing_name);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$i, $row->mailing_name);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$i, $row->mailing_personal_id);
$objPHPExcel->getActiveSheet()->setCellValue('H'.$i, $row->mailing_phone);
$objPHPExcel->getActiveSheet()->setCellValue('I'.$i, $row->mailing_address1.' '.$row->mailing_address2);
$objPHPExcel->getActiveSheet()->setCellValue('J'.$i, $row->nickname);
$objPHPExcel->getActiveSheet()->setCellValue('K'.$i, '1');
$objPHPExcel->getActiveSheet()->setCellValue('L'.$i, '0.3');
$objPHPExcel->getActiveSheet()->setCellValue('M'.$i, 'M');
$objPHPExcel->getActiveSheet()->setCellValue('N'.$i, 'PRODUCTO');
$objPHPExcel->getActiveSheet()->setCellValue('O'.$i, '0');
$i++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
// Sending headers to force the user to download the file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Envios'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
I have no clue of what PHP library they are using to read Excel files and I am certain they wont tell me if I ask them. I know they use PHP, and their library only read Excel 2003 files, however, I don't know why they can't read my files but they can read other files written manually on MS Excel.
Any clues, ideas or suggestions I could try would be greatly appreciated.
And PHPExcel's main developer is looking at this issue (among others), somewhere in between trying to find a new day job and having a life. I'm not familiar with the zoom website, or the software that they use. PHPExcel BIFF8 files can be read by Excel, OOCalc and Gnumeric without error... but a couple of questions spring to mind.
What version of PHPExcel?
Does any of the data contain UTF-8 characters?
Are there any formulae in the template worksheet?
If so, what are they?

Categories