xlsx to text with php

phpexcel
php excel writer
phpexcel example
phpexcel_iofactory php
convert text file to excel php
phpexcel php 7
phpexcel library documentation
extract phpexcel library

I have following function to convert xlsx to text..but it does not give space for new cell.. means for different cell it directly appends to previous word..

function xlsx_to_text($input_file){
    $xml_filename = "xl/sharedStrings.xml"; //content file name
    $zip_handle = new ZipArchive;
    $output_text = "";
    if(true === $zip_handle->open($input_file)){ 
        if(($xml_index = $zip_handle->locateName($xml_filename)) !== false){ 
            $xml_datas = $zip_handle->getFromIndex($xml_index);
           $xml_handle = new DOMDocument();
           $xml_handle->loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            $output_text = strip_tags($xml_handle->saveXML());
        }else{ 
            $output_text .="";
        }
        $zip_handle->close();
    }else{
    $output_text .="";
    }
    return $output_text;
}

I have output like.. Ihaveanapple.. but it should be like I have an apple

please help me to get out of this..

I got an answer.

function xlsx_to_text($input_file){
   require('SpreadsheetReader.php');

    $Reader = new SpreadsheetReader($input_file);
    $totalCount = 0;
    $test=0;
    foreach ($Reader as $Row)
    {   

        $count_rows = $Row;

        foreach($count_rows as $countRow){
            if($countRow != ''){
                $taa = explode(' ',$countRow);
                $tCount = count($taa);
                $result[]=$countRow." ";
            }

        }

    }
    return implode("",$result);
}

write below code in SpreadsheetReader.php

