[prev in list] [next in list] [prev in thread] [next in thread] 

List:       poi-user
Subject:    Re: Reading CELL_TYPE_NUMERIC as text
From:       "Des Hartman" <des () deshartman ! com>
Date:       2008-12-17 20:48:00
Message-ID: 8773d42e0812171248w4e18d4efoed40b1c0b43ffa69 () mail ! gmail ! com
[Download RAW message or body]


Anthony,

Yeah that was my initial thought, but if you look at the table the third
column contains legitimate Double values and I do not want to truncate these
to int.

Is there any way I can fiddle with Style properties so that when it reads
the cell and there are no values after the "." it will present it as
integer. I did the following test to see the styles:

--------------------------------

case HSSFCell.CELL_TYPE_NUMERIC:
                            double dval = cell.getNumericCellValue();
                            value = String.valueOf(dval);
    System.out.println(cell.getCellStyle().getDataFormatString()+ " -
Numeric value = " + value);
                            break;

case HSSFCell.CELL_TYPE_STRING:
                            value =
cell.getRichStringCellValue().getString();

System.out.println("cell.getCellStyle().getDataFormatString()+ " - String
value = " + value);
break;

------------------------------

General <- String value = sometext
@ <- String value = 2
0.0000 <- Numeric value = 0.1234
General <- String value = someother
@ <- Numeric value = 57.0
0.0000 <- Numeric value = 0.1

So if there is some way in which I can test for
cell.getCellStyle().getDataFormatString() == "@" and then write the value as
an integer?

Thanks
Des



2008/12/18 Anthony Andrews <pythonaddict@yahoo.com>

> Hi Des
>
> I do not think that you can change the type of a cell once it has been
> initialized - that is once it's type has been set and it holds some data -
> but I could well be wrong.
>
> Can you simply assume that if you see a numeric cell it contains an
> integral value? If so, you could probably simply copy the value into an int
> and rely on it's truncating the value.
>
> --- On Wed, 12/17/08, Des Hartman <des@deshartman.com> wrote:
> From: Des Hartman <des@deshartman.com>
> Subject: Reading CELL_TYPE_NUMERIC as text
> To: "POI Users List" <user@poi.apache.org>
> Date: Wednesday, December 17, 2008, 3:44 AM
>
> Hi,
>
> I have an interesting issue that I could do with some help on. I had a look
> through the mailing list, but there does not quite seem to be an answer.
>
> I am reading a spreadsheet in and converting it to CSV for further
> processing. The columns are a mixture of String and numeric. As an example:
>
>
> sometext      57         0.1234
> someother    2           0.1
>
> When reading it in, column one triggers case HSSFCell.CELL_TYPE_STRING and
> I
> process it no problem.
>
> However the next two values causes me some grief because of formatting on
> the excel sheet which I have no control over. the first row the value 57 is
> formatted as a number and I read it in using HSSFCell.CELL_TYPE_NUMERIC.
> The
> resulting text value is 57.0. The next row the number is formatted as text,
> i.e., '2 and the result is 2 (Which is the desired result)
>
> Is there a way to somehow change the cell style to be text before reading,
> maybe even change the entire sheet to text?
>
> It will be read-only processing.
>
>
> --
> Thanks
> Des Hartman
>
>
>
>
>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic