I have an excel file with around 40 sheets with some of the cells defined by formulas.
I read the contents of a single sheet and display on the UI. After user modifies the same i save it back to the excel using phpExcel save.
But when i save PHPExcel save modifies all the sheets with some junk values where ever there is a formula cell.
for ($row = 2; $row <= $highestRow; $row++) {
//echo "row=".$row;
if((sizeof($convertedArr)-1) > $row){
$jsonRow = $convertedArr[$row];
$type = PHPExcel_Cell_DataType::TYPE_STRING;
$colWrite = 0;
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
if($sheet->getCellByColumnAndRow($col, $row)->getValue() != 'xxxxx'){
//$sheet->setCellValueByColumnAndRow($col, $row,$jsonRow[$col]);
$sheet->getCellByColumnAndRow($col, $row)->setValueExplicit($jsonRow[$colWrite], $type);
$colWrite++;
}
}
}else{
break;
}
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
if(is_writable($siteName)){
$objWriter->setPreCalculateFormulas(FALSE);
$objWriter->save($siteName);
}else{
echo "File is not Writable";
}
How can i handle this situation.
How can PHPExcel just save the sheet in question and not all the sheets.
I am NOT even looping through all the sheets.
Also, one more problem is after save the formatting such as background color is gone!
Related
I tried to read a .csv file using the PHPExcel library but it is giving me question marks instead of the Hebrew text.
The problem only occur when I tried to upload .csv files created from Windows Operating System. I tried the same for MacOS and Linux and the data seems to be fine. How can I solve the encoding issue from windows
public function actionUpload()
{
$params = $_FILES['uploadFile'];
if($params)
{
$data = array();
$model = new UploadForm();
$model->uploadFile = $_FILES['uploadFile'];
$file = UploadedFile::getInstanceByname('uploadFile');
$inputFileName = $model->getpath($file,$data);
// Read your Excel workbook
try
{
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName['link']);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName['link']);
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($inputFileName['link'],PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$fileData = array();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++)
{
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
array_push($fileData,$rowData[0]);
// Insert row data array into your database of choice here
}
return $fileData;
}
}
The output array has "???? ???" in places where Hebrew text was present when .csv files created from Windows was uploaded.
I 'm using ms excel to upload large data to my system. When I upload excel file which has columns made by recipe.The uploading progess is longer than usual.Now,I want to check valid the input excel file.If they have columns made by recipe, the system will cancel progess. I 've read phpexcel doucment, but not found any solution.
Do you have any ideas?
My source
My source:
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($uploaded_dir . DIRECTORY_SEPARATOR . $new_name);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$total_order_row = $highestRow;
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
for ($i = 1; $i <= $highestRow; $i++) {
$data1 = $objWorksheet->getCellByColumnAndRow(0, $i)->getValue();
$data2= $objWorksheet->getCellByColumnAndRow(1, $i)->getValue();
$data3= $objWorksheet->getCellByColumnAndRow(2, $i)->getValue();
//Todo: insert to db
}
If "by recipe" you mean by a calculation, then check the datatype of a cell rather than the value:
if ($objWorksheet->getCellByColumnAndRow(1, $i)->getDataType() == PHPExcel_Cell_DataType::TYPE_FORMULA) {
echo 'I contain a formula; use getCalculatedValue() to get my value';
}
I'm trying to generate a .xlsx file using PHPExcel. The process is generating the file, but when I'm getting to open the file, It says It's corrupted. When I use Open and Repair, the file opens and It's ok, but I don't want to repair everytime I generate the file. I tried to use the function ob_end_clean() before saving, but It didn't work.
Here is the code I'm using:
$objSheet1 = $objPHPExcel->getActiveSheet();
$objSheet1->setTitle("sheet1");
$line_value = 1;
foreach($sheet1_data as $line){
$col = 'A';
foreach($line as $cell_value){
$objSheet1->setCellValue($col.$line_value, $cell_value);
$col++;
}
$line_value++;
}
$objSheet2 = $objPHPExcel->createSheet();
$objSheet2->setTitle("sheet2");
$line_value = 1;
foreach($sheet2_data as $line){
$col = 'A';
foreach($line as $cell_value){
$objSheet2->setCellValue($col.$line_value, $cell_value);
$col++;
}
$line_value++;
}
ob_end_clean();
$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel2007' );
$objWriter->save ( get_file_location() );
My code works perfect with file.xlsx but I can't make it work with old file.xls. I tried every code I found and nothing works... I tried change the version of Excel2007to 2003,2004 but nothing... Don't know what else to try.
Here is my current code (working with file.xlsx but I need to work with file.xls too).
<?php
function load_table(){
require_once('Classes/PHPExcel.php');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load("SampleData.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
echo '<table class="table">' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . "\n";
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
echo '<td>';
$first = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
if($first[0] == '='){
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getCalculatedValue();
}
else
echo $first;
echo '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
}
?>
The Reader for xls files is the Excel5 Reader.
But you really should simply use the IOFactory's load() method, and let PHPExcel identify the filetype for itself and select the correct reader, because a lot of files (particularly those downloaded from the internet) that have an .xls extension aren't really BIFF format files.... PHPExcel can identify and load these correctly when using the load() method, because it can pick the correct Reader for itself.
But when you specify a Reader such as Excel2007 or Excel5 yourself, you're telling PHPExcel which Reader it must use, even though it may not be the correct Reader for the actual file.
Document link, which lists all of the different Readers by name
I am currently exporting data from php to excel using the code as below:
include("dbconnect.php");
$query = $_POST['query'];
$result = odbc_exec($conn,$query);
$count = odbc_num_fields($result);
//Define Variable For ODBC
$data = "";
//Field Name Data
for ($i = 1; $i <= $count; $i++)
{
$data .= odbc_field_name($result, $i)."t";
}
$data .= "n";
//Row Data
while(odbc_fetch_row($result))
{
for ($j = 1; $j <= $count; $j++)
{
$data .= odbc_result($result, $j)."t";
}
$data .= "n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=ExcelFile.xls;");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;
odbc_close($conn);
This all works fine, but the generated excel file has a sheetname of: ".xls]ExcelFile(1)" , and when you try to rename the sheet it causes an error in excel (unless you save the file first).
How can I define the sheetname in my php file?
Thanks in advance!
Have a nice weekend:-)
You're not actually creating an xls file, but a tab separated value file. Excel can read this, but it simply populates the data in the first worksheet. Because it's not a true xls file, you can't name the worksheet tabs in any way.
One option would be to change your code to use a library that writes true xls files, such as PHPExcel ( http://www.phpexcel.net )... you would then be able to define a name for the worksheet tab within your script.
By changing the
header("Content-type: application/octet-stream");
To
header("Content-type: application/vnd-ms-excel");
It is downloading without any errors and i am able to save that. the worksheet name is same as excel name.