Exporting excel to html using php on windows - php

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

Related

PHPSpreadsheet generates an error "Wrong number of arguments for INDEX() function: 5 given, between 1 and 4 expected"

I tried to enter the following code in my excel formula bar directly
=INDEX($E$4:$E$132,AGGREGATE(15,6,ROW($1:$30) / ($J$4:$J$132=M4), COUNTIF($M$4:M4, M4))) and works perfectly fine (the left column on the pic below).
But if I'm using my web application to generate an excel report file (PHP, using CodeIgniter and Laravel). It displays an error "'Wrong number of arguments for INDEX() function: 5 given, between 1 and 4 expected'"
Here's my sample code snippet:
$code = "=INDEX(\$E\$4:\$E\$$occurance, AGGREGATE(15,6,ROW(\$1:\$$occurance) / (\$J\$4:\$J\$$occurance=M$top_cell), COUNTIF(\$M\$4:M$top_cell, M$top_cell)))";
$ews2->setCellValue("L$top_cell", $code);
I also have tried to use the setValueExplicit method but causes the excel file to NOT precalculate the code, it reads the code as a string
$ews2->setCellValueExplicit("L$top_cell", $code, DataType::TYPE_STRING);
NOTE TYPE_STRING is provided because if TYPE_FORMULA is also used, the same output mentioned at the top occurs
Here's what it looks like using the setCellValueExplicit
May I know the right solution or quick fix for this? Thank you very much in advance!
I have found out that the PHPSpreadsheet library for PHP is yet to allow the usage of the AGGREGATE() and complicated formulas/functions, so I had found another way around
By using this excel code
=INDEX(E$2:E$38,IF(M4=M3,MATCH(L3,E$2:E$38,0),0)+MATCH(M4,OFFSET(J$2,IF(M4=M3,MATCH(L3,E$2:E$38,0),0),0,COUNT(J$2:J$38)-IF(M4=M3,MATCH(L3,E$2:E$38,0),0),1),0))
I was able to traverse through my entire range and make sure that no duplicate publication names would appear
This is in relation with the my other question -> Excel - Getting the 2nd or nth matched string from your corresponding data

PHPExcel: problem while writing cell data type issue

I have PHP code to export the data to excel. Used PHPExcel library for the same.
PHPExcel library Version 1.7.6
We encountered a problem while writing the following value ==PD==[HW]RECEIVING CRC ERRORS
When I open the Excel manually and set the cell data type as TEXT it is accepting this value.
But while trying to generate the excel using PHPExcel library, getting an error as below exception 'Exception' with message 'L14 -> Formula Error: Unexpected operator '=''
I tried to solve this issue by setting the data type of the cell as STRING, but no luck... Tried below ways to set the cell data type...
#first try
$activeSheet->setCellValueExplicit($symptomColumn.$rowCount, $val, PHPExcel_Cell_DataType::TYPE_STRING);
#second try
$activeSheet->getCell($symptomColumn.$rowCount)->setValueExplicit($val, PHPExcel_Cell_DataType::TYPE_STRING);
#third try
$activeSheet->getCell($symptomColumn.$rowCount)->setDataType(PHPExcel_Cell_DataType::TYPE_STRING);
#fourth try
$activeSheet->getStyle($symptomColumn.$rowCount)
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_GENERAL
);
#fifth try
$activeSheet->getStyle($symptomColumn.$rowCount)
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_TEXT
);
Can anyone please help me to resolve the issue while writing the text "==PD==[HW]RECEIVING CRC ERRORS" to the cell while creating an excel using PHPExcel library?
Thanks in Advance...
If PHPExcel encounters a cell where the first character of content is an =, then it considers that cell to contain an Excel formula, and will try to evaluate it as such. If it isn't actually a formula, then it will throw an exception like this.
There is no simple solution in PHPExcel, other than to suggest that you add a leading space (or other character) before the =. PHPExcel is no longer a supported library, and this bug will not be fixed (especially not in the older version that you are running).
The latest master branch of the successor library PHPSpreadsheet does contain a fix for this, allowing you to set the style of the cell to quoted text. This will identify that this is not a formula to the calculation engine, and is similar to what you are recommended to do in MS Excel itself.
$workSheet->setCellValue('A2', "==PD==[HW]RECEIVING CRC ERRORS");
$workSheet->getCell('A2')->getStyle()->setQuotePrefix(true);

PHPExcel Multiple worksheet formula returning PHPExcel_Calculation_Exception

Goodevening,
I'm using the PHPExcel Library to populate an existing Excel sheet with some data, and show the result of the calculated value on a webpage.
I've got a couple of formulas that look like this:
=IF(OR($B$7=$A$17;$B$8=$A$17;$B$9=$A$17);"";B9)
=IF(B12<=B14;IF(B12=A17;A17;VLOOKUP(CEILING(B12;1);'Motor selection'!$1:$1048576;2;FALSE));A17)
I get the following error when I try to get the calculated value of 'B9' for example:
Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception' with message 'Pump selection!D7 -> Pump selection!B9 -> Formula Error: An unexpected error occured' in includes/PHPExcel/Classes/PHPExcel/Cell.php:291
I'm using the following PHP code, which is pretty standard.
$objType = PHPExcel_IOFactory::identify($file);
$objReader = PHPExcel_IOFactory::createReader($objType);
$objPHPExcel = $objReader->load($file);
// Populate values
$objPHPExcel->getActiveSheet()->setCellValue('B4', 100)
->setCellValue('B5', 75);
// Motor
$motor = $objPHPExcel->getActiveSheet()->getCell('B9')->getFormattedValue();
As I am not an Excel expert (a client sent me this) I have no clue what is going wrong or how to fix this. All I know is that the formulae in the sheet itself are working fine when I open it in Excel.
Would love to know if someone can assist me on this. I can share the complete Excel file if that's necessary.
Thanks in advance,
Kevin
Edit: I've read that the issues might have something to do with the dollar signs in the formula, and I've already tried escaping them with \$ but that doesn't seem to make a difference.
Edit2: The file can be found here. Idea is that B4 and B5 of the first sheet get populated with form data, and the result will be in D7:H7.

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

Formula disappear php excel

I'm trying to use a formula which is not implanted in PHPExcel but in excel, COUNTIFS whit 2 arguments.
I update the formula file in phpexcel and I add
$objWriter->setPreCalculateFormulas(false);
To have no generation errors.
But when I open the file with excel I only have a 0 and no formula. I also have an open and repair and this remove the function I think
This is for example
$sheet->setCellValue($this->intToChar($j).($end+$i),'=COUNTIFS(E$17:F$47;$B58;E$16:F$46;$B58)');
If I just add
$sheet->setCellValue($this->intToChar($j).($end+$i),'COUNTIFS(E$17:F$47;$B58;E$16:F$46;$B58)');
and I set an = in excel it works
Thanks for your help
The documentation explicitly states that commas (,) should be used as an argument separator for function arguments, unless you've explicitly set a locale for the calculation engine; so you need
$sheet->setCellValue(
$this->intToChar($j).($end+$i),
'=COUNTIFS(E$17:F$47,$B58,E$16:F$46,$B58)'
);
And why are you using a homebrew intToChar() function when there's a built-in PHPExcel_Cell::stringFromColumnIndex() function?

Categories