0

I have implemented import text file, read the data and display on the screen, using the same code how can implement the Excel sheet import, read the data and display on the screen?

Code which I have tried for .txt file import :

HTML CODE

<form action="#" method="post" enctype="multipart/form-data">
    <div> 
        <span>Choose file</span> 
        <span>
            <input type="file" class="upload" name="file"  style="width:260px;" >
        </span> 
    </div>
    <label class="fa-btn btn-1 btn-1e">
         <input type="submit" name="submit" value="Upload Data">
     </label>
</form>

PHP CODE

    if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
         echo "<p align=center style='color:green'><b>The file ".  basename( $_FILES['file']['name'])." has been uploaded</p>";
         $content = file_get_contents('upload/'.$_FILES["file"]["name"]);
         $lines = explode("\n", $content); // TAKING NEW ROW BY ' \n '
}


    foreach ($lines as $line) {
        $row = explode(":", $line); //IN TEXT FILE COLUMN IS SEPARATED BY ' : '
    ?>
    <tr style='background-color:lightgreen;'>
        <td><?php echo $row[0]; ?></td>
        <td><?php echo $row[1]; ?></td>
        <td> <?php echo $row[2]; ?></td>
        <td><?php echo $row[3]; ?></td>
        <td><?php echo $row[4]; ?></td>
        <td><?php echo $row[5]; ?></td>

}

Is it possible to display Excel data in the same way?

UPDATE

As suggested by @Mark Baker

working on this list

Community
  • 1
  • 1
fedrick
  • 341
  • 2
  • 5
  • 21

1 Answers1

1

No it isn't possible to read an Excel file (OfficeOpenXML or BIFF format) as though it was a text file, because it's a binary with a very defined structure that isn't linear. You'll need to work with a library such as PHPExcel to even to read the data.

However, using a library it's possible to read that Excel data and present it as an HTML table (or set of tables if there are multiple worksheets in the file).

EDIT

Example

include './libraries/PHPExcel/Classes/PHPExcel.php';

$inputFileName = './myExcelFile.xls';
$outputFileType = 'HTML';
$outputFileName = 'php://output';

$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);

$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
$objPHPExcel = $objPHPExcelWriter->save($outputFileName);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • do u know any simple library which reads excel sheet, i have seen some of the libraries but there are many files in library, i need short and simple library that satisfies my requirement – fedrick Jul 03 '14 at 07:13
  • yep.. but that library consists of too many files, i need a short and simple library – fedrick Jul 03 '14 at 07:17
  • What has the number of files in a library got to do with whether it's simple to use or not? Have a read of the documentation to see how easy it is to use: but if you can write the code to read an Excel file and generate an html page from that file showing data styles, images etc in 7 lines of code, then I'd say it's simple to use – Mark Baker Jul 03 '14 at 07:24
  • well, i dont want data style or images, i just only need to read some 7 columns and some 10 to 20 rows from an excel sheet. To read this data, is it required to use those many files ? – fedrick Jul 03 '14 at 07:29
  • Then find any other library from [this list](http://stackoverflow.com/questions/3930975/alternative-for-php-excel) that does what you want it to do – Mark Baker Jul 03 '14 at 07:31
  • But when you ask a question, and somebody provides an answer to that question that does what you're asking for, you really should have a better reason for ignoring their answer than "it has too many files"..... well structured libraries are often broken down into more than one file (one for each class), while a single file library (that has every single class all stored in one file) can be incredibly bad – Mark Baker Jul 03 '14 at 07:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56687/discussion-between-prassu-and-mark-baker). – fedrick Jul 03 '14 at 07:35