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

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

7 Comments

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
yep.. but that library consists of too many files, i need a short and simple library
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
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 ?
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.