PHPexcel set cellspacing - php

When I print to pdf, my cells have some spacing between them, which I cannot get rid of.
Basically I have similar problems as this:
How to generate a nice PDF with php Excel? (see the first picture)
which is described as solved
By setting cellspacing to 0 borders are handled almost as expected,
So I guess I need to set cellspacing to 0, it's just that I can't find out how to do that.
Update:
I found this http://phpexcel.codeplex.com/discussions/249376. But the solution involves messing with the source code of PHPexcel, I would much rather find a way to achieve this within my project, not changing the PHPexcel files..

See lines 1028 to 1032 of the HTML Writer. Cellspacing is set to 0 unless you're writing to PDF and getShowGridLines() for the sheet is true (which is the default value), in which case it's set to 1... so calling setShowGridLines(false) for the worksheet(s) before saving should set cellspacing to 0.

Related

Set auto height in PHPExcel not working

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

FPDF right margin values have no effect

I am using the PHP extension FPDF to create PDF documents. I believe I have the margins set to 1" all the way around with justified text, but when I create a PDF document, download it and open it in Acrobat, I see the left margin is 1" but the right margin is about 1.2". I've gone through fpdf.php looking for the issue and cannot find it.
$oPdf = new myPDF('P', 'mm', 'letter');
$oPdf->Open();
$oPdf->SetMargins(25.4,25.4,25.4,25.4);
//set default font/colors
$oPdf->SetFont('Times', '', 12);
$oPdf->SetTextColor(0,0,0);
$oPdf->SetFillColor(255, 255, 255);
//add the page
$oPdf->AddPage();
$oPdf->AliasNbPages();
If I change the top and left margin values, the document reflects the changes. If I change the bottom or right, there is no change. I've seen how the bottom can be changed wth set auto default page break, answered elsewhere. My question is how can I change the right margin and it actually take effect in my document? I have a feeling that this has to do with the measurement of the text when FPDF calculates whether the justified text will fit on the line. I've checked the width of the line and it outputs 165.1 (which is 6.5 inches x 25.4mm) Completely stumped.
Ok, I feel kind of dumb, but hopefully this will help others. I use the FpdfMulticell (See http://www.interpid.eu/fpdf-multicell) for more control over cells and other parts of displaying the content on the PDF document. This is great and all, except for the redundancies that are created. The components of this function require width and height for the cell to be created. This is where the problem was. For some reason I was passing 160 to it rather than 165.1. 6.5" x 25.4 = 165.1. When I go through the references to create the multiCell function within the class, it fixes my problem.
If you're going to use the multicell, I suggest declaring a variable at the beginning of your page such as $pw (i.e., page width) and then instead of passing a constant value, pass the variable. That way your values are uniform and if you want to change it up, then you only have 1 value to change. Make sure you comment so you know what the purpose of that variable is.
$oMulticell = new FpdfMulticell($oPdf);
$pw = 165.1
//Make sure that the content is long enough to go to another line or you won't
//notice the difference with the justified text
$content = "Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here ";
$oMulticell->multiCell($pw,10,$content,0,"J",1,0,0,0,0);
This works!

How to generate a nice PDF with php Excel?

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...

Get the cell background color in PhpExcel

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.

Exporting excel to html using php on windows

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 :)

Categories