I am generating Excel using PHPExcel.
All Code Works Fine.But Auto height code is not Working.
I have tried following code.
Apply row height on specific row
$objPHPExcel->getActiveSheet()->getRowDimension('7')->setRowHeight(-1);
Apply row height on for all row
$objPHPExcel->getActiveSheet()->getDefaultRowDimension(1)->setRowHeight(-1);
I have also tried word wrap property with it.
$objPHPExcel->getActiveSheet()
->getStyle('B7')
->getAlignment()
->setWrapText(true);
But it give me result as below:
Note : Working in MS office,Not Working in Apache open Office and
LibreOffice
Have just added the following to the 01simple.php example
$value = "To be or not to be-that is the question: whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and, by opposing, end them.";
$objPHPExcel->getActiveSheet()->setCellValue('A12', $value);
$objPHPExcel->getActiveSheet()->getRowDimension(12)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A12')->getAlignment()->setWrapText(true);
and this creates correctly wrapped output for both Excel2007 and Excel5 Writers
Related
I'm trying to copy styles from a range of cells in a template file and paste it to another range of cells in another file and then save the resulting file. I experimented on a small subset of my Excel file before I fully implemented it, but I found a strange result.
Here is my code snippet...
include "../class/PHPExcel.php";
include "../class/PHPExcel/IOFactory.php";
include "../class/PHPExcel/Cell.php";
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$frTemplate = $reader->load("document/Template/FR-001 Template.xlsx");
$frTemplate->setActiveSheetIndex(0);
$frTemplate->getActiveSheet()->setCellValueExplicitByColumnAndRow(3, 6, $year);
//copy style
$cellStyle = $frTemplate->getActiveSheet()->getStyle('E47:E49');
$frTemplate->getActiveSheet()->duplicateStyle($cellStyle, 'E52:E54');
//copy value
$cellValues = $frTemplate->getActiveSheet()->rangeToArray('E47:E49');
$frTemplate->getActiveSheet()->fromArray($cellValues, null,'E52');
$writer = PHPExcel_IOFactory::createWriter($frTemplate, "Excel2007");
$writer->save("document/Template/FR-001 2017.xlsx");
echo "Done";
Here is an image of the copied Excel file...
Copied File-Case 1
And here is an image of the target file...
Result-Case 1
I'm trying to apply the code with another data, and I found that -
"Merge cell" style cannot be copied
When a range of cells is copied, the only style copied is the first cell (upper left)
Sadly, I cannot post the evidence of the above case as my reputation is still very low (It is my first question in Stack Overflow!)
So, can you please tell me what is wrong with my code? Thanks in advance.
That's correct:
"Merge cell" is not an aspect of style; it's part of the structure of the worksheet, so it cannot be "copied" by duplicateStyle. If you want to merge cells, you need to use the mergeCells() method for each merge range that you want to set.
The duplicateStyle() method copies a style to a range of cells; it doesn't copy multiple styles to a new range. The getStyle() method allows a range for the purpose of setting a style against all cells in that range, but only returns the style of the first cell within that range. You need to duplicate each different style to its own range.
I'm trying to use PHPExcel to produce something which looks like this:
Label:
This is the value
Where "Label" is dark red, and "This is the value" is on a new line, in the same cell.
I've been using the following code:
$text = new PHPExcel_RichText();
$label = $text->createTextRun("This is the label: ");
$label->getFont()->setBold(true);
$label->getFont()->setItalic(true);
$label->getFont()->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKRED));
$text->createText(PHP_EOL . "This is the value");
$workbook->getActiveSheet()->getCell('A1')->setValue($text);
$workbook->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
But for some reason this doesn't seem to be working. I've tried "\r" and "\n" in place of PHP_EOL (being careful to use double quotes instead of single ones).
I've also tried moving the PHP_EOL to the label text.
If I call setValue($text . PHP_EOL) I lose the formatting, but the line break works.
I'm on a Mac using Excel 2016 if that makes any difference. Unfortunately I'm not able to test on a different version of Excel. Can someone show me how to fix this?
I have just tried your code on my Mac and it works fine but I have already patched several files of the phpExcel package.
I have unzipped the Excel file generated with phpExcel and compared the xml files with those from a regular Excel file and found some differences.
One of them is how bold is specified in the sharedStrings.xml file.
in a phpExcel generated file you have:
<b val="1"/>
and in a regular Excel file
<b/>
So in StringTable.php, in writeRichText, I have changed
// Bold 1
if($element->getFont()->getBold())
{
$objWriter->startElement($prefix.'b');
$objWriter->writeAttribute('val', ($element->getFont()->getBold() ? 'true' : 'false'));
$objWriter->endElement();
}
to
// Bold 1
if($element->getFont()->getBold())
{
$objWriter->startElement($prefix.'b');
$objWriter->endElement();
}
and I did the same for the other true/false properties.
It was not enough to solve my own problem of newlines disappearing in RichTextObjects, but i have given the other patches I've done in PHPExcel toRichTextObject only showing newlines after saving file again manually.
So that, when I use your code, the newlines are correctly set and visible.
Hope it will solve your problem!
Using Excel for Mac 2011 on latest OS X. Tried to add PHP_EOL and/or "\n".
$cellValue->createText(PHP_EOL);
$cellValue->createText("\n");
Neither is creating a new line when using PHPExcel_RichText.
I'm using the last PHPExcel version (7.7 i think). I'm able to generate my excel. I have to generate the pdf version with tcpdf, no choice here.
But the result is ugly and oversized.
See the gigantic font-size for 2012-000012, is 11 in excel.
As you can see, there is 2 problems here. The document is oversized, and the borders are crappy.
In order to get rid of the oversized side, i tryied this :
$this->printer->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
And this :
$this->printer->getActiveSheet()->getPageSetup()->setScale(50);
The scale is set to 50, but it do not affect output. So how can i get rid of these 2 problems ?
Here is a screen, of my excel How to set auto-line break PhpExcel? .
UPDATE :
By setting cellspacing to 0 borders are handled almost as expected, still a sligth problem, maybe caused by the oversized problem.
See :
Is there an opion or a method call to set it to 0 ?
This lies in the writer\html.php line 915, since i don't display gridlines.
To remove the borders that you find objectionable turn off grid lines on each sheet that you want to render in your the pdf.
For example:
$objPHPExcel->getActiveSheet()->->setShowGridlines(false);
to solve this problem I do two operations.
Step one is to set orientation for PHPexcel object
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
Step two - set different paper size for PHPExcel object:
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER);
Then convert PHPExcel object to object writer
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objWriter->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER);
This works for me. Also i changed font size inside PHPExcel object - just like this for headers
$objPHPExcel->setActiveSheetIndex($lastsheet)->getStyle("A1:K3")->applyFromArray(
array(
'font' => array(
'size' => 8
)
)
);
and for the rest of data:
$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(8);
Now im trying to get of ugly borders that tcPDF generates and one additional row at the end of file...
I'm using Excel5 in my project. I have already tried the following codes:
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->getARGB();
and
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getEndColor()->getARGB();
but these codes are returning wrong color. The getStartColor() always returning FFFFFFFF and FF000000 for getEndColor() instead of red.
I don't know what am missing. Can any one help me in figuring this out?
setReadDataOnly(TRUE) means read only the data from the cells, but none of the styling... and as background colours are part of the styling the reader will ignore background colours when it loads the file... if the fill style is not loaded, then the call to $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill() will return default fill style and colours.
Load the file with setReadDataOnly(FALSE) and you should find it works
EDIT
This is way beyond the scope of PHPExcel.... everything is populated via DDE, including most of the styling, so the underlying fill colour is plain (as returned by the PHPExcel getFill colour call) until the external executable TOS.exe populates the data and sets the styles accordingly. Your only option here is to use COM so that the workbook is being executed in MS Excel itself.
Your second code:
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getEndColor()->getARGB();
Works , it just returns a color hex code (FF0000).
RGB = Red Blue Green = xx xx xx
FF0000 is actually RED.
I am using xampp/wamp on windows and looking to convert an excel workbook to a html file.
I am not asking this question right away, i did a lot of research and finally managed to get to a point and got stuck here.
Am using php's COM library to open excel, then read a workbook and try to save it as html, how ever i am having issues with it
This is my code
$excel = new COM("Excel.Application",NULL,CP_UTF8) or die("Unable to instantiate Excel");
$excel->Application->Visible=1;
$excel->DisplayAlerts="False";
$workBook=$excel->Workbooks->Open(realpath("./example-03e-02.xlsx"));
$workBook->PublishObjects->Add(xlSourceSheet, "c:\\temp\\x.htm", "Sheet1", "", xlHtmlStatic, "test_27778", "");
$workBook->Publish (True);
$workBook->AutoRepublish(0);
$excel->Workbooks->Close();
$excel->Application->Quit();
$excel = null;
$workBook=null;
The PUlishObjects method keeps telling me that xlSourceSheet is not defined, i tried to pass it as a string "xlSourceSheet" but it keeps saying parameter type mismatch in one or the other. IN the above case, it says parameter 6 type mismatch;
if i remove the optional parameters like divid and title (the last 2) it shows a type mismatch on source range which empty obviously since am exporting a sheet.
Any body can shed some light on this and tell me what i am doing wrong.
Thanks
Anywyas, i managed to sort out the problem with some further digging into excel developer manual.
I just had to replace xlSourceSheet and xlHtmlstatic with their respective numbers that i found in the docs. xlSourceSheet is 1 and xlHtmlStatic is 0.
If anybody is looking for these codes, here they are
xlSourceAutoFilter 3 An AutoFilter range
xlSourcePivotTable 6 A PivotTable report
xlSourcePrintArea 2 A range of cells selected for printing
xlSourceQuery 7 A query table (external data range)
xlSourceRange 4 A range of cells
xlSourceSheet 1 An entire worksheet
xlSourceWorkbook 0 A workbook
xlHtmlCalc 1 Use the Spreadsheet component. Deprecated in Excel 2007.
xlHtmlChart 3 Use the Chart component. Deprecated in Excel 2007.
xlHtmlList 2 Use the PivotTable component. Deprecated in Excel 2007.
xlHtmlStatic 0 Use static (noninteractive) HTML for viewing only.
$object=$excel->ActiveWorkbook->PublishObjects->Add(1,"c:\\temp\\x.htm","Sheet1",0)->Publish(1);
Thanks cweiske :)