Page 1 of 1

Cells with character values

PostPosted: Mon Oct 03, 2011 7:20 pm
by MaSEL
Making cells with numeric values causes no errors at the opening file in the MS Excel 2007.
Making cells with numeric values causes error of unrecoverable content.

Do you have any ideas?

My code is
Code: Select all
Row row = Context.getsmlObjectFactory().createRow();
            Cell cell = Context.getsmlObjectFactory().createCell();
            cell.setV(value);
            row.setOutlineLevel(outlineLevel);
            row.getC().add(cell);

Re: Cells with character values

PostPosted: Mon Oct 03, 2011 7:47 pm
by MaSEL
I understood the problem. It looks like the string values should be stored as Shared Strings in the sharedStrings.xml.
And the value in the
Code: Select all
cell.setV(value);
should be the index of the string in this file.
But I don`t know how to create shared strings by the code.

Re: Cells with character values

PostPosted: Mon Oct 03, 2011 8:44 pm
by MaSEL
I got it, guys.
Thanks to arimmer posts in the http://www.docx4java.org/forums/xlsx-java-f15/adding-style-information-crashes-excel-t647.html.
The code is something like this:

Code: Select all
WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
                SharedStrings sharedStrings = null;
                Parts parts = pkg.getParts();
                Part workBook = parts.get( new PartName("/xl/workbook.xml") );
                    sharedStrings = new SharedStrings(new PartName("/xl/sharedStrings.xml"));
                    ctst = new CTSst();
//...
//filling cells data, something like this:
            CTRst crt = new CTRst();
            crt.setT(value);
            ctst.getSi().add(crt);
            sharedStringsCount++;
            cell.setT(STCellType.S);
            cell.setV(String.valueOf(sharedStringsCount));
//...
ctst.setCount((long) sharedStringsCount);
                ctst.setUniqueCount((long) sharedStringsCount);
                sharedStrings.setJaxbElement(ctst);
                workBook.addTargetPart( sharedStrings );
                   

Re: Cells with character values

PostPosted: Sat Apr 21, 2012 7:21 am
by herolsen
Hello MaSel --

Thank you very much for your post as it helped me sort out my issue getting strings to 'stick' in xlsx4j. I came up with a somewhat different solution (as I am too lazy to count string references) by using inline strings -- hopefully it will help the next person struggling with this cryptic format. In reference to your original post:

Code: Select all
ctrst = new CTRst();
ctrst.setT(value);

Row row = Context.getsmlObjectFactory().createRow();
Cell cell = Context.getsmlObjectFactory().createCell();
cell.setT(STCellType.INLINE_STR);
cell.setIs(value); // add ctrst as inline string
row.getC().add(cell);

Re: Cells with character values

PostPosted: Sat Apr 21, 2012 7:24 am
by herolsen
Correction:

--> cell.setIs(ctrst); // add ctrst as inline string

Re: Cells with character values

PostPosted: Fri Jul 26, 2013 11:15 pm
by juse
Hi,
I get this same error from Excel whenever I write a string into a cell, could you please post some working code?
I've tried the one you posted in this topic but it doesn't work either.

org.xlsx4j.sml.Cell cell6 = Context.getsmlObjectFactory().createCell();

CTRst ctrst = new CTRst();
ctrst.setT(statRecord.getTitle());

cell6.setT(STCellType.INLINE_STR);
cell6.setIs(ctrst);


Is it possible one has to get crazy with this library just to get to write a simple string??
Many thanks!

Re: Cells with character values

PostPosted: Sat Jul 27, 2013 1:08 pm
by jason

Re: Cells with character values

PostPosted: Tue Jul 30, 2013 7:55 pm
by juse
Hi Jason,
thanks for your reply, I couldn't reply earlier as I'm testing the code just today.

I get an error for class CTXstringWhitespace, which is not present in any library imported.
Could you please tell me where I can find the needed JAR?

Many thanks

Re: Cells with character values

PostPosted: Tue Jul 30, 2013 9:31 pm
by jason
Oh, sorry, that is 3.0 snapshot code, so please use any of the nightly builds at http://www.docx4java.org/docx4j/

The most recent ones require:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

instead of log4j.

Re: Cells with character values

PostPosted: Tue Jul 30, 2013 10:30 pm
by juse
I've added the XML configuration to the JBoss pom.xml file, is it that correct? Sorry I don't know much of MAVEN.

I've also downloaded version 3 of DOCX4J with all its dependecies, and it compiles correctly now, I'm using your exact code.
But the XLSX file is still corrupted at opening (I've attached it).

The string I try to write to the Excel file is very simple.

Am I missing some other step?

Many thanks

Re: Cells with character values

PostPosted: Wed Jul 31, 2013 2:41 am
by juse
I think now it works, maybe there was one incorrect istruction such as:

Context.getsmlObjectFactory();

I guess I have taken it from some example on the web.

Thanks very much for your help!