PHPExcel prevent calculating formula - excel

I'm trying to convert a CSV file to a XLSX file using PHPExcel library. Once the csv file is read into PHPExcel object and before saving it as a xlsx file, I recalculate and set column widths based on relevant column content.
$objReader = PHPExcel_IOFactory::createReader('CSV');
$objPHPExcel = $objReader->load("test.csv");
$activesheet = $objPHPExcel->getActiveSheet();
$lastColumn = $activesheet->getHighestColumn(); // get last column with data
$lastColumn++;
for ($column = 'A'; $column != $lastColumn; $column++) { // for each column until last
$activesheet->getColumnDimension($column)->setAutoSize(true); // set autowidth
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("downloads/test.xls");
with bit of a research i found that if there are any formulas in the file, call to setAutoSize() calculates the value for them to make use when calculating the column width.
My problem is that some of my csv files contain values that begins with = (equal sign) which are not formulas. for ex. cell values like '===='. This causes above code to throw an error PHPExcel_Calculation_Exception Formula Error: An unexpected error occured.
Since I know that any of my input csv files cannot contain formulas, is there a way to prevent PHPExcel calculating values for cells which contain values beginning with = sign?

After research and given suggestions I ended up iterating through all the cells and rewriting cell values (beginning with = sign), to prevent PHPExcel considering them as formulas. setCellValueExplicit() method instructs PHPExcel to not consider the cell value as a formula in this case.
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) {
if (preg_match( '/^=/', $cell->getValue())) {
$cellcoordinate = $cell->getCoordinate();
$worksheet->setCellValueExplicit($cellcoordinate,$worksheet->getCell($cellcoordinate));
}
}
}
}
It's painful, but couldn't find a better solution.

Related

How to calculate formula when I create excel file in PhpSpreadsheet

