Ah well, got too excited about both of these issues it seems. Martin's suggestion of using regular expressions to sort out the data type of the String you read from the CSV file is looking like the best bet for you on the one hand it seems. Further, I reckon you will have to handle style pooling yourself. I did something similar for copying worksheets from one workbook to another and it is a quite simple task - if a little long-winded as you will have to compare each style attribute.
Sorry if you wasted any time with the cell utility class, I only spotted it whilst having a quick break and did not have the opportunity to test it out. --- On Thu, 10/16/08, Rob Y <[EMAIL PROTECTED]> wrote: From: Rob Y <[EMAIL PROTECTED]> Subject: Re: HSSFCellStyle - global styles vs individual cell styles To: [email protected] Date: Thursday, October 16, 2008, 9:34 AM > I have just been looking through the api docs and come across a class called HSSFCellUtil. One if it's > methods allows you to create a cell from a String and to pass a style object in addition. Thanks for pointing me to this. It doesn't do what I want, but it verfies that I can do it myself in the way I thought I'd have to. Unfortunately, the createCell method just applies a style to the cell. Presumably the caller's responsible for creating the style, leaving the issue of sharing styles unaddressed: Here's the createCell code from HSSFCellUtil: public static HSSFCell createCell( HSSFRow row, int column, String value, HSSFCellStyle style ) { HSSFCell cell = getCell( row, column ); cell.setCellValue(new HSSFRichTextString(value)); if ( style != null ) { cell.setCellStyle( style ); } return cell; } The other method, setCellStyleProperty kind of does what I want. It scans all the styles in the workbook, looking for one with the property you asked for. If it finds one, it uses it. If not, it allocates a new one. The problem with this method is that it only supports one property at a time. So if you wanted to set font, color, data type, etc. You'd end up creating a series of styles that accumulate these properties one at a time. Only the last style would actually end up referenced by the cell, but the others would still be there in the workbook - kind of silly. What I want is to create a reference style (via cloneStyleFrom?), set all the properties I want and then pass that in to an API that does what setCellStyleProperty does, but based on matching all the properties in a style at once. Sound right? Here's the setCellStyleProperty code from HSSFCellUtil: public static void setCellStyleProperty( HSSFCell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue ) throws NestableException { try { HSSFCellStyle originalStyle = cell.getCellStyle(); HSSFCellStyle newStyle = null; Map values = PropertyUtils.describe( originalStyle ); values.put( propertyName, propertyValue ); values.remove( "index" ); // index seems like what index the cellstyle is in the list of styles for a workbook. // not good to compare on! short numberCellStyles = workbook.getNumCellStyles(); for ( short i = 0; i < numberCellStyles; i++ ) { HSSFCellStyle wbStyle = workbook.getCellStyleAt( i ); Map wbStyleMap = PropertyUtils.describe( wbStyle ); wbStyleMap.remove( "index" ); if ( wbStyleMap.equals( values ) ) { newStyle = wbStyle; break; } } if ( newStyle == null ) { newStyle = workbook.createCellStyle(); newStyle.setFont( workbook.getFontAt( originalStyle.getFontIndex() ) ); PropertyUtils.copyProperties( newStyle, originalStyle ); PropertyUtils.setProperty( newStyle, propertyName, propertyValue ); } cell.setCellStyle( newStyle ); } catch ( Exception e ) { e.printStackTrace(); throw new NestableException( "Couldn't setCellStyleProperty.", e ); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