<?php

    class SpreadsheetReader implements SeekableIterator, Countable
    {
        const TYPE_XLSX = 'XLSX';
        const TYPE_XLS = 'XLS';
        const TYPE_CSV = 'CSV';
        const TYPE_ODS = 'ODS';

        private $Options = array(
            'Delimiter' => '',
            'Enclosure' => '"'
        );

        /**
         * @var int Current row in the file
         */
        private $Index = 0;

        /**
         * @var SpreadsheetReader_* Handle for the reader object
         */
        private $Handle = array();

        /**
         * @var TYPE_* Type of the contained spreadsheet
         */
        private $Type = false;

        /**
         * @param string Path to file
         * @param string Original filename (in case of an uploaded file), used to determine file type, optional
         * @param string MIME type from an upload, used to determine file type, optional
         */
        public function __construct($Filepath, $OriginalFilename = false, $MimeType = false)
        {
            if (!is_readable($Filepath))
            {
                throw new Exception('SpreadsheetReader: File ('.$Filepath.') not readable');
            }

            // To avoid timezone warnings and exceptions for formatting dates retrieved from files
            $DefaultTZ = @date_default_timezone_get();
            if ($DefaultTZ)
            {
                date_default_timezone_set($DefaultTZ);
            }

            // Checking the other parameters for correctness

            // This should be a check for string but we're lenient
            if (!empty($OriginalFilename) && !is_scalar($OriginalFilename))
            {
                throw new Exception('SpreadsheetReader: Original file (2nd parameter) path is not a string or a scalar value.');
            }
            if (!empty($MimeType) && !is_scalar($MimeType))
            {
                throw new Exception('SpreadsheetReader: Mime type (3nd parameter) path is not a string or a scalar value.');
            }

            // 1. Determine type
            if (!$OriginalFilename)
            {
                $OriginalFilename = $Filepath;
            }

            $Extension = strtolower(pathinfo($OriginalFilename, PATHINFO_EXTENSION));

            switch ($MimeType)
            {
                case 'text/csv':
                case 'text/comma-separated-values':
                case 'text/plain':
                    $this -> Type = self::TYPE_CSV;
                    break;
                case 'application/vnd.ms-excel':
                case 'application/msexcel':
                case 'application/x-msexcel':
                case 'application/x-ms-excel':
                case 'application/vnd.ms-excel':
                case 'application/x-excel':
                case 'application/x-dos_ms_excel':
                case 'application/xls':
                case 'application/xlt':
                case 'application/x-xls':
                    // Excel does weird stuff
                    if (in_array($Extension, array('csv', 'tsv', 'txt')))
                    {
                        $this -> Type = self::TYPE_CSV;
                    }
                    else
                    {
                        $this -> Type = self::TYPE_XLS;
                    }
                    break;
                case 'application/vnd.oasis.opendocument.spreadsheet':
                case 'application/vnd.oasis.opendocument.spreadsheet-template':
                    $this -> Type = self::TYPE_ODS;
                    break;
                case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
                case 'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
                case 'application/xlsx':
                case 'application/xltx':
                    $this -> Type = self::TYPE_XLSX;
                    break;
                case 'application/xml':
                    // Excel 2004 xml format uses this
                    break;
            }

            if (!$this -> Type)
            {
                switch ($Extension)
                {
                    case 'xlsx':
                    case 'xltx': // XLSX template
                    case 'xlsm': // Macro-enabled XLSX
                    case 'xltm': // Macro-enabled XLSX template
                        $this -> Type = self::TYPE_XLSX;
                        break;
                    case 'xls':
                    case 'xlt':
                        $this -> Type = self::TYPE_XLS;
                        break;
                    case 'ods':
                    case 'odt':
                        $this -> Type = self::TYPE_ODS;
                        break;
                    default:
                        $this -> Type = self::TYPE_CSV;
                        break;
                }
            }

            // Pre-checking XLS files, in case they are renamed CSV or XLSX files
            if ($this -> Type == self::TYPE_XLS)
            {
                self::Load(self::TYPE_XLS);
                $this -> Handle = new SpreadsheetReader_XLS($Filepath);
                if ($this -> Handle -> Error)
                {
                    $this -> Handle -> __destruct();

                    if (is_resource($ZipHandle = zip_open($Filepath)))
                    {
                        $this -> Type = self::TYPE_XLSX;
                        zip_close($ZipHandle);
                    }
                    else
                    {
                        $this -> Type = self::TYPE_CSV;
                    }
                }
            }

            // 2. Create handle
            switch ($this -> Type)
            {
                case self::TYPE_XLSX:
                    self::Load(self::TYPE_XLSX);
                    $this -> Handle = new SpreadsheetReader_XLSX($Filepath);
                    break;
                case self::TYPE_CSV:
                    self::Load(self::TYPE_CSV);
                    $this -> Handle = new SpreadsheetReader_CSV($Filepath, $this -> Options);
                    break;
                case self::TYPE_XLS:
                    // Everything already happens above
                    break;
                case self::TYPE_ODS:
                    self::Load(self::TYPE_ODS);
                    $this -> Handle = new SpreadsheetReader_ODS($Filepath, $this -> Options);
                    break;
            }
        }

        /**
         * Gets information about separate sheets in the given file
         *
         * @return array Associative array where key is sheet index and value is sheet name
         */
        public function Sheets()
        {
            return $this -> Handle -> Sheets();
        }

        /**
         * Changes the current sheet to another from the file.
         *  Note that changing the sheet will rewind the file to the beginning, even if
         *  the current sheet index is provided.
         *
         * @param int Sheet index
         *
         * @return bool True if sheet could be changed to the specified one,
         *  false if not (for example, if incorrect index was provided.
         */
        public function ChangeSheet($Index)
        {
            return $this -> Handle -> ChangeSheet($Index);
        }

        /**
         * Autoloads the required class for the particular spreadsheet type
         *
         * @param TYPE_* Spreadsheet type, one of TYPE_* constants of this class
         */
        private static function Load($Type)
        {
            if (!in_array($Type, array(self::TYPE_XLSX, self::TYPE_XLS, self::TYPE_CSV, self::TYPE_ODS)))
            {
                throw new Exception('SpreadsheetReader: Invalid type ('.$Type.')');
            }

            // 2nd parameter is to prevent autoloading for the class.
            // If autoload works, the require line is unnecessary, if it doesn't, it ends badly.
            if (!class_exists('SpreadsheetReader_'.$Type, false))
            {
                require(dirname(__FILE__).DIRECTORY_SEPARATOR.'SpreadsheetReader_'.$Type.'.php');
            }
        }

        // !Iterator interface methods

        /** 
         * Rewind the Iterator to the first element.
         * Similar to the reset() function for arrays in PHP
         */ 
        public function rewind()
        {
            $this -> Index = 0;
            if ($this -> Handle)
            {
                $this -> Handle -> rewind();
            }
        }

        /** 
         * Return the current element.
         * Similar to the current() function for arrays in PHP
         *
         * @return mixed current element from the collection
         */
        public function current()
        {
            if ($this -> Handle)
            {
                return $this -> Handle -> current();
            }
            return null;
        }

        /** 
         * Move forward to next element. 
         * Similar to the next() function for arrays in PHP 
         */ 
        public function next()
        {
            if ($this -> Handle)
            {
                $this -> Index++;

                return $this -> Handle -> next();
            }
            return null;
        }

        /** 
         * Return the identifying key of the current element.
         * Similar to the key() function for arrays in PHP
         *
         * @return mixed either an integer or a string
         */ 
        public function key()
        {
            if ($this -> Handle)
            {
                return $this -> Handle -> key();
            }
            return null;
        }

        /** 
         * Check if there is a current element after calls to rewind() or next().
         * Used to check if we've iterated to the end of the collection
         *
         * @return boolean FALSE if there's nothing more to iterate over
         */ 
        public function valid()
        {
            if ($this -> Handle)
            {
                return $this -> Handle -> valid();
            }
            return false;
        }

        // !Countable interface method
        public function count()
        {
            if ($this -> Handle)
            {
                return $this -> Handle -> count();
            }
            return 0;
        }

        /**
         * Method for SeekableIterator interface. Takes a posiiton and traverses the file to that position
         * The value can be retrieved with a `current()` call afterwards.
         *
         * @param int Position in file
         */
        public function seek($Position)
        {
            if (!$this -> Handle)
            {
                throw new OutOfBoundsException('SpreadsheetReader: No file opened');
            }

            $CurrentIndex = $this -> Handle -> key();

            if ($CurrentIndex != $Position)
            {
                if ($Position < $CurrentIndex || is_null($CurrentIndex) || $Position == 0)
                {
                    $this -> rewind();
                }

                while ($this -> Handle -> valid() && ($Position > $this -> Handle -> key()))
                {
                    $this -> Handle -> next();
                }

                if (!$this -> Handle -> valid())
                {
                    throw new OutOfBoundsException('SpreadsheetError: Position '.$Position.' not found');
                }
            }

            return null;
        }
    }
