converting csv to xlsx
This commit is contained in:
+41
-11
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
include "zefixAPI.php";
|
||||
include 'PHPExcel/IOFactory.php';
|
||||
include 'simpleXMLElement.php';
|
||||
|
||||
$maxExecutionTime = 480;
|
||||
$taskDir = 'tasks';
|
||||
@@ -52,18 +52,48 @@ function sendEmail($emailAddress, $filename)
|
||||
echo "sending ".$filename." too ".$emailAddress;
|
||||
}
|
||||
// Stop executing bevore reaching PHP Max Execution Time
|
||||
function convertcsvToXlsx(string $csvFile, string $xslxFile)
|
||||
function convertcsvToXlsx(string $csvFile, string $xlsxFile)
|
||||
{
|
||||
$objReader = PHPExcel_IOFactory::createReader('CSV');
|
||||
// Read the CSV file
|
||||
$csvData = file_get_contents($csvFile);
|
||||
|
||||
// If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader
|
||||
$objReader->setDelimiter("\t");
|
||||
// If the files uses an encoding other than UTF-8 or ASCII, then tell the reader
|
||||
$objReader->setInputEncoding('UTF-16LE');
|
||||
// Split the CSV data into rows
|
||||
$csvRows = explode("\n", $csvData);
|
||||
|
||||
$objPHPExcel = $objReader->load($csvFile);
|
||||
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
||||
$objWriter->save($xslxFile);
|
||||
// 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');
|
||||
}
|
||||
}
|
||||
|
||||
// Save the XML data to a file
|
||||
$xmlWorkbook->asXML($xlsxFile);
|
||||
}
|
||||
|
||||
while($latesEndTime - time() > 60){
|
||||
@@ -93,7 +123,7 @@ while($latesEndTime - time() > 60){
|
||||
// if there are no Requests left, send the E-Mail
|
||||
else {
|
||||
convertcsvToXlsx(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]));
|
||||
unlink(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]));
|
||||
// unlink(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]));
|
||||
sendEmail($task['email'], str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]));
|
||||
echo "Email sent";
|
||||
echo "<br>";
|
||||
|
||||
Reference in New Issue
Block a user