I have a problem with my code. I generate an excel file unsing PhpSpreadsheet lib. So i can write my data and it worked fine. I add a data validation list in a cell using an other worksheet and it work too. So I try to add in a cell a formula which can change the value (using VLOOKUP) when someone select something in my data validation list and this work too.
But my problem is my formula is not calculated when I open my file. When I open the file I just see the formula and I wanted to see the result.
This is my code:
$i=2;
$j=6;
$filename = "export-excel.xlsx";
$spreadsheet = new Spreadsheet();
$dataSheet = $spreadsheet->getSheet(0);
$dataSheet1 = $spreadsheet->createSheet(1);
$dataSheet1->setTitle("Materiel");
$dataSheet1->setCellValue('A1','Modèle');
$dataSheet1->setCellValue('B1','Marque');
$dataSheet1->setCellValue('C1','Calibre');
$dataSheet1->setCellValue('D1','ITH');
$dataSheet1->setCellValue('E1','IMG');
$dataSheet1->setCellValue('F1','Type Matériel');
//remplissage matériel
$listMateriel = $this->entityManager->getRepository(Materiel::class)->findAll();
$nbrMateriel = count($listMateriel) +1;
foreach($listMateriel as $materiel){
$dataSheet1->setCellValue('A'.$i,$materiel->getModele());
$dataSheet1->setCellValue('B'.$i,$materiel->getMarque());
$dataSheet1->setCellValue('C'.$i,$materiel->getCalibre());
$dataSheet1->setCellValue('D'.$i,$materiel->getIth());
$dataSheet1->setCellValue('E'.$i,$materiel->getImg());
$dataSheet1->setCellValue('F'.$i,$materiel->getTypeMateriel()->getLibelle());
$i++;
}
//remplissage enveloppe
$enveloppe = $appareil->getEnveloppe();
$dataSheet->setCellValue('A5',1);
$dataSheet->setCellValue('B5',1);
$dataSheet->setCellValue('C5',$enveloppe->getLieu()->getLibelle());
$dataSheet->setCellValue('D5',$enveloppe->getNom());
$dataSheet->setCellValue('E5',$enveloppe->getNumeroschema());
$dataSheet->setCellValue('F5',$enveloppe->getCodegmao());
$dataSheet->setCellValue('G5',$enveloppe->getCodemire());
$dataSheet->setCellValue('H5',$enveloppe->getIk());
$dataSheet->setCellValue('I5',$enveloppe->getResponsable());
$dataSheet->setCellValue('J5',$enveloppe->getUnitegeo());
$dataSheet->setCellValue('K5',$enveloppe->getCentreMainteneur());
//remplissage appareil
foreach($enveloppe->getAppareils() as $app){
$dataSheet->setCellValue('A'.$j,1);
$dataSheet->setCellValue('B'.$j,2);
$dataSheet->setCellValue('C'.$j,$app->getRepere());
$dataSheet->setCellValue('D'.$j,$app->getNom());
$dataSheet->setCellValue('E'.$j,$app->getOrdreDansArmoire());
$dataSheet->setCellValue('F'.$j,$app->getMonotri());
$dataSheet->setCellValue('G'.$j,$app->getDepart());
$dataSheet->setCellValue('H'.$j,$app->getDdr());
$dataSheet->setCellValue('I'.$j,$app->getCalibreddr());
$dataSheet->setCellValue('J'.$j,$app->getMotorise());
$dataSheet->setCellValue('K'.$j,$app->getAlarme());
$dataSheet->setCellValue('L'.$j,(null === $app->getNaturealimentation()) ? '' : $app->getNaturealimentation()->getLibLong());
$dataSheet->setCellValue('M'.$j,$app->getSimultaneite());
$dataSheet->setCellValue('N'.$j,$app->getCompteur());
$dataSheet->setCellValue('O'.$j,(null === $app->getTypeconsommateur()) ? '' : $app->getTypeconsommateur()->getLibelle());
$dataSheet->setCellValue('P'.$j,$app->getTypecable());
$dataSheet->setCellValue('Q'.$j,$app->getNumcable());
$dataSheet->setCellValue('R'.$j,$app->getLongueurcable());
$dataSheet->setCellValue('S'.$j,$app->getSectioncable());
$dataSheet->setCellValue('T'.$j,$app->getMateriel()->getModele());
$this->dataValidation('T'.$j, $spreadsheet, $nbrMateriel);
$dataSheet->setCellValue('U'.$j,$app->getReglageIth());
$dataSheet->setCellValue('V'.$j,$app->getReglageImag());
$dataSheet->setCellValue('W'.$j,$app->getObservation());
//This is my formula who work fine
$dataSheet->setCellValueExplicit('X'.$j,'=SI(T'.$j.'<>"";RECHERCHEV(T'.$j.';Materiel!$A$2:$F$'.$nbrMateriel.';4;FAUX);"SO")', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$spreadsheet->getActiveSheet()->getCell('X'.$j)->getCalculatedValue();
Calculation::getInstance($spreadsheet)->disableCalculationCache();
$dataSheet->setCellValue('Y'.$j,$app->getMateriel()->getImg());
$j++;
}
$tmpPath = $this->getTmpFile2();
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setPreCalculateFormulas(true);
$writer->save($tmpPath);
This is what I have when I open my file:
I want to keep my formula in the cell and just show the result when I open my file. So someone have an Idea to how I can calculate my formula before to open my file ? I try with the documentation but it doesn't work.
Than in advance
The problem is that you use \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING in
$dataSheet->setCellValueExplicit('X'.$j,'=SI(T'.$j.'<>"";RECHERCHEV(T'.$j.';
Materiel!$A$2:$F$'.$nbrMateriel.';4;FAUX);"SO")', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
you should use TYPE_FORMULA.
A other common problem for non calculate are string operants instead of number typ operants in the formula. If you have string typs as operant the calculation excel refuse executed by opening the formula.

Node - exceljs: writing to file breaks fomulas in the file

I have an excel (xlsx) file that contains random columns. Some of these columns have formulas mapped to the sum of some cells; for example:
=J8+F9-H9
In my case I have the following three columns:
F: number
H: number
J: =sum of previous row's F and H cell's values.
I aim to get external data and store them cell by cell in this workbook. For this I am using Node module exceljs.
This is my code so far, I am harcoding values for now (which I will be getting from another file later on).
var workbook = new Excel.Workbook();
var filename = 'Bank Synoptic Journal.xlsx'
workbook
.xlsx
.readFile(filename)
.then(function() {
var worksheet = workbook.getWorksheet('Bank Synoptic');
var row = null;
row = worksheet.getRow(8);
row.getCell('J').value = Math.random();
row.commit();
for(var i=9; i<=305;i++) { //row
row = worksheet.getRow(i);
row.getCell('F').value = Math.random();
row.getCell('H').value = Math.random();
row.commit();
}
})
.then(function() {
return workbook.xlsx.writeFile(filename + '_modified.xlsx');
})
.then(function() {
console.log('Done!');
});
It prints the output into a new excel file. The problem I am facing is that for cells 'J' ie which contains the formulas; these cells are breaking with no consitency:
Some cells keep formulas and do the calculations
Others have no more formulas nor calculations done (have '0' instead of formula)
Recalculations are not done automatically using this injection mechanism
(Snapshots)
What I am missing or doing wrong that is leading to this error?
After several trials and errors I moved to Apache POI and so built the script using Java.
I downloaded and included the following JARs in my project:
It manipulates rows/columns and keeps the formulas intact. Once you open the modified excel file all you have to do is refresh (On Windows: ctrl + alt + f9) and it will recalculate.

