PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Johnny   Convert CSV to Excel   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: An upload form which accepts a csv file and download the result xlsx file
Class: Convert CSV to Excel
Convert CSV files to Excel using PHPExcel library
Author: By
Last change: move modify header in front of download file
Date: 12 years ago
Size: 757 bytes
 

Contents

Class file image Download
<?php
if (!isset($_FILES["file"]))
{
?>
<html>
    <body>
        <h1>Convert CSV to XLSX</h1>
        <form action="test.php" method="post" enctype="multipart/form-data">
            <input type="file" name="file"/>
            <input type="submit"/>
        </form>
    </body>
</html>
<?php
   
exit;
}

//obtain PHPExcel from http://phpexcel.codeplex.com
require_once('PHPExcel.php');
require_once(
'CSVToExcelConverter.php');

if (
$_FILES["file"]["error"] > 0)
{
    echo
"Error: " . $_FILES["file"]["error"];
    exit;
}

try
{
   
header('Content-type: application/ms-excel');
   
header('Content-Disposition: attachment; filename='.'test.xlsx');

   
CSVToExcelConverter::convert($_FILES['file']['tmp_name'], 'php://output');
} catch(
Exception $e) {
    echo
$e->getMessage();
}