Ignore all of this message. 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. To quote from the documentation;
Various utility functions that make working with a cells and rows easier. The various methods that deal with style's allow you to create your HSSFCellStyles as you need them. When you apply a style change to a cell, the code will attempt to see if a style already exists that meets your needs. If not, then it will create a new style. This is to prevent creating too many styles. there is an upper limit in Excel on the number of styles that can be supported. The section I liked was 'the code will attempt to see if a style already exists.......' --- On Thu, 10/16/08, Anthony Andrews <[EMAIL PROTECTED]> wrote: From: Anthony Andrews <[EMAIL PROTECTED]> Subject: Re: HSSFCellStyle - global styles vs individual cell styles To: "POI Users List" <[email protected]> Date: Thursday, October 16, 2008, 12:10 AM You can easilly add this feature Rob. Think about simple 'pooling' of which there are many examples in the core API; all you need to do is create some way of keeping track of the cell styles as you create them. As a crude example, you could use an ArrayList to hold references to all of your cell styles. As you come across a requirement for a new cell style simply query the ArrayList to see if one with those attributes already exists and if it does grab the exiting reference to it. If a suitable cell style does not exist, add the new one to the list...etc. Or, you could simply have a method called something like removeDuplicateStyles() that you call prior to saving the workbook away. This method would iterate thropugh the sheet removing any duplicate cell styles. I am not too sure about this and you would have to test it but I do believ that the equals() method the the HSSFCellStyle class has been over-ridden to make it easy to compare cell styles for equality. I do have to agree with you however that this would incur some overhead but I do not feel that there is anyway to avoid it. By the way, the limit on cell styles IS imposed by Excel and POI merely has to work within the constraint. HSSF is a community project and I am pretty sure that if you want to contribute a feature or features that could improve this aspect of the API, then the development team would respond positivly if you were to approach them with an idea. --- On Wed, 10/15/08, Rob Y <[EMAIL PROTECTED]> wrote: From: Rob Y <[EMAIL PROTECTED]> Subject: HSSFCellStyle - global styles vs individual cell styles To: [email protected] Date: Wednesday, October 15, 2008, 11:34 AM In Apache POI-HSSF, it seems like the HSSFCellStyle is global to the workbook. I assume that means that in order to, say, color cells as you add them, creating a new HSSFCellStyle for each would be hugely inefficient. In fact I've seen lots of messages here saying that Excel will complain if you create too many cell styles. But if my data source makes it difficult to know all possible cell styles upfront (especially, since cell styles are combinations of font, color, border, etc), how can I know when I need to create a new one? I suppose I could keep a table of all the style parameter combinations I've encountered, but doesn't the library already do this? And if I did create a sheet where all the green cells shared a single cell style, would a user changing a single cell's color end up changing them all? Or would Excel be smart enough to detect that the cell now needs a new style created? I guess this boils down to a few questions: Is there a way to query your workbook while building it to find out whether a cell style you want to use matches one that's already in the workbook and to use that instead? If so, wouldn't it be nice to automate that and allow you to just set individual styles without incurring the inefficiency? I can't imagine Excel has a limit on how many unique combinations of cell attributes you can have. If that's true, does xls have some other mechanism for coloring cells, etc that isn't supported by poi? Rob --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
