Am trying to read excel file with PHPExcel library
https://github.com/PHPOffice/PHPExcel
however when i read row then am not able to get comments on field and strikes on text also missing .
my code is
include 'PHPExcel/IOFactory.php';
$ftype = 'Excel2007';
$fname = 'data.xlsx';
$objexcel = PHPExcel_IOFactory::load($fname);
$sdata = $objexcel->getActiveSheet()->toArray(null,true,true,true);
foreach ($sdata as $k=>$d) {
print_r($d); // output
}
so it output comments and strikes which are in data.xlsx file are missing .
any idea how to display and check if row have comments and strikes through text
Related
i have a php script that read data from excel file starting from cell B6. However. I would like the php to be able to read data from B6 until the last row of data which in this case is cell D7.
I try searching online for answer and all i get is this $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
Can anyone help?
Below is my php script code:
require_once 'Classes/PHPExcel.php';
$path = 'import/Attachment F - Visitor or Supplier Request Template.xlsx';
$reader = PHPExcel_IOFactory::createReaderForFile($path);
$excel_obj = $reader->load($path);
$worksheet = $excel_obj->getSheet('0');
for($row=6;$row<=14;$row++){
$category = $worksheet->getCell('B'.$row)->getValue();
if($category!=''){
$uen = $worksheet->getCell('C'.$row)->getValue();
$company = $worksheet->getCell('D'.$row)->getValue();
echo $category." ".$uen." ".$company."<br/>";
}
}
Using PHPSpreadsheet, when populating an existing spreadsheet, all styles, conditional formatting, dropdowns, etc. are gone.
How can I retain the original settings while writing to php://output?
Here's a screenshot of what the base ODS file should look like:
When opening this template and writing values from the database to the R/A/S/C/I cells, the outcome is this:
Here is the code I have so far. You can ignore $modulePath etc. as that is just the lookup of the full system path to the file.
$original = $modulePath . '/resources/ISO27k-RASCI-tool.ods';
$reader = IOFactory::createReader('Ods');
$sheet = $reader->load($original);
$sheet->setActiveSheetIndex(1);
$sheet->setHasMacros(true);
$col = range('A', 'Z');
$teams = $this->dataRecord->Annex()->Teams();
foreach ($teams as $i => $team) {
$sheet->getActiveSheet()->setCellValue($col[$i + 2] . '1', $team->Name);
}
$writer = new Ods($sheet);
$writer->save('php://output');
Be aware of that not all excel/ods styling commands are implemented in phpspreedsheet.
As well there is no "pivottable" and "format as table" support.
Your table looks like you use at least "format as table".
The problem is that you crate a new file and your file is only a kind of template. Unsupported cell styling will be skipped. The only solution is that you must style your output with the styling commands.
The cv column holds filenames of files uploaded to server.
How to wrap the cell values with a link to pull the files from server.
This is my code.
$sql = "SELECT name,email,phone,cv from applicants";
$res = $this->db->query($sql);
$exceldata="";
foreach ($res->result_array() as $row){
$exceldata[] = $row;
}
$this->excel->getActiveSheet()->fromArray($exceldata, null, 'A3');
$this->excel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('B3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('C3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('D3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
I want the cell to show the value with link pointing to the file.
Desired output of the cells:
ex. href="http://examle.com/uploads/cv/ '. cell_value .'">cellvalue
Set the cell as a hyperlink, as described in the PHPExcel Documentation and shown in the examples like 05featuredemo
$cellValue = 123;
$objPHPExcel->getActiveSheet()
->getCell('E26')
->setValue($cellValue);
$objPHPExcel->getActiveSheet()
->getCell('E26')
->getHyperlink()
->setUrl('http://examle.com/uploads/cv/' . $cellValue)
->setTooltip('Click here to access file');
Have a look at the feature demo in the PHPExcel package, there is an example of how to add a link to a cell.
Here's some mockup code for this:
$yourcell = "A3";
$objPHPExcel->getActiveSheet()->setCellValue($yourcell, 'www.example.net');
$objPHPExcel->getActiveSheet()->getCell($yourcell)->getHyperlink()->setUrl('http://www.example.net');
$objPHPExcel->getActiveSheet()->getCell($yourcell)->getHyperlink()->setTooltip('Navigate to website');
I have a problem regarding PHPExcel when reading .csv files.
I wanted to get the values from the .csv file, but the problem is the data from a specific row considered as a single cell.
heres my code:
include 'Classes/PHPExcel/IOFactory.php';
$inputFileType = 'CSV';
$inputFileName = $_FILES['file']['tmp_name'];
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$table = "<table border=1><tr><td>first</td><td>middle</td><td>last</td><td>email</td>";
for ($x = 2; $x <= count($sheetData); $x++){
foreach ($sheetData[$x] as $data){
$first = $sheetData[$x]['A'];
$middle = $sheetData[$x]['B'];
$last = $sheetData[$x]['C'];
$email = $sheetData[$x]['D'];
}
$table .= "<tr><td>" . $first ."</td><td>" . $middle . "</td><td>" . $last . "</td><td>" . $email . "</td></tr>";
}
$table .= "</table>";
echo $table;
It is working on .xls and .xlsx files and I get the desired output that I wanted.
This works fine:
$objReader->setDelimiter("\t");
However, when you are not 100% sure if its tab or comma separated there seems to be no way to add BOTH e.g. $objReader->setDelimiter("\t",); which is something that would be required. When you open Excel and go to Import CSV the actual on screen steps allow you to specify multiple delimiters which is something that would be cool.
Is this something you are working on with PHP Office?
On a separate note here are two links that help you using PHP to find out if the file is comma, tab, or pipe separated - quiet a clever solution:
how to find out if csv file fields are tab delimited or comma delimited
How should I detect which delimiter is used in a text file?
This worked for me:
$objReader = new PHPExcel_Reader_CSV();
$objReader->setDelimiter(';');
$objReader->setEnclosure('');
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(0);
$objPHPExcel = $objReader->load($myFile);
More info https://docs.typo3.org/typo3cms/extensions/phpexcel_library/1.7.4/manual.html#_Toc237519888
So what is the separator in your file? Is it a comma, a semi-colon, a tab, something else?
PHPExcel doesn't yet have an automagic detect mode, so unless you specify what separators and enclosures to use, it will default to a comma separator, and a double quote (") enclosure. If your file is using tabs, or semi colons, or some other character as a separator instead, then you need to manually tell the CSV reader what character to use, otherwise it will treat the row as a single cell.
There's a whole section of the User documentation for the Readers devoted to explaining these options for CSV files (section 4.6).
Note that I'm targeting logic to "best guess" separator and enclosure values from the file itself at the #phpnw13 hackathon, but until then you need to specify manually if it isn't the defaults
I am using an existing Excel file, with the first row containing plain text, to be used later as template. I then changed a few of the column formats to currency, accounting, date...e.t.c. Now I am loading in this file in phpExcel and populating data from my database to this file and saving it with a new name. But on the saved file, all cells in the newly created rows seem to be marked general, though the original plain text row still seems to have the correct format assigned.
My Code:
$xl_tmplt = ((isset($_REQUEST['excel_template']) && $_REQUEST['excel_template'] != "")
? $_REQUEST['excel_template']
: "report_tab1.xlsx");
require_once 'includes/classes/PHPExcel/PHPExcel.php';
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize' => '8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$tmp_workbook = PHPExcel_IOFactory::load($tmp_folder_path . '/templates/' . $xl_tmplt);
$tmp_workbook->setActiveSheetIndex(0);
$sheet = $tmp_workbook->getActiveSheet();
$sheet->fromArray($arr, '', 'A2');
$objWriter = PHPExcel_IOFactory::createWriter($tmp_workbook, 'Excel2007');
$objWriter->save($tmp_folder_path . "output/$filename");
$result['type'] = "success";
$result['msg'] = "The file was successfully created.";
Any idea how I can load in an excel file with predefined column formats and use it to generate a new excel? Or change/set the format of a column or cell range during generation?
PS: I am not too good with excel so I might have set the column format in the wrong way. basically I clicked on the column letter which selects the column, and then changed the format in the dropdown above. If this is wrong, please let me know.
Please check below link for example of setting Row and Cell style
http://phpexcel.codeplex.com/workitem/7333
UPDATE:
For formatting Numbers, Text, Date and Currency, please check below link
PHPExcel Formatting