How to avoid Excel "bad format error" when saving spreadsheet - phpspreadsheet

Using PHPSpreadsheet saving XLSX format works OK running the default code
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("filename.xlsx");
But if I want to have the user to select the target directory using
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="filename.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadSheet, 'Xlsx');
$writer->save('php://output');
The file saves OK but Excel 2016 does not want to open it. Excel returns the following error
Excel Error
I looked through all documentation and posts but cannot find the solution.
Thanks !
Edit: Just in case, this solution does not work for me.
Edit 2: The sample provided Simple Download Xlsx works perfectly, but when doing a copy/paste for my spreadsheet, Chrome gives me a
Resource interpreted as Document but transferred with MIME type application/octet-stream
Edit 3: Used
ob_end_flush(); to clean any left over header in my code.
The file now saves OK, but needs repair when opening in Excel. Why ?
Thanks

Solution:
Bug from PhpSpreadsheet.
When using
header("Content-Type: application/vnd.ms-excel");
i.e. compatibility mode for Excel, the file opens OK.

Related

DropZone: How to Accept .csv Files?

I've read quite a few answers on SO about this, and most advise using the acceptedFiles property to specify the accepted mime types.
However, the DropZone docs say:
Mime type determination is not reliable across platforms. CSV files,
for example, are reported as text/plain under macOS but as
application/vnd.ms-excel under Windows. In some cases there might not
be a mime type set at all.
I'm trying to upload a .csv file, and (using material-ui-dropzone), so far I've tried:
<Dropzone
acceptedFiles={['.csv', 'text/*']}
showPreviews={true}
showFileNamesInPreview={true}
/>
<Dropzone
acceptedFiles={'.csv', 'text/*'}
showPreviews={true}
showFileNamesInPreview={true}
/>
<Dropzone
acceptedFiles={'.csv', 'text/csv'}
showPreviews={true}
showFileNamesInPreview={true}
/>
...etc., but so far none are working:
The open file dialog shows .csv files as grayed out
Dragging-and-dropping a .csv file to DropZone gets a "File SeriesNotes.csv was rejected. File type not supported." message
What is the correct way to solve this for material-ui-dropzone (or for any version of DropZone)?
After lots of trial and error, this worked for me.
acceptedFiles={[".csv, text/csv, application/vnd.ms-excel, application/csv, text/x-csv, application/x-csv, text/comma-separated-values, text/x-comma-separated-values"]}
The main one on windows was the .csv the rest are just in case.

PHPEXCEL weird characters on form inputs

I need some help with PHPEXCEL library, everything works great, I'm successfully extracting my SQL query to excel5 file, I need to give this file to transport company in order to auto collect informations about packages, unfotunately the generated excel file has some ascii characters between each letter of the cell text, and when the excel file is imported you need to manually delete these charaters.
If I open the excel file, everything is fine I see: COMPANY NAME, If I open the excel file with notepad++, I see the cell values this way: C(NUL)O(NUL)M(NUL)P(NUL)A(NUL)N(NUL)Y N(NUL)A(NUL)M(NUL)E
If I open again the file with excel and save, then reopen with notepad++ I see COMPANY NAME.
So I do not understan why every time I create an excel file using PHPEXCEL my every letter of all words are filled with (nul) every letter.
So how do I prevent the generated excel file to include (nul) between every word????
Also if you open the original excel file generated from PHPExcel samples are also filled with (nul) and if you open and save it, the (nul) is gone.
Any help would be appreciated, thanks.
what is the (nul) ??? 0x00??? char(0)???
ok, here is the example:
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('Disponibile solo su browser');
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Solidus")
->setLastModifiedBy("Solidus")
->setTitle("Import web")
->setSubject("Import File")
->setDescription("n.a")
->setKeywords("n.a")
->setCategory("n.a");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueExplicit("A1", "COMPANY")
->setCellValue('A2', 'SAMSUNG');
$objPHPExcel->getActiveSheet()->setTitle('DDT');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="TEST.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Cache-Control: private',false);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');
As you can see from this little example, this scripts creates a file excel5 with 2 cells, A1 = COMPANY, A2 = SAMSUNG
when I send this file to the transport company, they import the file into their system, but as you can see from the picture, there is an weird character between each letter.
so I noticed every time I open the generated Excel5 with notepad++ file I get:
S(nul)A(nul)M(nul)S(nul)U(nul)N(nul)G
If I save the save with excel and then open it again with notepad++ I get:
SAMSUNG
and this file is ok for the transport company
so my question is, how should I avoid the file generated to contain thi '(nul) charachter between each letter????
some help?
weird characters
SAMSUNG
I found the soluion by myself, I explain just in case anyone has also this problem:
there is not way to change the way the excelfile is encoded by PHPEXCEL
so I figured out the problem was reading the file, I did some simulations and reproduce the problem, every time a read the file and put the result into inputs a get weird characters:
C�O�M�P�A�N�Y�
If I set the output enconding enconding as follows:
$excel->setOutputEncoding('UTF-8');
the file loads fine, so the problem was not creating the excel file, but reading the excel file.
If I print the variable with ECHO I get: "COMPANY",
if I put the variable on input as value I get: "C�O�M�P�A�N�Y�"
setting the output solves the problem, but I would like to know why the difference when I put the variable on input as value, thanks

