-2

I was thinking since i can import the data from excel to my database, is it possible to upload the excel file itself and be able to display it in your website?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
keme
  • 7
  • 2

1 Answers1

0

For client webpage side, use html form <input type="file"> to upload excel file from browser to PHP file.

Example upload-submit.html:

<form action="upload.php">
    <input type="file" name="excel-upload" />
    <input type="submit" />
</form>

In your PHP upload script you will need to include the PHPExcel library (https://github.com/PHPOffice/PHPExcel) to read and parse the uploaded Excel file. You can read Mark Baker's very good example on how to read from Excel and insert into a database: how to use phpexcel to read data and insert into database?

Example upload.php :

//  Include PHPExcel_IOFactory
include 'PHPExcel/IOFactory.php';

// Move the uploaded file from temporary server directory into an 'uploads' directory
$fileName = basename($_FILES["excel-upload"]["name"]);
$uploadedExcelFullPath =  __DIR__.'/uploads/'.$fileName;
move_uploaded_file($_FILES['excel-upload']['tmp_name'],$uploadedExcelFullPath);


//  Read your Excel workbook
try {
    $inputFileType = PHPExcel_IOFactory::identify($uploadedExcelFullPath);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($uploadedExcelFullPath);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($uploadedExcelFullPath,PATHINFO_BASENAME).'": '.$e->getMessage());
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                    NULL,
                                    TRUE,
                                    FALSE);

    //  Insert row data array into your database here

    // You can also format data into HTML table row format and echo out here
    var_dump($rowData);
}
Kevin HR
  • 249
  • 1
  • 6
  • i will try this one – keme Oct 12 '17 at 04:44
  • it aint working and it's a long one, is there any other easy and short way to do it, i mean uploading csv file iiself not data in it. Im trying to upload a csv file into database then display it on my webpage and make it downloadable. – keme Oct 12 '17 at 11:13