Apply text wrap to CSV cell containing long string - php

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.

Related

XAMPP, PHP: echo adds two spaces to blob read from database

I read blob (word 2010 document) from mysql database and store it in $data variable. When I simply store that data directly in PHP like so:
file_put_contents('c:\\temp\\dump.docx', $data);
I can open dump.docx in Word (size matches original file). If I attempt to send $data like this:
ob_start();
header('Content-disposition: attachment; filename=' . $name);
header('Content-type: ' . $type);
ob_clean();
echo $data;
ob_end_flush();
exit;
The stored file is two bytes longer. There are two spaces in front:
To check if I somehow do not output those spaces, I called ob_get_contents() just before echo and dumped content to a file. File has zero bytes.
So it looks like echo is producing those two bytes.
Here's post that helped me:
https://drupal.stackexchange.com/questions/163628/extra-space-at-beginning-of-downloaded-image/163644
ob_start was already called ealier. I needed to call only ob_clean() before sending content.

exporting csvs in php. issue with filename

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\"");

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);
}

How to fix "tabs" in Excel Export (PHP)?

I applied this solution to solve an encoding problem I got exporting files to .CSV and Excel and it worked successfully, but now another problem emerged:
The tabs ("\t") I use in PHP as the delimiter for Excel files stopped working. It was working before I solve the previous problem. When I open the excel file, it display likes:
"ColumnAColumbBColumnC" (all together).
If a export it like csv ('Content-type: text/csv; charset=UTF-8') I have success, but not as Excel (Content-type: application/vnd.ms-excel; charset=UTF-8).
Any solution for this?
Note: using "," as a separator is not a good solution for me, because I have values with "," in some fields.
The tabs ("\t") I use in PHP as the delimiter for Excel files stopped working.
Yes, that is correct.
Despite this there is still a simple way to export to Excel.
Use a table with <td> and <tr>, this does excel okay.
An example:
<?php
header("Content-Type: text/plain");
echo "<table border='1'>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>First Name</th>";
echo "<th>Department</th>";
echo "<th>Date</th>";
echo "<th>Topic</th>";
echo "<th>State</th>";
echo "<th>E-mail</th>";
echo "<th>Place</th>";
echo "<th>Registration fee</th>";
echo '</tr>';
echo '</table>';
header("Content-disposition: attachment; filename=example_export_to_excel.xls");
}
?>
This works perfectly for export to excel.
There are no libraries needed, why look complicated things simple as it can.
If you have a comma in a string field and want to use a comma as a separator, then you enclose the string in quotes... that's how CSV files work.
So my assumption is that you aren't using PHP's built-in fputcsv() function to create your csv file, else you'd already know this... why not consider using it, because it handles all the little intricacies that you don't seem to be aware of.
Note that Excel csv files do use a tab, and are normally encoded as UTF-16LE with a BOM rather than as UTF-8; and there is no need to identify the charset in the content type header.

What is the benefit of "echo" chunks, while ouputing a file?

What is benefit and difference between the following:
Statement 1:
header("Content-type: image/jpeg");
header('Expires: ' . date('r',time() + 864000));
header("Pragma: public");
header("Cache-Control: public");
header("Content-Length: " . strlen($contents));
$splitString = str_split($contents, 1024);
foreach($splitString as $chunk)
echo $chunk;
Statement 2:
header("Content-type: image/jpeg");
header('Expires: ' . date('r',time() + 864000));
header("Pragma: public");
header("Cache-Control: public");
header("Content-Length: " . strlen($contents));
echo $contents;
Due to the way TCP/IP packets are buffered, using echo to send large strings to the client may cause a severe performance hit. Sometimes it can add as much as an entire second to the processing time of the script. This even happens when output buffering is used.
If you need to echo a large string, break it into smaller chunks first and then echo each chunk. So, using method 1 to split the string OR using substr to split it and sending to client performs faster for large files than method 2.
Statement 1 adds unnecessary overhead and obfuscation.
in the first statement content divide into 1024 bytes chunks and one chunk have 1024 bytes content and in the second statement detemine the length of whole content ant then echo this but in first divide in chunk and then echo with help of for each one by one.
If the $contents is very large, you can echo it in chunks to stop the whole thing being echo'd at once.

Categories