Ole::Storage::FormatError: OLE2 signature is invalid

I want to read an Excel File in my Rails Application.
This is how I open my Excel file and read it.
doc = Spreadsheet.open('./try.xls', "r")
sheet = doc.worksheet 0
sheet.each do |row|
array_rows << row.to_a
end
I have it as a rake task.When I try to Read this file it throws an error.
Ole::Storage::FormatError: OLE2 signature is invalid
What is happening? what should I do?
The .xls file must be saved in EXCEL 2003 format. So
File-->Save As
from All Formats dropdown select the Excel year 2003
This solved my problem
On Mac I had to save it as Excel 97-2004(.xls) to get it to work

nicEdit Uploading Locally - Issues with nicUpload

If anyone has managed to get locally uploading images I'd be mightily appreciative of some help.
I've downloaded the latest version of nicEdit along with the nicUpload plug in (from nicedit.com - Version 0.9 r24 released June 7th, 2012).
I've also downloaded nicUpload.php from http://svn.nicedit.com//trunk/nicUpload/php/nicUpload.php
NicUpload.php - I've set NICUPLOAD_PATH and NICUPLOAD_URI both to 'images' which is the subfolder of where nicupload.php and nicEdit.js are located.
NicEdit.js - I've added the following to line 271:-
uploadURI : 'nicUpload.php?id=123',
I've given it an ID otherwise it was failing with an invalid ID code. But the ?id=123 isn't meant to be there. I've also set the iconsPath accordingly.
Line 1370 I've switched this:-
nicURI : 'http://api.imgur.com/2/upload.json',
for this:-
nicURI : 'http://www.mydomain.com/nicedit/nicUpload.php',
But I'm still getting "Failed to upload image". I've searched and searched and searched for answers to this and I'm getting close to having spent two days tinkering with it.
With a few debugging displays I can see that it's failing on line 46 of nicUpload.php where it says:-
$file = $_FILES['nicImage'];
$image = $file['tmp_name'];
$max_upload_size = ini_max_upload_size();
if(!$file) {...
That last IF is true and that's where it exits with the error.
Appreciate anyone being able to help.
The nicUpload.php script file laying around sucks and I don't even understand how it could work.
NicEditor uses imgur as the default image upload service. The source code follows the API format described here: http://api.imgur.com/resources_anon#upload
My suggestion would be to implement the API request and response defined there.
I did not use the niceedit upload function to do what you want. I managed to add a button to the link and img dropdown menu. The button opens a file manager window where you also can upload. I managed to put then de url of the image or document into the nicedit drop down img or url window. That is how I solved the problem.

Clarification on what can be exported to excel on ipad

Trying to fix an old .asp site to work on an ipad. One of the features is the users ability to download their search results into an excel worksheet. The code uses:
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;filename=results.xls"
Response.CharSet = "iso-8859-1"
When viewing the site on the ipad, when the link is click for the page with the code above it does nothing, just spins. Is it the fact that I am trying to export the data as excel, I have read in some posts how it is the encoding! Should I convert the code to export the results page as a csv file and then allow the user to open it in anything they want/have available? What's the best way to do it to hit the most devices...
Thanks
In the past i'd a same scenario so what i did:
FILE: DOWNLOAD.ASP
<%
' get the file to download
myFile = request.querystring("File")
myFullPath = "c:\name_folder\" & myFile ' example of full path and filename
' set headers
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" & myFile
' send the file using the stream as
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(myFullPath)
Response.BinaryWrite adoStream.Read()
adoStream.Close
Set adoStream = Nothing
%>
FILE: HTML
Download Excel file
This example is full working with Ipad using the native browser Safari.
The file Result.xls is downloaded and loaded in the Viewer whitout the capability to be edit.
My iPad users use the App QuickOffice to let the file be saved in a virtual folder, rename the file, delete, ... but they cant edit the file, that App is just for manage the files and isnt required for download the file.
If your user need also edit the XLS file on the iPad i suggest to use (for example) the Google App Document, it let the user to edit and manage the file directly in the browser.
Hope it help

Resources