I was doing this:

   public DataCellInfo(String heading, Cell dataCell, CellStyle 
defaultStyleDate,int originalOrder) {
        header = heading;
        ordinal = originalOrder;
        column = dataCell.getColumnIndex();
        datatype = dataCell.getCellType();
        dataStyle = dataCell.getCellStyle();
        if (dataStyle ==null && heading.toLowerCase().contains("date")) {
            System.out.println(String.format("Forcing data styling on column 
%s",header));
            dataStyle = defaultStyleDate;
        }
   }

where the incoming dataCell is cell from the input workbook/Sheet1.  The error occurs when I assign the dataStyle member to a new cell in a new sheet of the same, input workbook as here


   private void extendPersonData(Row inr, ReportSubjectAnalysis rsa, 
ReportTissueInfoManager rtim) {

        Row outr = rsa.subjectRow;
        String sampleType = getCurrentSampleType(inr);

        int tissueOffset = rtim.getTypeOffset(sampleType);
        int unitsWritten = rsa.getUnitsWritten(sampleType);
        int lastusedColumn = tissueOffset + (unitsWritten * 
(2+rtim.cellInfoMap.size()));

        Cell ncell = outr.createCell(++lastusedColumn);
        ncell.setCellValue(sampleType);
        ncell.setCellStyle(cellStyleSample);// should see thick border at start 
of bloods, sera etc ncell = outr.createCell(++lastusedColumn);
        
ncell.setCellValue(inr.getCell(SampleReportTransformer.COLUMN_LABID).getStringCellValue());
        int maxOrdinal = rtim.cellInfoMap.size() -1;
        for (Map.Entry<String, DataCellInfo> me : rtim.cellInfoMap.entrySet()) {
            DataCellInfo ci = me.getValue();
            Cell inCell = inr.getCell(ci.column);
            if (inCell ==null) {
                continue;
            }
            Cell sdCell = outr.createCell(lastusedColumn + (ci.getOrdinal()));
     ==>>   sdCell.setCellStyle(ci.dataStyle);
            switch (ci.datatype) {
            case NUMERIC:
                sdCell.setCellValue(inCell.getNumericCellValue());
                break;
            case STRING:
                sdCell.setCellValue(inCell.getStringCellValue());
                break;
            case BLANK:
            case FORMULA:
            case BOOLEAN:
            default:
                System.out.println ("Unexpected data type: " + me.getKey());
            }
        }
   }

The row being written to is in the output Sheet create as follows:

        private void transformToPerson() {
            //setInputHeaders();
            long startAt = System.currentTimeMillis();
            Map<Long, ReportSubjectAnalysis> rsaMap = new HashMap<>();
            ReportTissueInfoManager rtim = firstPass(rsaMap);
   ==>>    Sheet outputSheet = inputBook.createSheet("PersonView");
            setOutputHeaders(outputSheet, rtim);
            secondPass(rtim, outputSheet, rsaMap);
            saveTransformedBook();
            long written = System.currentTimeMillis();
        }

I have corrected the error in my ways by updating the DataCellInfo constructor 
as:

        public DataCellInfo(String heading, Cell dataCell, CellStyle
   defaultStyleDate, int originalOrder, Workbook workbook) {
            header = heading;
            order = originalOrder;
            column = dataCell.getColumnIndex();
            datatype = dataCell.getCellType();
            dataStyle = workbook.createCellStyle();
   dataStyle.cloneStyleFrom(dataCell.getCellStyle());
            if (heading.toLowerCase().endsWith("date")) {
                dataStyle = defaultStyleDate;
                System.out.println(String.format("\tForcing date %s
   styling on column %s", defaultStyleDate.getDataFormatString(),header));
            }
        }

This is a bit splotchy, sorry.  There's four classes and 600 lines of code, all rather desparate at best.  It's been a long time since I have uses POI.  There's no good save point between working and not.  Fishing in intelliJ's local history...


On 12/20/18 4:53 PM, Greg Woolsey wrote:
I'm interested to see how you are creating the new sheet, wondering if
something lost a reference to the StylesTable somehow.  Since I have code
that also manipulates styles and adds/removes cells, I'm interested to see
if a bug was introduced or documentation needs improving, or perhaps some
internal API is exposed without proper documentation or safety checks.

Greg

On Thu, Dec 20, 2018 at 3:05 PM Rob Sargent <[email protected]> wrote:

I have circumvented the situation by using copyStyleFrom(). I posted
because a) I'm setting styles on cell in a new sheet in the origin
workbook - which to me contradicts the helpful hint and b) I had been
doing exactly that in the recent pass.

If you still want my code which generates the error I can back up a
bit.  The partial stack trace I sent was all lines not in my code.


On 12/20/18 3:55 PM, Greg Woolsey wrote:
Can you provide a test case, sample code, or full stacktrace?  How is the
new sheet created? It looks to me from the code that the message means
exactly what it says - the style you are trying to set and the cell you
are
trying to set it on come from different Workbook objects.  Both the style
and the cell hold a reference to the StylesTable object read from (or
created for) the Workbook in which they are found.  POI doesn't support
directly assigning styles across Workbook instances.  See this thread[1]
for a description of why, and what to do instead when you need to copy a
style between workbooks.

Greg

[1]

https://stackoverflow.com/questions/10773961/apache-poi-apply-one-style-to-different-workbooks
On Thu, Dec 20, 2018 at 11:58 AM Rob Sargent <[email protected]>
wrote:
I'm using 4.0.1 as of this morning hoping this would help.  I have
recently begun to get:

      Exception in thread "main" java.lang.IllegalArgumentException: This
      Style does not belong to the supplied Workbook Styles Source. Are
      you trying to assign a style from one workbook to the cell of a
      different workbook?
           at


org.apache.poi.xssf.usermodel.XSSFCellStyle.verifyBelongsToStylesSource(XSSFCellStyle.java:121)
           at

org.apache.poi.xssf.usermodel.XSSFCell.setCellStyle(XSSFCell.java:667)
and I am adding style to a cell in a new sheet, same workbook. The
immediate code is not new and had been working (in 4.0.0). Not sure what
change I made has caused this to start.

I confess the original xlsx has been touched by LibreOffice/Calc and
saved as xlsx.  Linux file command reports:

      file '/home/u0138544/tools/reform/Compicated Query for Rob
Tool.xlsx'
      /home/u0138544/tools/reform/Compicated Query for Rob Tool.xlsx:
      Microsoft OOXML

Any suggestions appreciated.



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to