Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3457340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:50:54+00:00 2026-05-18T09:50:54+00:00

i’m trying to read some excel files with phpexcel, which works ok so far.

  • 0

i’m trying to read some excel files with phpexcel, which works ok so far. phpexcel is a bit too clever though, and removes leading zeros from my cells. i guess i need to tell it to treat the cell as a text string, not as general or number. but i’ve failed. i even found threads on stackoverflow about this but the suggested solutions simply wouldn’t work.

below is a snippet of the stuff i’m working on.

foreach(@$objWorksheet->getRowIterator() as $row){
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);
    foreach($cellIterator as $cell){
        #$objWorksheet->getStyle($cell->getCoordinate())->getNumberFormat()->setFormatCode('@');
        $cells[] = $cell->getValue();
    }
}

any idea? i don’t want to limit myself to only read numeric content, as i don’t have any control over what the users will upload. treating everything as strings would be perfect.

/peder

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:50:54+00:00Added an answer on May 18, 2026 at 9:50 am

    The getValue() method does exactly what it should do, without trying to be clever. It returns the actual value stored in the cell. If that cell contains an integer, it returns an integer; if the cell contains a float, it returns a float; if it contains a string, it returns a string. Being clever is returning that value as a formatted string (with leading zeroes if appropriate), then you need to use a rather different method, and apply the cell formatting to the returned value.

    foreach($cellIterator as $cell){ 
       $cells[] = PHPExcel_Style_NumberFormat::toFormattedString( $cell->getValue(),
                  $objPHPExcel->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
       );
    }
    

    or if the cell contains a formula:

    foreach($cellIterator as $cell){ 
       $cells[] = PHPExcel_Style_NumberFormat::toFormattedString( $cell->getCalculatedValue(),
                  $objPHPExcel->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
       );
    }
    

    And please don’t use @ to try and suppress errors. PHPExcel throws exceptions, and you really should want to trap these.

    However, for what you’re doing, you might consider the worksheet’s toArray() method, that will return an array of all the cell values in the worksheet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.