converting csv to xlsx
This commit is contained in:
+12
-48
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
include "zefixAPI.php";
|
||||
include 'simpleXMLElement.php';
|
||||
include "spreadSheetReader/read_write_xlsx.php";
|
||||
|
||||
$maxExecutionTime = 480;
|
||||
$maxExecutionTime = 61;
|
||||
$taskDir = 'tasks';
|
||||
$downloadDir = 'download';
|
||||
$latesEndTime = time() + $maxExecutionTime;
|
||||
@@ -18,12 +18,6 @@ function doRequest($data, $filename, $username, $password)
|
||||
echo "<br>";
|
||||
$response = sendAPICompanySearchRequest($username, $password, $data);
|
||||
$companyArray = json_decode($response, true);
|
||||
// TODO implement
|
||||
$companyData = [
|
||||
['John', 30, 'New York'],
|
||||
['Alice', 25, 'Los Angeles'],
|
||||
['Bob', 35, 'Chicago'],
|
||||
];
|
||||
$companyData = array();
|
||||
foreach ($companyArray as $company) {
|
||||
$companyData[] = [$company['name'],$company['ehraid'],$company['uid'],$company['chid'],$company['legalSeat'],$company['registryOfCommerceId'],$company['legalForm']['name']['de'],$company['status'],$company['sogcDate'],$company['deletionDate']];
|
||||
@@ -32,7 +26,6 @@ function doRequest($data, $filename, $username, $password)
|
||||
// If File doesn't exist yet
|
||||
$file = fopen($filename, 'w');
|
||||
if($file) {
|
||||
// TODO change headers
|
||||
fputcsv($file, ['name', 'ehraid', 'uid', 'chid', 'legalSeat', 'registryOfCommerceId', 'legalForm', 'status', 'sogcDate', 'deletionDate']);
|
||||
}
|
||||
} else {
|
||||
@@ -54,46 +47,18 @@ function sendEmail($emailAddress, $filename)
|
||||
// Stop executing bevore reaching PHP Max Execution Time
|
||||
function convertcsvToXlsx(string $csvFile, string $xlsxFile)
|
||||
{
|
||||
// Read the CSV file
|
||||
$csvData = file_get_contents($csvFile);
|
||||
$csvData = [];
|
||||
|
||||
// Split the CSV data into rows
|
||||
$csvRows = explode("\n", $csvData);
|
||||
|
||||
// Create a SimpleXMLElement for the workbook
|
||||
$xmlWorkbook = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
|
||||
xmlns:o="urn:schemas-microsoft-com:office:office"
|
||||
xmlns:x="urn:schemas-microsoft-com:office:excel"
|
||||
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
|
||||
xmlns:html="http://www.w3.org/TR/REC-html40">
|
||||
<Worksheet ss:Name="Sheet1">
|
||||
<Table>
|
||||
</Table>
|
||||
</Worksheet>
|
||||
</Workbook>');
|
||||
|
||||
// Reference to the Worksheet's Table
|
||||
$xmlTable = $xmlWorkbook->Worksheet->Table;
|
||||
|
||||
// Loop through CSV rows and create XML elements
|
||||
foreach ($csvRows as $csvRow) {
|
||||
// Split the CSV row into cells
|
||||
$csvCells = str_getcsv($csvRow);
|
||||
|
||||
// Create a new row in the XML table
|
||||
$xmlRow = $xmlTable->addChild('Row');
|
||||
|
||||
// Add cells to the row
|
||||
foreach ($csvCells as $csvCell) {
|
||||
$xmlCell = $xmlRow->addChild('Cell');
|
||||
$xmlData = $xmlCell->addChild('Data', htmlspecialchars($csvCell));
|
||||
$xmlData->addAttribute('ss:Type', 'String');
|
||||
if (($handle = fopen($csvFile, "r")) !== false) {
|
||||
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
|
||||
$csvData[] = $data;
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
echo "Failed to open $csvFile for reading.";
|
||||
}
|
||||
|
||||
// Save the XML data to a file
|
||||
$xmlWorkbook->asXML($xlsxFile);
|
||||
write_xlsxFile($xlsxFile, $csvData);
|
||||
}
|
||||
|
||||
while($latesEndTime - time() > 60){
|
||||
@@ -102,8 +67,8 @@ while($latesEndTime - time() > 60){
|
||||
$taskfiles = scandir($taskDir);
|
||||
$taskfiles = array_diff($taskfiles, array('.', '..'));
|
||||
sort($taskfiles);
|
||||
// if there are any taks which are older then 1 Minutes. To be sure the file writing process is finished.
|
||||
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 60 < time()) {
|
||||
// if there are any taks which are older then 0.5 Minutes. To be sure the file writing process is finished.
|
||||
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 30 < time()) {
|
||||
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
|
||||
$task = json_decode($taskString, true);
|
||||
|
||||
@@ -141,5 +106,4 @@ while($latesEndTime - time() > 60){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user