如何使用xlsx reader中的特殊字符获取单元格数据

时间:2016-01-16 11:41:08

标签: php excel xlsx

我正在使用Simplexlsx php类从xlsx文件读取数据并导入数据库。

我面临的一个问题是谷歌之后我没有找到它,问题是在xlsx列中有一些值如100%,$ 300但是库正在解析数据并给出如下输出:1,300 - 就像它的评估一样值和删除符号。

Excel文件: enter image description here

使用库输出解析后:

[10] => SimpleXMLElement Object
        (
          [v] => 5
        )
[11] => SimpleXMLElement Object
        (
          [v] => 1.2
        )
[12] => SimpleXMLElement Object
        (
          [v] => 1.1000000000000001
        )
[13] => SimpleXMLElement Object
        (
          [v] => 1
        )

我的预期应该是这样的:

[10] => SimpleXMLElement Object
            (
              [v] => 2.5%
            )
    [11] => SimpleXMLElement Object
            (
              [v] => 2%
            )
    [12] => SimpleXMLElement Object
            (
              [v] => 1.50%
            )
    [13] => SimpleXMLElement Object
            (
              [v] => 1%
            )

这是我正在使用的类链接: http://www.phpclasses.org/package/6279-PHP-Parse-and-retrieve-data-from-Excel-XLS-files.html

也适用于Git但旧版本: https://github.com/raulferras/simplexlsx

这是我的文件解析函数,上面输出我从echo语句得到你在$ sheet解析条件下面的函数中看到:

function _parse() {
        // Document data holders
        $this->sharedstrings = array();
        $this->sheets = array();
//      $this->styles = array();

        // Read relations and search for officeDocument
        if ( $relations = $this->getEntryXML("_rels/.rels" ) ) {

            foreach ($relations->Relationship as $rel) {

                if ($rel["Type"] == SimpleXLSX::SCHEMA_REL_OFFICEDOCUMENT) {

//                      echo 'workbook found<br />';
                    // Found office document! Read workbook & relations...

                    // Workbook
                    if ( $this->workbook = $this->getEntryXML( $rel['Target'] )) {

//                      echo 'workbook read<br />';

                        if ( $workbookRelations = $this->getEntryXML( dirname($rel['Target']) . '/_rels/workbook.xml.rels' )) {

//                          echo 'workbook relations<br />';

                            // Loop relations for workbook and extract sheets...
                            foreach ($workbookRelations->Relationship as $workbookRelation) {

                                $path = dirname($rel['Target']) . '/' . $workbookRelation['Target'];

                                if ($workbookRelation['Type'] == SimpleXLSX::SCHEMA_REL_WORKSHEET) { // Sheets

//                                  echo 'sheet<br />';

                                    if ( $sheet = $this->getEntryXML( $path ) ) {
                                        $this->sheets[ str_replace( 'rId', '', (string) $workbookRelation['Id']) ] = $sheet;
                                        echo '<pre>'.htmlspecialchars( print_r( $sheet, true ) ).'</pre>';
                                    }

                                } else if ($workbookRelation['Type'] == SimpleXLSX::SCHEMA_REL_SHAREDSTRINGS && $this->entryExists( $path )) { // 0.6.6

//                                  echo 'sharedstrings<br />';

                                    if ( $sharedStrings = $this->getEntryXML( $path ) ) {
                                        foreach ($sharedStrings->si as $val) {
                                            if (isset($val->t)) {
                                                $this->sharedstrings[] = (string)$val->t;
                                            } elseif (isset($val->r)) {
                                                $this->sharedstrings[] = $this->_parseRichText($val);
                                            }
                                        }
                                    }
                                } else if ($workbookRelation['Type'] == SimpleXLSX::SCHEMA_REL_STYLES) {
                                    $this->styles = $this->getEntryXML( $path );

                                    $nf = array();
                                    if ( $this->styles->numFmts->numFmt != NULL )
                                        foreach( $this->styles->numFmts->numFmt as $v )
                                            $nf[ (int) $v['numFmtId'] ] = (string) $v['formatCode'];

                                    if ( $this->styles->cellXfs->xf != NULL )   
                                        foreach( $this->styles->cellXfs->xf as $v ) {
                                            $v = (array) $v->attributes();
                                            $v = $v['@attributes'];
                                            if (isset($this->built_in_cell_formats[ $v['numFmtId'] ]) )
                                                $v['format'] = $this->built_in_cell_formats[ $v['numFmtId'] ]; 
                                            else if (isset($nf[ $v['numFmtId'] ]))
                                                $v['format'] = $nf[ $v['numFmtId'] ];
                                            else
                                                $v['format'] = '';
                                            $this->workbook_cell_formats[] = $v;
                                        }
                                        //print_r( $this->workbook_cell_formats ); exit; 
                                }
                            }

                            break;
                        }
                    }
                }
            }
        } 
        // Sort sheets
        ksort($this->sheets);
    }

任何帮助都将非常感激。提前谢谢。

0 个答案:

没有答案