<?
require_once '../Classes/PHPExcel.php';
require_once '../Classes/PHPExcel/IOFactory.php';
require_once '../Classes/PHPExcel/Writer/Excel2007.php';
$objPHPExcel = PHPExcel_IOFactory::load("../../ruw/myfile.xlsx");
$objPHPExcel->setActiveSheetIndex(0);
$row = $objPHPExcel->getActiveSheet()->getHighestRow()+1;
$objPHPExcel->getActiveSheet()->SetCellValue("A5",$m5);
$objPHPExcel->getActiveSheet()->SetCellValue("B5",$m1);
$objPHPExcel->getActiveSheet()->getStyle('A1:L300')->
getProtection()->setHidden(PHPExcel_Style_Protection::OTECTION_PROTECTED);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertColumns(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatColumns(true);
$objPHPExcel->getActiveSheet()->getProtection()->setDeleteColumns(true);
$objPHPExcel->getActiveSheet()->getProtection()->setDeleteRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSelectLockedCells(true);
$objPHPExcel->getActiveSheet()->getProtection()->setPassword($passy);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$d =$m1;
$d = preg_replace('/[^a-zA-Z0-9%$##*&^-_()]/','',$m1);
$fil = "../../ruw/".$d.".xlsx";
$objWriter->save($fil);
echo "<label style='font:Verdana, Geneva, sans-serif; font-size:20px; color:#CE0000'>
File download start automatically if not click here to </label> ";
echo "<a style='font:Verdana, Geneva, sans-serif; color:#000; font-weight:bold;' href='$fil'>Download</a> ";
echo "<script type='text/javascript'>" ;
echo "reply_click(".$_SESSION['mid'].",'$fil')";
echo "</script>";
?>
This code is working fine to write on ".xlsx" but not working to write no ".xls"
if i try to write and open .xls i am getting different format,unreadable content and xml parse arror
i want to write to .xls 97-2003 and save it as .xls 97-2003 format
Thanks in advance
You're using the wrong Writer instance you need to change PHPExcel_Writer_Excel2007 to PHPExcel_Writer_Excel5()
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save("myfile.xls");
Note: You will need to update your include path accordingly.
Related
I'm having trouble in getting the correct characters from my .xls file.
Based on what I found on the internet, it seems that it has something to do with the encoding or its something about html that cant be read through PHPexcel. It says that for PHPExcel to read the file it has to be utf-8. Can you give me hints on how to get the correct encoding or getting to read it by utf-8?
Here's my code:
require_once 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';
$pathgalleriadeclare = "C:\Users\Desktop\sample.xls";
try {
$inputfiletype = PHPExcel_IOFactory::identify($pathgalleriadeclare);
$objReader = PHPExcel_IOFactory::createReader($inputfiletype);
$objReader->setInputEncoding('ISO-8859-1');
utf8_encode($objReader);
$objPHPExcel = $objReader->load($pathgalleriadeclare);
} catch(Exception $e){
die('Error loading file "' .pathinfo($pathgalleriadeclare, PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$highRow = $sheet->getHighestRow();
$highColumn = $sheet->getHighestColumn();
for ($row = 7; $row <= $highRow; $row++){
//REPORT
$value = $objPHPExcel->getSheet(0)->getCell('A'.$row)->getValue();
echo $value ;
echo "<br />";
}
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
exit();
Here's a sample output:
I just started using PHPExcel yesterday. It works like a charm except when I use it to read xlsx file downloaded from internet. It is not able to read the file until I open the dowloaded file and save it explicitly without making any change.
$objPHPExcel = PHPExcel_IOFactory::load($inputFile);
Line I use to load the Excel file.
Where am I going wrong? Am I missing some code. Something else needs to be written along with the above line of code to load the file.
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow; ++ $row) {
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
if($col==0){
$val = \PHPExcel_Style_NumberFormat::toFormattedString($val, 'YYYY-MM-DD');
}
$dataArr[$row][$col] = $val;
}
This code is able to read the excel when I open the excel and save it.
So, I'm a fairly new PHP programmer and I'm trying to set up a page that uploads a file, displays the info in a table, then writes the data to a database.
I've got these things working (I found some helpful code online) however my dates are displaying incorrectly.
For example, the date "2/15/2017 17:55" is displaying as "42781.746527778".
Here is the code:
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
. '": ' . $e->getMessage());
}
echo '<center><table style="width:50%;" border=1>';
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for ($row = 1; $row <= $highestRow; $row++) {
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL, TRUE, FALSE);
echo "<tr>";
foreach($rowData[0] as $k=>$v)
echo "<td>".$v."</td>";
echo "</tr>";
}
echo '</table></center>';
}
else{
echo '<p style="color:red;">Please upload file with xlsx extension only.</p>';
}
I get why it is displaying the date that way, however I'm not having luck in fixing it when trying to display the entire column as a date and time, or displaying it in a single cell as the correct date and time.
var_dump($objPHPExcel->getActiveSheet()->getCell('B2')->getFormattedValue());
This code displays the correct date and time for one of my sample spreadsheets, however I'm not able to get that value in the cell.
Any help would be appreciated. Thank you!
I believe this is what you are looking for? Check if the cell is a Excel date and time then extract and convert the Excel date to a PHP date.
if(PHPExcel_Shared_Date::isDateTime($k)) {
$v = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($v));
}
I installed Excelbundle with PhpExcel library. I want to read excel files I found this function.
How can I use it? Any suggestion?
public function xlsAction()
{
$filenames = "your-file-name";
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filenames);
foreach ($phpExcelObject ->getWorksheetIterator() as $worksheet) {
echo 'Worksheet - ' , $worksheet->getTitle();
foreach ($worksheet->getRowIterator() as $row) {
echo ' Row number - ' , $row->getRowIndex();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
echo ' Cell - ' , $cell->getCoordinate() , ' - ' , $cell->getCalculatedValue();
}
}
}
}
}
My suggestion is "Read the documentation" and start hacking at it. Working with excel is, in my experience, quite complex and time consuming so don't expect other people to solve your problem online.
It seems like you're talking about this bundle:
https://github.com/liuggio/ExcelBundle
It has great documentation, even full examples (see "Fake Controller").
With PHPExcel it is quite easy to read a Excel document.
See my example :
$dir = $this->getContainer()->getParameter("kernel.root_dir") . "/../../data/import/";
$file_name = "my_excel_file.xlsx";
$excel = $this->getContainer()->get('phpexcel')->createPHPExcelObject($dir . DIRECTORY_SEPARATOR . $file_name);
$sheet = $excel->getActiveSheet();
$row = 0;
while ($sheet->getCellByColumnAndRow(1, $row)->getValue()) {
$data = $sheet->getCellByColumnAndRow(2, $row)->getValue(); // get value from nth line and 2nf column
//do stuff -- see doc for functions on $sheet
}
I am trying to write mySQL data into Exlcel using PHPExcel, but I am getting file format is invalid in my above PHP code using PHPExcel. Can someone please help me to find out what is wrong in code:
$query = "SELECT company_id, userid FROM uam_user";
$result = mysql_query($query) or die ("Error in Query".mysql_error());
// Instantiate a new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Security Team")
->setLastModifiedBy("System")
->setTitle("System")
->setSubject("System Security")
->setDescription("Non Production user Upload Data")
->setKeywords("User Upload")
->setCategory("Security");
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue("A1", 'company_id');
$objPHPExcel->getActiveSheet()->SetCellValue("B2", 'userid');
//start of printing column names as names of MySQL fields
$rowCount = 2;
//$num_rows = mysql_num_rows($result);
//echo $num_rows;
$i = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
if ($row['userid'] != '')
{
//echo "A$rowCount=".$row['company_id']."|B$rowCount=".$row['userid']."<br>";
$objPHPExcel->getActiveSheet()->SetCellValue("A$rowCount",$row['company_id']);
$objPHPExcel->getActiveSheet()->SetCellValue("B$rowCount",$row['userid']);
}
$rowCount++;
//echo $rowCount;
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');
ob_end_clean();
$objWriter->save ( 'php://output' );