PHPExcel setWrapText(true) not working - php

I have a serious problem with text wrapping via PHPExcel. I have a column, which contains texts in new lines. It does the linebreaks in LibreOffice. In MS Office it is displayed in a single line. In both viewers, it only does the wrap, when I double click into a cell and then click out the cell. I have the following code:
foreach($view->results as $row){
//...
foreach($unserialized as $task){
$value = $field_info['settings']['allowed_values'][$doc['document']];
$current_tasks .= $value . "\n";
}
$active_sheet->setCellValue($letter.$i, $current_tasks);
//...
//end of main foreach loop
$active_sheet->getStyle('L' . $i)->getAlignment()->setWrapText(true);
$i++;
}
//tried this too outside the foreach:
$active_sheet->getStyle('L2:L' . $i)->getAlignment()->setWrapText(true);
They don't seem to be working. Am I doing something wrong? I googled it up and neither of the solutions worked for me.

I only needed to set the height of the rows.
$numtasks = 20;
foreach($unserialized as $task){
$value = $field_info['settings']['allowed_values'][$doc['document']];
$current_tasks .= $value . "\n";
$active_sheet->getRowDimension($i)->setRowHeight($numtasks);
$numtasks += 20; //20 is for 1 cells height in pixels
}

Related

PHPExcel Remove cell (column) removeColumn() not working

