PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 5, 2022

[FIXED] How to created w readable Excel file with PHP Excel

 October 05, 2022     csv, php, phpexcel     No comments   

Issue

I have been using PHPExcel to created a spreadsheet from a csv file. I have been able to get that working perfectly with the exception that the Excel files are marked Read-Only. I have browsed the GitHub issues for the Class and I have run a search on this board and can not find anyone speaking to this issue.

Can PHPExcel write an Excel file that is not marked Read-Only? Here is my code. As you can see I have tried marking all of the sheet protection and lock options to false.

<?php /** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
date_default_timezone_set('Europe/London');
echo "<h1>FINDLAY FOODS Product List CSV Import to Excel</h1><br /><br />";
/** Include PHPExcel */
require_once dirname(__file__) . '/../Classes/PHPExcel.php';

// Create new PHPExcel object
echo date('H:i:s'), " Create new PHPExcel object", EOL;
$objPHPExcel = new PHPExcel();
// Set document properties
echo date('H:i:s'), " Set properties", EOL;
$objPHPExcel->getProperties()->setCreator("Findlay Foods")->setLastModifiedBy("Findlay Foods")->setTitle("Prod_List_Test_1")->
    setSubject("PRod List Test")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->
    setKeywords("office 2007 openxml php")->setCategory("Test result file");
echo date('H:i:s'), " Add data", EOL;
$objPHPExcel->setActiveSheetIndex(0);

$new_array = array();
$row = 1;

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Code');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'True');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Rebate');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Break');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Portion');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Unit');
if (($handle = fopen("prodlist.csv", "r")) !== false)
{
    while (($data = fgetcsv($handle, 1000, ",")) !== false)
    {
        $row++;
        for ($c = 0; $c < 1; $c++)
        {
            $fcode = trim(str_pad($data[0], 5, "0", STR_PAD_LEFT));
            $fcode = (float)$fcode;
            $true = trim($data[1]);
            $true = (float)$true;
            $true = number_format($true, 2, '.', '');
            $rebate = trim($data[2]);
            $bf = trim($data[3]);
            $str = trim($data[4]);
            $portion = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
            $size = strtolower(preg_replace("/[0-9]/", "", $str));
            $size = strtolower(preg_replace("/\./", "", $size));
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $row, $fcode);
            $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode('0000#');
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $row, $true);
            $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode('#,##0.00');
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $row, $rebate);
            $objPHPExcel->getActiveSheet()->getStyle('C' . $row)->getNumberFormat()->setFormatCode('#,##0.00');
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $row, $bf);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $row, $portion);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $row, $size);
        }
    }
    fclose($handle);
}
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(false);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(false);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(false);
$objPHPExcel->getSecurity()->setLockWindows(false);
$objPHPExcel->getSecurity()->setLockStructure(false);

echo date('H:i:s'), " Write to Excel5 format", EOL;
$callStartTime = microtime(true);

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save(str_replace(__file__, '/../data/list/findlay-prodlist.xlsx', __file__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s'), " File written to ", str_replace('.php', '.xls', pathinfo(__file__, PATHINFO_BASENAME)),
    EOL;
echo 'Call time to write Workbook was ', sprintf('%.4f', $callTime), " seconds", EOL;
// Echo memory usage
echo date('H:i:s'), ' Current memory usage: ', (memory_get_usage(true) / 1024 / 1024), " MB", EOL;`enter code here`
// Echo memory peak usage
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
// Echo done
echo date('H:i:s'), " Done writing file", EOL;
echo 'File has been created in ', getcwd(), EOL; ?>

Solution

Do "save as" after you open it.

Edit: sorry, maybe I misunderstood the question. To expand on my answer, the protections etc. that you have addressed are 'inside' the file. The Read-Only status is set by the operating system. It is the file permissions that need to be dealt with. Wherever you make the file must be a writable directory for the user reading it. If downloaded through a browser, the same applies.



Answered By - Pete
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing