We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

The table has no background color

I want the first row or the first column of the table to be colored。This is my data and my code
image.png (49.2 KB)
image.png (35.9 KB)
image.png (98.2 KB)

 private static void writeTable(DocumentBuilder documentBuilder, List<List<String>> list, TitleDirection titleDirection) throws Exception {

    Table table = documentBuilder.startTable();
    documentBuilder.getCellFormat().setFitText(true);
    documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    documentBuilder.getCellFormat().clearFormatting();
    documentBuilder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
    for (int row = 0; row < list.size(); row++) {
        for (int colum = 0; colum < list.get(row).size(); colum++) {
            if (titleDirection == TitleDirection.TRANSVERSE){
                if(row == 0){
                     documentBuilder.getCellFormat().getShading().setBackgroundPatternColor(Color.getHSBColor(189, 214, 229));
                }
                documentBuilder.insertCell();
                documentBuilder.write(list.get(row).get(colum));
                if(row == 0){
                    documentBuilder.getCellFormat().getShading().clearFormatting();
                }
            }
            if (titleDirection == TitleDirection.PORTRAIT){
                if(colum == 0){
                    documentBuilder.getCellFormat().getShading().setBackgroundPatternColor(Color.getHSBColor(189, 214, 229));
                }
                documentBuilder.insertCell();
                documentBuilder.write(list.get(row).get(colum));
                if(colum == 0){
                    documentBuilder.getCellFormat().getShading().clearFormatting();
                }
            }
        }
        documentBuilder.endRow();
    }
    documentBuilder.endTable();
    documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
}

@860534 The problem occurs because in your code you are clearing background of the current cell, i.e. you set background and then after putting cell content immediately clear its background. I have modified your code and now it should work as expected.

private static void writeTable(DocumentBuilder documentBuilder, List<List<String>> list, TitleDirection titleDirection) throws Exception {

    Table table = documentBuilder.startTable();
    documentBuilder.getCellFormat().setFitText(true);
    documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    documentBuilder.getCellFormat().clearFormatting();
    documentBuilder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
    for (int row = 0; row < list.size(); row++) {
        for (int colum = 0; colum < list.get(row).size(); colum++) {
            documentBuilder.insertCell();
            documentBuilder.write(list.get(row).get(colum));
            if ((titleDirection == TitleDirection.TRANSVERSE)&& (row == 0)){
                documentBuilder.getCellFormat().getShading().setBackgroundPatternColor(Color.getHSBColor(189, 214, 229));
            }
            else if ((titleDirection == TitleDirection.PORTRAIT) && (colum == 0)){
                documentBuilder.getCellFormat().getShading().setBackgroundPatternColor(Color.getHSBColor(189, 214, 229));
            }
            else  {
                documentBuilder.getCellFormat().getShading().clearFormatting();
            }
        }
        documentBuilder.endRow();
    }
    documentBuilder.endTable();
    documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
}

Thank you, I mistakenly thought that coloring should come before writing data