i created export PHPExcel, but here i want to remove cell "A" here is piece of my code
$spareparts = $this->spareparts_m->getDataSpareparts();
if ($spareparts) {
foreach ($spareparts as $key => $sparepart)
{
$rownum = $key + $row_geser;
$this->excel->getActiveSheet()->getStyle('A' . $rownum.':W'. $rownum)->getFont()->setSize(10);
if(isset($flip[1])){
$this->excel->getActiveSheet()->setCellValue('A' . $rownum, $sparepart['part_no']);
}else{
$this->excel->getActiveSheet()->removeColumn("A");
}
/* Blah..blah.blah */
but here $this->excel->getActiveSheet()->removeColumn("A");
not working and my excel output became like this screenshot output excel
any solution please ?
Thank you
If you want to remove the column A start it with column B.
something like this...
$this->excel->getActiveSheet()->setCellValue('A', 'Sample');
To: $this->excel->getActiveSheet()->setCellValue('B', 'Sample');
then change the correspond letter to it. you need to set it manually + the row number added from your foreach function.

php excel export text broken new line

I have array data set in column row and export excel but if long text added then line broken.
Can you please suggest how to long text wrap?
$heading = false;
if(!empty($records)) {
foreach($records as $row) {
if(!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
}
Instead of:
echo implode("\t", array_keys($row)) . "\n";
use Chr to return ASCII Code 13:
echo implode("\t", array_keys($row)) . chr(13);
or use PHP_EOL:
echo implode("\t", array_keys($row)) . PHP_EOL;
Edit:
Line breaks will only show in Excel for cells that have "Wor Wrap" enabled.
In your output Excel file:
highlight the data that was output by PHP
right-click on the highlighted cell(s)
choose Format Cells
go to Alignment tab
check box Word Wrap.
If that fixes the issue then your data is (correctly) breaking the line, but it's just Excel's word-wrap default that's preventing the line breaks. You can set it programmatically but it would need a different approach in writing to the file. An easier solution is setup the formatting (including Word Wrap) in the file prior to writing to it, like a template.

php generate data from foreach loop on txt file with table formatted

I had tried to generate txt file from php foreach loop,
it works but the result is not parallel like if used table here:
My expectation like this :
This is my code :
$output[] = ''."\n".'';
$output[] = ' No '.' Name Product '.' Total '."\n".'';
foreach($query->result() as $row){
$results = $row->id_product;
$product = $this->db->query("select product_name from master_product WHERE product_id='".$results."'");
$product_name = $product->row()->product_name;
$output[] = ' '.$no.str_repeat(" ",20).''. $product_name.str_repeat(" ",20).''.$row->quantity.' '.$row->scale."\n".'';
$no++;
}
file_put_contents(APPPATH."txt/test.txt", $output);
Please someone help me.
Why don't you implement a function like this:
function padColumnText($text, $colWidth)
{
if (strlen($text) > $colWidth)
{
return substr($text, 0, $colWidth);
}
return $text . str_repeat(" ", $colWidth - strlen($text));
}
Then, any time you want to print a column, do it like this:
output[] = padColumnText($no, 5) . padColumnText($product_name, 20) ...
I'm assuming you want this printed to some kind of plain text file with a fixed width font. If you're doing something for the web, this is the wrong approach (you should use HTML for your formatting).

Search a cell by string in PHPExcel

I want to know if it's possible to get a cell by its name in an xls document, I mean Ii have this info in a excel file:
Normally to get the coordinate of the cell with the value "ASUS"
$objPHPExcel->getActiveSheet()->getCell('B3')->getValue();
my problem is that users send my this excel file, and sometimes the rows are in disorder, e.g the row B3 sometimes appear in a different row like "B6" or "B7" or "B5", how I can get the cell "ASUS" getting by cell name "Modelo"
There is nothing built-in to PHPExcel to do a search, but using the following example, it can do well for your problem.
$foundInCells = array();
$searchTerm = 'ASUS';
foreach ($objPHPExcel->getWorksheetIterator() as $CurrentWorksheet) {
$ws = $CurrentWorksheet->getTitle();
foreach ($CurrentWorksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) {
if ($cell->getValue() == $searchTerm) {
$foundInCells[] = $ws . '!' . $cell->getCoordinate();
}
}
}
}
var_dump($foundInCells);
You'd have to loop through the entire file to find it just like searching a value in a 2D array.
Take a look at this answer

Exporting to csv formatting issue

I'm trying to export some data into a csv file, I've got the data going into the file, this is my code:
*Please excuse me if the code is bad, I've never done anything like this and this was the only way I could get it working.
/* CSV Export - Create Row */
$csv_row_content .= $userdata->id.',';
$csv_row_content .= $userdata->firstname.' '.$userdata->lastname;
$csv_row_content .= ','.$split_date[2].'-'.$split_date[1].'-'.$split_date[0].',';
$csv_row_content .= $sale->name;
if($sale->optionlabel):
$csv_row_content .= ' ('.$sale->optionlabel.')';
endif;
$csv_row_content .= ',';
$csv_row_content .= $sale->status.',';
$csv_row_content .= number_format($sale->unitprice, 2, '.', '');
$csv_row_content .= "\r\n";
$data_array[] = $csv_row_content;
$csv_file = fopen('../wp-content/plugins/data-export/export_doc.csv', 'w');
foreach ($data_array as $single_line)
{
fputcsv($csv_file,split(',',$single_line));
}
fclose($csv_file);
/* Clear Array */
unset($data_array);
$data_array = array();
It's working except I'm having trouble with the quotations marks on certain items
303,"User Name",12-02-2013,"College Sweater (Black)",,"20.00
207","User Name",30-01-2013,"College Sweater (Black)",,"20.00
"
So I'm not sure what the go is with the first and last items, the one quotation mark show up sometimes and not in others.
Notice the odd quotation mark on row id 207 & on the last value for both row.
Also there's a new line begin made on the third row with just a single quote.
Also on some other items the function is splitting the name of the item into two items. eg:
207","User Name",30-01-2013,"College ","Sweater (Black)",,"22.73
So obviously I'm off base here somewhere, if anyone could help me with this, I'm really keen on learning the correct way this kind of thing should be done, checked the php.net docs quite a bit, but a lot of the time I find that resource incredibly overwhelming and this is one such occasion.
If anyone can point me in the right direction on this I'd really appreciate it.
I'd prefer to understand this than just have a copy and paste solution.
Thanks,
Frank
You are manually creating a CSV string and splitting it before using fputcsv to put it back together. Try this instead:
$row = array(
$userdata->id,
$userdata->firstname . ' ' . $userdata->lastname,
$split_date[2] . '-' . $split_date[1] . '-' . $split_date[0],
$sale->name . ($sale->optionlabel ? $sale->optionlabel : ''),
$sale->status,
number_format($sale->unitprice, 2, '.', '')
);
$data[] = $row;
$csv_file = fopen('../wp-content/plugins/data-export/export_doc.csv', 'w');
foreach ($data as $line) {
fputcsv($csv_file, $line);
}
fclose($csv_file);
This creates an array containing all the fields which can be passed to fputcsv which takes care of enclosing and delimiting fields as well as line endings.

Categories