Rob

After getting up early to watch the qualifying for the next Grand Prix, I had a 
few hours to think about the application you are working on and to throw 
together a bit of code.

Am I correct in assuming that you are reading in data from a CSV file and using 
that to populate an Excel spreadsheet? If so then you may not have any problems 
with cell styles at all.

Typically, in such an application, you will know in advance just how many 
different sorts of cell you will be dealing with and, therefore, how many 
different cell styles you need to create. Usually, the problem becomes 
identifying the different data types and populating the spreadsheet 
accordingly. Take a look at the Java file I have attached to this e-mail. It 
processes the CSV file that I have also attached and populates a spreadsheet 
with the data. If you look at the code, you will be able to see that it uses 
regular expressions to determine the data type and sets the cell type and style 
accordingly.

The regular expressions bit does look intimidating at first but there is lots 
of help on the internet to help you define expressions that work properly and 
all you need to do is become used to the syntax. To get you started, my code 
uses this regular expression to check for dates;

(0[1-9]|[12][0-9]|3[01])  [- /.]  (0[1-9]|1[012])  [- /.]  
(((19|20)\\d\\d)|(\\d\\d))

Look for this pattern in that string - [- /.] - you should see it twice because 
it is checking for the separator which in this case could be a hyphen, a space, 
a foward slash or a full stop. Now look at the sub-expression that precedes the 
first separator pattern;

(0[1-9] | [12][0-9] | 3[01])

This will ensure that the day portion of the date is valid. As with Java, the | 
character can be read as 'or' so again, you can break this down into sections 
0[1-9], [12][0-9] and 3[01]. The first checks for values in the range 0, 1, 2, 
3, 4, 5, 6, 7, 8 and 9, the second for values in the range 10 to 29 and the 
final portion for 30 and 31. Looking at the first section in more detail, it 
says, in effect, a leading zero followed by a single digit in the range 1 to 9 
is valid. In similar fashion, the third section says in effect that a 3 
followed be either a zero or a one is valid. Luckilly the second section 
becomes easier to understand once you grasp this basic syntax, it says that the 
first digit can be either a 1 or a 2 and the second digit and number in the 
range 0 to 9.

Have a look and see if it makes sense to you. Also, think about the cell styles 
issue as I do not think that you need to worry about comparing styles at all, 
you can simply set them up before processing the data and apply accordingly. 
The challenge then becomes determining when to apply which style.

--- On Fri, 10/17/08, Rob Y <[EMAIL PROTECTED]> wrote:
From: Rob Y <[EMAIL PROTECTED]>
Subject: Re: HSSFCellStyle - global styles vs individual cell styles
To: [email protected]
Date: Friday, October 17, 2008, 11:56 AM

> Of course I will Rob. At home now so it will take me a few hours to search
the
code out. There is one limit
> however, I was working with just a single workbook and comparing styles
within
the workbook so to speak. I
> know that problems can arise if you are using more than one workbook - for
example if you are copying over
> from one workbook to another - with fonts colours etc.

I'm in luck - one workbook, one worksheet - for now ;)

For yucks, I tried writing a function that createsappropriate cell types
from a string.  Pretty easy (not ready to try regex's yet, but...):

    // Add a new cell
    public void addCell(int rownum, int colnum, String text)
    {
        HSSFRow r = sheet.getRow((short) rownum);
        HSSFCell c = r.createCell((short) colnum);
        
        // Determine data type.  Try numeric first.
        try
        {
            double value = java.lang.Double.parseDouble(text);
            c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            c.setCellValue(value);
        }
        catch (NumberFormatException nfe)
        {
            // Not numeric, try date.
            try
            {
                SimpleDateFormat df = new SimpleDateFormat("MM/d/yy");
                Date date = df.parse(text);
                c.setCellStyle(datestyle);
                c.setCellValue(date);
            }
            catch (ParseException pe)
            {
                // Not date, default to text
                HSSFRichTextString s = new HSSFRichTextString(text);
                c.setCellStyle(orangecell);
                c.setCellType(HSSFCell.CELL_TYPE_STRING);
                c.setCellValue(s);
            }
        }
    }



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



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to