?>

How can PHP Read Excel File xlsx format using SimpleXLSX: Parse , This class can be used to parse and retrieve data from Excel XLS spreadsheet files. It can parse a given Excel XLS file by extracting its contents files and parsing  Click on the following link to convert our demo file from XLSX to TXT: XLSX to TXT conversion with our XLSX example file. XLSX, Microsoft Excel Open XML Spreadsheet (.xlsx) XLSX files are produced by the applications of Microsoft's 2007 Office suite, namely by the product called Microsoft Excel, a spreadsheet processing software that is known

Why don't you use some existing libraries like Spout or PHPExcel to read data from your XLSX files? There are a lot of edge cases that these libraries have already solved. Some examples: security with the XML file, inline data, metadata stored in sharedStrings.xml, large XML file... Doing it yourself means you will have to find all these edge cases again and handle them in your code.

Parsing the sharedStrings.xml file is unfortunately not as easy as calling strip_tags($xml_handle->saveXML());.

Now your code should kind of work if the string "I have an apple" is in one cell. It seems like each word is in a different cell so your XML looks like this: <si><t>I</t></si> <si><t>have</t></si> <si><t>an</t></si> <si><t>apple</t></si>

So when you remove all the tags, you end up with "Ihaveanapple", as no spaces get inserted.

