Free Support Forum - aspose.com

Excel crashing/text formatting oddly when editting cells

I’ve encountered an issue when running through my code where the Excel report seems to stop responding or the text changes oddly when I attempt to edit cells. Attached to this post is a report with the issue where it contains one cell. If you attempt to highlight the cell via the formula bar and remove the italic, sometimes Excel crashes or sometimes it would remove italic but also remove the font colour except the first character.

I wrote some test code to identify the problem and I think it might have something to do with the individual character formatting where it turns the letters green. Although it doesn’t crash, if you run the code, remove the italics and then reapply it, Excel goes into Not Responding. I will continute to investigate if I can replicate Excel crashing.

Workbook wb = new Workbook();
Worksheet ws = wb.getWorksheets().get(0);

ws.getCells().setColumnWidthPixel(1, 250);
ws.getCells().setRowHeightPixel(1, 150);
ws.getCells().merge(1, 1, 1, 1);

Cell cell = ws.getCells().get(1, 1);
Style style = cell.getStyle();

StringBuilder out = new StringBuilder();
out.append(“2”);
out.append("%");
out.append(" ");
out.append(“B”);
out.append(“l”);
out.append(“a”);
out.append(“r”);
out.append(“g”);
String text = out.toString();
cell.putValue(text);

style.setBackgroundColor(Color.getTransparent());
style.getBorders().getByBorderType(BorderType.TOP_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.TOP_BORDER).setColor(Color.getBlack());
style.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).setColor(Color.getBlack());
style.getBorders().getByBorderType(BorderType.LEFT_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.LEFT_BORDER).setColor(Color.getBlack());
style.getBorders().getByBorderType(BorderType.RIGHT_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.RIGHT_BORDER).setColor(Color.getBlack());

style.setTextWrapped(true);
style.setVerticalAlignment(TextAlignmentType.TOP);
style.setHorizontalAlignment(TextAlignmentType.LEFT);

cell.characters(0, 8).getFont().setName(“Comic Sans MS”);
cell.characters(0, 8).getFont().setSize(28);
cell.characters(0, 8).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 204, 255).getRGB()));
cell.characters(0, 8).getFont().setBold(false);
cell.characters(0, 8).getFont().setItalic(true);
cell.characters(0, 8).getFont().setUnderline(FontUnderlineType.NONE);

cell.characters(3, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));
cell.characters(4, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));
cell.characters(5, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));
cell.characters(6, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));

cell.setStyle(style);

ws.setGridlinesVisible(false);
ws.getPageSetup().setCenterHorizontally(true);
ws.getPageSetup().setCenterVertically(true);
ws.getPageSetup().setFooterMargin(0);
ws.getPageSetup().setHeaderMargin(0);
ws.getPageSetup().setTopMargin(0);
ws.getPageSetup().setBottomMargin(0);
ws.getPageSetup().setLeftMargin(0);
ws.getPageSetup().setRightMargin(0);


Hi,


Thanks for providing some details and sample code.

I have used your sample code to generate the Excel file. I can notice the issue of Excel crashing and text formatting oddly in MS Excel 2007 but not all the time. Sometimes, I got the issue e.g If you attempt to highlight the cell via the formula bar and remove the italic, sometimes Excel crashes or sometimes it would remove italic but also remove the font color except the first character while sometimes.

I have logged a ticket with an id “CELLSJAVA-40478”. We will look into your issue soon.

Once we have any update on it, we will let you know here.

Thank you.

Hi,

Thanks for your posting and using Aspose.Cells.

MS Excel try to merge FontSettings if contiguous characters contains the same font. Sometimes MS Excel fails.It should be the strange behavior of MS Excel.

Please change your codes:


cell.characters(3, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));

cell.characters(4, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));

cell.characters(5, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));

cell.characters(6, 1).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));

as

cell.characters(3, 4).getFont().setColor(Color.fromArgb(new java.awt.Color(0, 255, 0).getRGB()));


Thank you Shakeel for your assistance. The test code was written in a way of how it would eventually come out as each character gets checked over for any kind of font control individually, running through multiple methods and classes. However, I have done some restructuring and managed to get the outcome you mentioned, which seemed to have stopped the issue. If any further issues pop up, I will let you know.

Thank you once again.

Hi,


Good to know that your issue is sorted out by replacing the suggested line.

Feel free to contact us any time if you need further help or have any other issue, we will be happy to assist you soon.

thank you.