PHP Fill in values ​​with existing .xlsb file with data from Oracle

I want to write an excel .xlsb file whose name is the day number.
For example, the file name for yesterday 23.xlsb.

The original file name is "Template.xlsb" and the location is different. As you can see this file, it is copied and renamed to a new location. I have many macros and vba codes in this file, and therefore I do not want to create a new Excel file.

At the end, the link for this Excel file is in this variable $renamed_link.
$renamed_link = C:\Documents and Settings\Administrator\Desktop\Rezultate DIDU\2017\Apr\23.xlsb.

I want to fill out the first worksheet called "Parametri" with data from my SQL query ( $parametri).
The range from the sheet to be filled with data is from A2to T and the total number of the rows. The table contains 20 columns.

<?php
require(realpath(dirname(__FILE__)."/PHPExcel-1.8/Classes/PHPExcel.php"));
$sql_data = date('d.m.Y', strtotime('-1days'));

    $conn = oci_connect('USER', 'PASS', 'dark:1521/DAR');
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    Else {echo 'Connection successfully !';
    }
$parametri = oci_parse($conn,"SELECT * FROM R0508UNITP WHERE TO_DATE('${sql_data}','DD.MM.YYYY') BETWEEN R0508UNITP.R0508VFROM AND R0508UNITP.R0508VTILL");

oci_execute($parametri);

echo "<table border='1'>\n";
while ($row = oci_fetch_array($parametri, OCI_ASSOC+OCI_RETURN_NULLS)) {


echo "<tr>\n";
foreach ($row as $item) {
    echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
}
    echo "</tr>\n";
}
echo "</table>\n";

$day = date('j', strtotime('-1 days'));
$month = date('M', strtotime('-1 days'));
$year = date('Y', strtotime('-1 days'));


$link = "C:\Documents and Settings\Administrator\Desktop\Rezultate DIDU\\${year}\\${month}";
$template_link = 'C:\Documents and Settings\Administrator\Desktop\Rezultate DIDU\Template.xlsb';
$destination_link = "C:\Documents and Settings\Administrator\Desktop\Rezultate DIDU\\${year}\\${month}".'\Template.xlsb';
$renamed_link = "C:\Documents and Settings\Administrator\Desktop\Rezultate DIDU\\${year}\\${month}\\${day}".'.xlsb';


if(!is_dir($link)) 
{

mkdir($link, 0777,true);

}

if(!file_exists($destination_link))
{
    copy($template_link,$destination_link);

} else {
}

rename($destination_link,$renamed_link);
?>

Screen print of my php table (some of them).
In this table, I want to be in my excel file. enter image description here

+6
source share
1 answer

Parsing and (especially) editing and xlsb (the miscrosoft excel binary) are not trivial at all.

At the time of writing, you can find the ~ 1000 page format specification at this link: http://interoperability.blob.core.windows.net/files/MS-XLSB/[MS-XLSB.BIZ.pdf

xlsb, PHP, :

EasyXLS - :

https://www.easyxls.com/

PHPExcel

https://github.com/PHPOffice/PHPExcel

LGPL. ( ) BIFF 8 (.xls) Excel 95 and above . , xlsb .


db ( ), xlsb . .

: https://www.easyxls.com/manual/tutorials/php/read-xlsb-file.html


UPDATE

EasyXLS xlsb, BIFF12.

-, , " ", , .

EasyXLS 295 , .

EasyXLS PHP: .NET COM + Library PHP ( ). * nix, Windows, .


PHPExcel

PhpSpreadsheet

. , , / .

PhpSpreadsheet BIFF5 BIFF8 , .xls.

BIFF12 - .xlsb.


PHPSpreadsheet

.

Composer , .

"" Template.xlsb, .xls ( Excel). -, .

...

<?php
require 'vendor/autoload.php';

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$spreadsheet = $reader->load( 'Template.xls' );

$worksheet = $spreadsheet->getSheetByName( 'PARAMETRI' );

$worksheet->setCellValue( 'A2', 123 );

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls( $spreadsheet );
$writer->save( "Template-Modified.xls" );

... Template-Modified.xls .

.xls -.

, , .


, PHPSpreadsheet.

, .xls / .

PHPSpreadsheet , .

GitHub

+6

Source: https://habr.com/ru/post/1016725/


All Articles