(Excel, CSV, TXT, XML, HTML, PDF) in PHP using , Use the sample source code below to extract and convert spreadsheets between various formats such as TXT, XLS, XLSX, XML, CSV, PDF, HTML in PHP using  DOC, DOCX, PPT, XLSX to TXT in PHP. GitHub Gist: instantly share code, notes, and snippets.

Why not use PhpOffice/PhpSpreadsheet.

<?php
// Create a new Reader of the type defined in $inputFileType
// If file type is Xlsx / Csv this package will read.
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");

// Load $inputFileName to a PhpSpreadsheet Object
$spreadsheet = $reader->load($xlsxFileName);
$worksheet = $spreadsheet->getActiveSheet();

// TODO: Add a const for Xlsx and Csv
$dataEndRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();

$headersArrayList = $worksheet->rangeToArray("A1:{$highestColumn}1", null, true, true, true);
if (!isset($headersArrayList[1])) {
    return;
}
$headersArray = $headersArrayList[1];
$data = $worksheet->rangeToArray("A2:{$highestColumn}{$dataEndRow}", null, true, true, true);
?>

The data will contain two dimensional array. Rows keys will be integer and Columns keys will be string.

Accessing a cell, $data[2][A] will give you value in 2nd row, A column of your spreadsheet.

Note: This package requires xmlreader, xml, dom, xmlwriter php extensions

Parse simple XLSX in PHP with SimpleXML and ZipArchive · GitHub, work if cells contain anything more than text or a number (so formulas,. * graphs, etc , I don't know what'd happen). */. $inputFile = '/path/to/spreadsheet.​xlsx';. I was unable to find a minimal XLSX (Excel) parser. The closest free option would be apache POI with about 8+ megabyte of JARs and features I don't need. The input is an XLSX file and the output is a Map<String,String[][]> with key of 'sheet name' and a String[][] of cell data. Dates are reformatted to dd/MM/yyyy (configurable in the code).

DOC, DOCX, PPT, XLSX to TXT in PHP · GitHub, DOC, DOCX, PPT, XLSX to TXT in PHP. officetophp.php. /*determine th file extension. $file_name = $_FILES['image']['name'];. $file_extn = end(explode(". simplexlsxgen 0.9.10. MIT/X Consortium 5. XML, Files and Folders. Description. Author. This package can export data in Excel XLSX format. It can take an array of data with the definition of the values that will fill a spreadsheet and exports that spreadsheet definition to MicroSoft Excel XLS format.

Rich text format for Excel cells in PHP, Code sample PHP: Export Excel file with rich text format in cells using EasyXLS library. XLSX, XLSM, XLSB, XLS file in PHP. Files can be saved in the XLSX format, which is based on the Open XML format and uses ZIP compression for smaller file sizes. Actions: XLSX to TXT - Convert file now View other document file formats: Technical Details: XLSX improves file and data management and data recovery. XLSX extends what is possible with the binary files of earlier versions.

Exporting Data to Excel < PHP, PHP header("Content-Type: text/plain"); $flag = false; foreach($data as $row) Because of the .xls extension and the ms-excel file type, most computers will  XLSX – is an e-book created with one of the versions of the Excel program older than 2007. Thus the data have the form of the tables consisting of columns and lines. Each table is divided into cells that have a unique address, which consists of numeric and alphabetic characters.

Comments
  • replace one of the tags with a space
  • but actually with same file, i got desired result from reference site..
  • not sure what that means but i imagine they have no spaces between tags so you have to had them
  • PHPExcel is a superb one for this purpose.