Question

I've a export function which contain excel, csv, pdf option. For excel option I've achieved using this code

var tableToExcel = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,',
            template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
            base64 = function(s) {
        return window.btoa(unescape(encodeURIComponent(s)))
    }, format = function(s, c) {
        return s.replace(/{(\w+)}/g, function(m, p) {
            return c[p];
        })
    }
    return function(table, name, filename) {
        if (!table.nodeType)
            table = document.getElementById(table)
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}

        document.getElementById("dlink").href = uri + base64(format(template, ctx));
        document.getElementById("dlink").download = filename;
        document.getElementById("dlink").click();

    }
})()

And the result is the excel file is downloadable. Then, I want to reuse the excel file for convert to csv or pdf using phpexcel lib. But I don't know how to modification the code above so, I the excel will be saved in temp folder than the file will be executed with this code to produce pdf or csv.

<?php
require_once('PHPExcel/Classes/PHPExcel.php');

//Usage:
convertXLStoCSV('input.xls','output.csv');

function convertXLStoCSV($infile,$outfile)
{
    $fileType = PHPExcel_IOFactory::identify($infile);
    $objReader = PHPExcel_IOFactory::createReader($fileType);

    $objReader->setReadDataOnly(true);  
    $objPHPExcel = $objReader->load($infile);   

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
    $objWriter->save($outfile);
}

?>

How I can achieved to save the excel file temporarily and reuse it for converting process?

Was it helpful?

Solution

After some method that I've tried, I decided not to use the methode above (make a temp excel file then convert it into csv) but to use direct csv converting mechanism from sql result that have been generated before but it's saved in session.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top