There are options available to you from within the core API. As I am sure you 
know, the core API contains 'wrapper' classes for the numeric data types - 
java.lang.Integer for example. They allow you to create objects that represent 
numeric values from Strings and all throw NumberFormat exceptions if the String 
you pass to them is invalid. What would be wrong with a piece of logic 
something like;

Get the String from the CSV file.
Try to create an instance of the java.lang.Double class. If this succeeds, 
assume that we have a floating point number and add a cell to the worksheet.
If the above step throws an exception, catch that and try to create an instance 
of the java.lang.Integer class. If this succeeds, assume we have an integral 
numeric value and add a cell to the sheet.
If this fails, assume we have a String and add it to the sheet.

Of course, you could extend this to handle dates, could add extra checks to see 
if there was a currency sign all cues that coudl tell you how to correctly 
handle the data.

--- On Wed, 10/15/08, Rob Y <[EMAIL PROTECTED]> wrote:
From: Rob Y <[EMAIL PROTECTED]>
Subject: Excel complains when I put numeric strings into text cells
To: [email protected]
Date: Wednesday, October 15, 2008, 11:19 AM

I'm writing a program to import application-formatted CSV data into an excel
spreadsheet using POI-HSSF.
For the most part the app is telling me whether the data's text, numeric,
date,
etc.  But not always.

As a quick and dirty first stab, I tried just importing all the data as strings
and leaving the cell data type as the default (text, I assume).
This works, but when I open the xls in Excel, it puts up little green triangles
in the numeric cells and pops up an 'exclamation' icon when I select
them.
It's complaining that I've got a number stored as text.

When you import a csv file into excel directly, this doesn't happen.
I assume Excel makes it's best attempt to figure out the data type of the
csv
text and formats the cells appropriately.
Is there any way to get POI to do that as well?

Another issue is that POI just fails outright (probably throwing an exception
that I'm not catching) if I simply set the cell value from a string and
then
change the cell data type to numeric.
There may well be cases where the app that's generating the CSV data passes
me
improperly identified data (the app's just formatting it for display in a
data
grid that isn't very finicky about data types).
If I'm not really careful, I'm gonna get into trouble with the cell
data types.
I suppose I can always fall back to text when I'm not sure, but then Excel
will
complain.

Any ideas?

Thanks,
Rob


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




      

Reply via email to