Compare values from one excel workbook to another

I have two excel workbooks. Workbook1 has list of urls and other i.e. Workbook2 has along with list of urls few more columns.
Workbook1:
COLUMN A
url_list
url1
url2
url3
url
Workbook2:
COLUMN A COLUMN B COLUMN C
Key Words URL Jan 2015
Website search Engine Optimisation url1 72614
Website search Engine Optimisation url2 20890
Website search Engine Optimisation url3 133968
Engine Optimisation url7 584625
I want to compare list of urls from workbook1(Column A) with workbook2(Column B).
If any url from workbook1 is missing in workbook2 then it has to be added in workbook2 in the end.
For example:
Now url is not present in workbook2, so it will be add , and will look like this
Workbook2:
COLUMN A COLUMN B COLUMN C
Key Words URL Jan 2015
Website search Engine Optimisation url1 72614
Website search Engine Optimisation url2 20890
Website search Engine Optimisation url3 133968
Engine Optimisation url7 584625
url
I am using library phpexcel to work with excel sheets in php in windows 7.
Also is there any direct excel formula to do so?
I know with php i can do this.
Thanks
I have a similar task and i have been working tirelessly compiling some code. Though no comparison in-built functions exist, i get data from two different workbooks here (.xlsx files), retrieve specific columns from two worksheets, strip off unnecessary stuff from the data, and store the values in two different associative arrays. I then can use in-built php functions to compare the arrays. You can then pick out the values you intend to write to a new worksheet. I still have to do more work pertaining to my task but i hope this helps someone some day.
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
//set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
//include_once 'Lib/PHPExcel.php';
$fileType = 'Excel2007';
$fileName = 'testBook.xlsx';
// Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();
$objPHPExcelXX = new PHPExcel();
$objPHPExcelW = new PHPExcel();
// Read the file
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReaderXX = PHPExcel_IOFactory::createReader($fileType);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelW, 'Excel2007');
$objReader->setReadDataOnly(true);
$objReaderXX->setReadDataOnly(true);
try {
$objPHPExcel = $objReader->load("Gemeinde_Bad_Rothenfelde.xlsx");
$objPHPExcelXX = $objReaderXX->load($fileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheetXX = $objPHPExcelXX->getActiveSheet();
print($objWorksheet->getTitle());
print($objWorksheetXX->getTitle());
//$objColumn = $objWorksheet->getHighestColumn();
//$objOtherCols = $objWorksheet->getHighestColumn();
$highestRow = $objWorksheetXX->getHighestRow();
$gemendeHighest = $objWorksheet->getHighestRow();
}catch(Exception $e) {
die($e->getMessage());
}
print("\n");
$arrayOrtStr = array();
$arrayGemStr = array();
$count = 1;
$i = 0;
//$colOrtXX is column in primus sheet, $colOrts is column in Gemeinde sheet,the numbers are the real column numbers in the sheets
for ($row = 1, $colOrtXX=1, $colOrtsT=7, $colOrtsTeil=2,$colStrXX=3, $colOrt=6,$colStr = 10; $row <= $highestRow; $row++) {
//$cell = $objWorksheet->getCell($objColumn.$row);
//Getting cell values for Primus Sheet (Columns PostOrt,PostOrtsteil,PostStrasse)
$cellOrtXX = $objWorksheetXX->getCellByColumnAndRow($colOrtXX,$row);
$cellStrXX = $objWorksheetXX->getCellByColumnAndRow($colStrXX,$row)->setDataType(PHPExcel_Cell_DataType::TYPE_STRING);
$cellOrtsTeil = $objWorksheetXX->getCellByColumnAndRow($colOrtsTeil,$row);
$valOrtXX = $cellOrtXX->getValue();
$valStrXX = $cellStrXX->getValue();
$valOrtsTeil = $cellOrtsTeil->getValue();
// Get cell values for Gemeinde sheet (Columns Ort and Strasse)
$cellOrt = $objWorksheet->getCellByColumnAndRow($colOrt,$row);
$cellStr = $objWorksheet->getCellByColumnAndRow($colStr,$row)->setDataType(PHPExcel_Cell_DataType::TYPE_STRING);
//$cellOrtsT = $objWorksheet->getCellByColumnAndRow($colOrtsT,$row);
$valOrt = $cellOrt->getValue();
$valStr = $cellStr->getValue();
// array populated for strasse column in gemeinde sheet but numbers stripped off the address
$onlyStr = preg_replace('/[0-9]+/','',$valStr);
$arrayGemStr[$i] = array("Strasse"=>$onlyStr);
// Go through the Strasse column, only pick cells with Ort Bad Rothenfelde..compare and write
if($valOrtXX == "Bad Rothenfelde"){
// Creating associative array with Ortsteil and Strasse from Primus sheet
$arrayOrtStr[$i] = array("OrtsTeil"=>$valOrtsTeil,"Strasse"=>$valStrXX);
}
$i++;
//print_r($array);
}
$ortTeil = array();
$contentFound = array();
$withStr = array();
foreach($arrayOrtStr as $arr) {
$contentFound[] = $arr['Strasse'];
}
foreach($arrayOrtStr as $arr) {
if(in_array($arr['Strasse'], $contentFound)){
$ortTeil[] = $arr["OrtsTeil"];
$withStr[] = $arr["Strasse"];
}
}
echo '<br/>========================================================<br/>';
print_r($ortTeil);
print_r($withStr);
// Write the Excel file to filename some_excel_file.xlsx in the current directory
//$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcelW);
//$objWriter->save('Gemeinde_Bad_.xlsx');
Copy ColumnA (excluding header/s) from Workbook1 and append to ColumnB of Workbook2 then apply Excel's Remove Duplicates to ColumnB of Workbook2. Removing duplicates should delete all entries from your example but you might blank out B2 (or maybe B1) from Workbook2 first to avoid that.
I post here a very simple method.
This is not a "direct formula", but it may work for you.
I will assume your sources are Sheet1 and Sheet2 in the same workbook, it is easy to adapt to your needs.
Steps to follow:
Add a helper column in Sheet1:
Enter formula =IF(ISNA(MATCH($A2,Sheet2!$B$2:$B$5,0)),ROW(),100000) in B2.
Copy downwards. This will extract the row numbers of URLs to be copied, using a number larger than those for the rest (100000 here). Replace Sheet2!$B$2:$B$5 by the actual range.
Set a list of indexes N of URLs to copy: Locate in Sheet2 the cell at the row just below the last (6 in your example) and the column just to the right of the last (D in your case). Enter the sequence 1,2,... from that cell down.
Pick the Nth URL to copy: Enter the formula =OFFSET(Sheet1!$A$2,SMALL(Sheet1!$B:$B,D6)-2,0) in B6. Copy down.
Variations on this can be produced.
We are migrating from PHPExcel to PhpSpreadsheet. Here is the snippet I used in my phpunit test to compare 2 excel files using PhpSpreadsheet:
// compare files
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
// no need to read styles, we just care about data
$reader->setReadDataOnly(true);
// load expected file (stored somewhere in the tests directory)
$spreadsheetExpected = $reader->load($expectedFilePath);
// load the generated file
$spreadsheetActual = $reader->load($actualFilePath);
// loop through 3 pages, indices 0, 1, and 2
foreach (range(0, 2) as $sheet) {
// loop through 2 rows
foreach (range(1, 20) as $row) {
// loop through first 6 columns
foreach (['A', 'B', 'C', 'D', 'E', 'F'] as $column) {
// find coordination
$cell = $column . $row;
// get expected cell value
$expected = $spreadsheetExpected->getSheet($sheet)->getCell($cell)->getValue();
// get actual cell value
$actual = $spreadsheetActual->getSheet($sheet)->getCell($cell)->getValue();
// compare values, show the sheet and coordination in case of failure
$this->assertEquals($expected, $actual, "Mismatch in sheet {$sheet}, cell {$cell}");
}
}
}
Apparently this test fails on the first mismatch.

Getting Cell as String in PHPExcel by column and row

I am trying to read a cell with possible trailing zeros as a string instead of numeric (which strips off leading zeros). The cell is read by integer column/row as below instead of column string as this answer has.
initial code
$instReader = $reader->load($this->file);
$sheet = $instReader->getSheet(0);
I tried modifying this from:
$keyCell = $sheet->getCellByColumnAndRow(1,5);
to:
$sheet->setCellValueExplicitByColumnAndRow(1,5, PHPExcel_Cell_DataType::TYPE_STRING);
$keyCell = $sheet->getCellByColumnAndRow(1,5);
the former gives 1407 for $keyCell instead of 01407
the latter gives "s" or ""
how do I treat the cell as string before calling getCellByColumnAndRow and using only integer values for column and row.
(BTW, if this can be done once for an entire column instead of each time for each individual cell that would be better)
$keyCell = $sheet->getCellByColumnAndRow(1,5)->getValue();
Will read the cell data in the format that it's actually stored by Excel, you can't arbitrarily change that or tell PHPExcel to read it as a different datatype.
However, if the cell has formatting applied, then you can use
$keyCell = $sheet->getCellByColumnAndRow(1,5)->getFormattedValue();
instead, and this will return the data as a string, with whatever format mask was defined in the Excel spreadsheet
Same issue for me. I become crazy.
Tried to set
$objReader->setReadDataOnly(true);
wasn't working
tried
$sheet->getCellByColumnAndRow(4,$row)->getValue()
because normaly display text as raw => doesn't working.
So last I change code in library. Edit file named DefaultValueBinder.php
Search for dataTypeForValue function and set this :
} elseif (is_float($pValue) || is_int($pValue)) {
return PHPExcel_Cell_DataType::TYPE_STRING;//TYPE_NUMERIC patch here;
} elseif (preg_match('/^\-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)$/', $pValue)) {
return PHPExcel_Cell_DataType::TYPE_STRING;//TYPE_NUMERIC patch here;
So now return numbers with 0

CSV generation possible with Apache POI?

I need to generate csv files and I stumbled on a module in our project itself which uses Apache POI to generate excel sheets aleady. So I thought I could use the same to generate csv. So I asked google brother, but he couldnt find anything for sure that says Apache POI can be used for CSV file generation. I was checking on the following api too and it only talks about xls sheets and not csv anywhere. Any ideas?
http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Workbook.html
Apache Poi will not output to CSV for you. However, you have a couple good options, depending on what kind of data you are writing into the csv.
If you know that none of your cells will contain csv markers such as commas, quotes, line endings, then you can loop through your data rows and copy the text into a StringBuffer and send that to regular java IO.
Here is an example of writing an sql query to csv along those lines: Poi Mailing List: writing CSV
Otherwise, rather than figure out how to escape the special characters yourself, you should check out the opencsv project
If you check official web site Apache POI, you can find lots of example there. There is also an example that shows how you can have csv formatted output by using apache POI.
ToCSV example
Basic strategy:
1) Apache Commons CSV is the standard library for writing CSV values.
2) But we need to loop through the Workbook ourselves, and then call Commons CSV's Printer on each cell value, with a newline at the end of each row. Unfortunately this is custom code, it's not automatically available in XSSF. But it's easy:
// In this example we construct CSVPrinter on a File, can also do an OutputStream
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
CSVPrinter csvPrinter = new CSVPrinter(reader, CSVFormat.DEFAULT);
if (workbook != null) {
XSSFSheet sheet = workbook.getSheetAt(0); // Sheet #0
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
csvPrinter.print(cell.getStringCellValue()); // Call Commons CSV here to print
}
// Newline after each row
csvPrinter.println();
}
}
// at the end, close and flush CSVPrinter
csvPrinter.flush();
csvPrinter.close();
An improved and tested version of gene b's response is this:
/**
* Saves all rows from a single Excel sheet in a workbook to a CSV file.
*
* #param excelWorkbook path to the Excel workbook.
* #param sheetNumber sheet number to export.
* #param csvFile CSV file path for output.
* #throws IOException if failed to read the Excel file or create/write to a CSV file.
*/
public static void excelToCsv(String excelWorkbook, int sheetNumber, String csvFile) throws IOException {
try (Workbook workbook = WorkbookFactory.create(new File(excelWorkbook), null, true); // Read-only: true
BufferedWriter writer = new BufferedWriter(new FileWriter(csvFile));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
Sheet sheet = workbook.getSheetAt(sheetNumber);
DataFormatter format = new DataFormatter();
for (Row row : sheet) {
for (int c = 0; c < row.getLastCellNum(); c++) {
// Null cells returned as blank
Cell cell = row.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = format.formatCellValue(cell);
csvPrinter.print(cellValue);
}
csvPrinter.println();
}
csvPrinter.flush();
}
}
The following improvements were made:
NullPointerException won't be thrown if a cell in an Excel Row was never edited. A blank value will be written to the CSV instead.
Excel values are rendered using DataFormatter allowing the CSV to match the visual representation of the Excel sheet.
try-with-source used for auto-close of the file objects.
The workbook is opened in the read-only mode.

Resources