exporting csvs in php. issue with filename - php

I'm trying to export csvs in php using company names from a database.
I'd like to keep as much of the formatting of a company's name as I can. Companies like AutoDesk, Inc. Are trouble some because of the comma and the period. Even the space is an issue.
header("Content-Disposition: attachment; filename=AutoDesk Inc.csv");
In this case (even when I remove comma and period) my filename stops right before the space. I only get AutoDesk
Is there any way to keep my filename intact when exporting a csv?

Try:
header("Content-Disposition: attachment; filename=\"AutoDesk Inc.csv\"");

Related

Remove Double quotes from text when exporting csv file via PhpExcel and open it with notepad

I am exporting the csv file using PhpExcel but when I open it with notepad it shows Double quotes around the text. I do not want these double quotes.
Can anyone help me?
Below is demo output from that file:
"Credits","Modified_User","Plain_Course_Name","Popular_Courses","ISBN_9","ISBN_7","Course_Number","ISBN_8","Book_2_REQUIRED_or_OPTIONAL","ISBN_5","ISBN_6","Course_Status","ISBN_3","ISBN_4","Book_9_REQUIRED_or_OPTIONAL","Bookstore_2_Course_Status","ISBN_2",
to generate csv, I am using below code:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'csv');
If I use following code then the output in notepad is correct but it will change the data order in excel csv file.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV')->setDelimiter(',')
->setEnclosure('"')
->setLineEnding("\r\n")
->setSheetIndex(0)
->save(__DIR__.'/'.$file_name);
So, from what I understand of PHPExcel, the text enclosure is set by default to use ", which is why you are having " around your text in your file. The second snippet of code hits on directly what you need though, so you were close!
You should be able to use $createWriter->setEnclosure('') in order to print your items without double quotes.
In your code that would look something like this:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV')->setDelimiter(',')
->setEnclosure(' ')
->setLineEnding("\r\n")
->save(__DIR__.'/'.$file_name);
I'm not sure if you need the setLineEnding, especially if it is one line. Perhaps for later parsing you might. I hope that this information helps out!
There is additional documentation on the subject on their gitHub: https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md

Make PHP exported CSV with UTF-8 character work on mac excel using commas

So I'm trying to export a csv using PHP in which the contents contains UTF-8 character and I want the resultant csv to open in Excel smoothly (including Mac excel)
So there is an answer here: How can I output a UTF-8 CSV in PHP that Excel will read properly?
Checkout the top answer.
But then in order to implement that you need to use tabs to separate the fields instead of commas...Is there a way to achieve this while still using commas and not tabs and still have it work in OS X
EDIT
Mostly to Mark Baker but everyone feel free to comment
Another code update
while(#ob_end_clean());
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=fileexport.csv");
echo "\xEF\xBB\xBF";
print "sep=,\n";
print $output;
exit;
fputcsv should work fine in this instance. Take the following example, where as the third parameter of fputcsv is the delimiter. By default it is , (comma), but you could also use "\t" for tab files. CSV files should be interpreted the same on either OS
if( $fh = fopen("output_file.csv","w") ){
$put = array("column1, with comma","column2, with comma","column3" /*,"columnN"*/);
fputcsv($fh,$put,",");
fclose($fh);
}

Hexadecimal String to binary PDF file

I have a hexadecimal value that is a PDF that I am getting from a web service that I am trying to save locally using PHP. The below is a snippet of the value.
I have tried to achieve this using pack in PHP either receive an error that "x" is not valid or the pdf will not save correctly. It will be empty or says error opening.
Partial Value is: "0x255044462D312E340A0A322030206F626A0A3C3C2F5479"
I have tried the following unsuccessfully after searching google for some time:
$pack = pack("h*", $string);
file_put_contents('my.pdf', $pack);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $pack;
Can anyone tell me what I am doing incorrectly? I realize this is not the entire PDF but I cant put entire online.
Thanks for any help

Apply text wrap to CSV cell containing long string

i am using php code to export data to CSV file.Everything is working fine as required.but problem is that when there comes long text in a cell.I want to wrap text so that i can increase cell size to handle long text.Below is my code.
header("Content-Type: application/force-download\n");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Content-Disposition: attachment; filename=store_earning_report.csv");
echo "Notes \n";
echo $Notes;
echo "\n";
exit;
I have searched but didn't find any solution.Is there any way to handle this problem.
Thank you.
Make sure you are including a comma "," after each field, and "\r\n" to trigger a new line in the .csv file that is created.
A .csv is just a text file with commas used to separate the field values - So there is no way you can control the cell sizes that will appear when the file is first opened in Excel.

PHP generate Excel/CSV file and send as UTF-8

I'm retrieving data from my Postgres DB in UTF-8. The db and the client_connection settings are in UTF-8.
Then I send 2 headers to the visitor:
header("Content-Type: application/msexcel");
header("Content-Disposition: $mode; filename=export.xls");
and start outputting plain text data in a CSV-manner. This will open as a simple Excel file on the visitors desktop.
$cols = array ("col1", "col2", "col3");
echo implode("\t", $cols)."\r\n";
Works fine, untill special characters like é, è etc are encountered.
I tried changing my client_encoding while retrieving the data from the db to latin-1, which works in most cases but not for all chars. So that is not a solution.
How could I send the outputted file as UTF-8? I don't think converting the data from the db to latin-1 is possible, since the char seems unknown in latin-1 ... so I need Excel to treat the file as UTF-8
I'd look into using the PHPExcel engine. It uses UTF-8 as default and it can generate a whole list of spreadsheet file types (Excel, OpenOffice, CSV, etc.).
I would recommend not sending plain-text and masquerading it as Excel. XLS files are typically binary, and while binary isn't required, the official Excel method of using non-binary data is to format it as XML.
You mention "CSV" in the title, but nothing about your problem includes anything related to CSV. I bring this up because I believe that you should actually change your tabs to commas, and then you could simply output a standard .csv file, which is read by Excel still but doesn't rely on undocumented or unstable functionality.
If you truly want to send application/msexcel, then you should use a real Excel library, because currently, you are not creating a real Excel file.
use ; charset=UTF-8 after aplication/xxxxxx I do use:
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
// header("Content-Length: " . strlen($thecontent)); // this is not mandatory
header('Content-Disposition: attachment; filename="file.xls"');
Try mb_convert_encoding function.
Try to use iconv, for converting string into required charset.
Have you tried utf8_encode() the string?
So something like: echo implode("\t", utf8_encode($cols)."\r\n")
Not sure if that would work, but give it a go

Categories