Page 1 of 1

How to set the row height

PostPosted: Fri Jun 22, 2012 1:00 am
by lvdpal
I tried to change the default row height of my spread sheet, but for some reason I can't get it to work. I must be missing something, but I don't know what.

I've tried setting the default height, and I've tried setting the height of the rows itself, as that was what I saw in the xml of the file I made by hand, but neither seems to have any effect.

Code: Select all
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
      
WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/sheet1.xml"), "Sheet1", 1);
      
CTSheetFormatPr format = Context.getsmlObjectFactory().createCTSheetFormatPr();
format.setDefaultRowHeight(16.8);
format.setCustomHeight(Boolean.TRUE);
sheet.getJaxbElement().setSheetFormatPr(format);
      
SheetData sheetData = sheet.getJaxbElement().getSheetData();
            
Row row = Context.getsmlObjectFactory().createRow();
      
row.setHt(16.8);
row.setCustomHeight(Boolean.TRUE);
row.setR(1L);
      
Cell cell1 = Context.getsmlObjectFactory().createCell();
cell1.setV("1234");
row.getC().add(cell1);
      
Cell cell2 = Context.getsmlObjectFactory().createCell();
cell2.setV("56");
row.getC().add(cell2);
sheetData.getRow().add(row);
            
SaveToZipFile saver = new SaveToZipFile(pkg);
saver.save("C:\\sheet10.xlsx");

Re: How to set the row height

PostPosted: Fri Jun 22, 2012 6:39 pm
by jason
Actually, your code works fine; its just that you didn't notice with the small departure from the default height :-)

To see, try changing to:

Code: Select all
      format.setDefaultRowHeight(5);

      row.setHt(66.0);

Re: How to set the row height

PostPosted: Mon Jun 25, 2012 5:20 pm
by lvdpal
Hmm, that's really weird, because if I drag the columns to the right hight in Excel by hand, I do see the difference. (And it results in the same XML even!) But if I open the generated file the row height is the default 13.2 and if I open the manually altered Excel file, I see the right height of 16.8.

So now I'm left to wonder why you can use exact numbers manually, but not when generating a file. I've also noticed this when setting the column widths, it's never the exact size I specified. If I set the height to 66.8 I see a height of 53.4. Really strange.

Anyway, thanks for the help, this really had me stumped. To fix it I'll just play around with the height until I do get the one I want.