Как создать таблицу TableStyleMedium9, которая динамически обновляется в apache POI - PullRequest
0 голосов
/ 27 апреля 2020

Поскольку я новичок в apache poi, я попытался создать таблицу TableStyleMedium9, которая полностью соответствует моим требованиям. Ниже приведен код, который я попробовал.

    PharmacyReturnsPDFProxy proxy=segregationStoreWarehouse(session,usersession,pg);
        /* Start with Creating a workbook and worksheet object */

  Workbook wb= ExcelUtil.getAbstractWorkBook(usersession, proxy);
  XSSFSheet sheet= (XSSFSheet) wb.getSheet("Sheet0");

     String gap="            ";
    Row row6=sheet.createRow(6);
    String cellValue="\nDate:"+proxy.getStartDate()+"to"+proxy.getEndDate();
    if (proxy.getMultiLocationFlag().equals(Boolean.TRUE)){
        cellValue=cellValue+gap+"Total Refund Amount For All Locations"+proxy.getTotalRefundedAmountforAllLocations();
    }
    row6.createCell(1).setCellValue(cellValue);
    int tb=10;
    int valueIndex=0;
    for(PharmacyTotalAmountsProxy parentList:proxy.getFinalList()){



         /* Create an object of type XSSFTable */
        XSSFTable my_table =  sheet.createTable(null);

            /* get CTTable object*/
        CTTable cttable = my_table.getCTTable();

        /* Let us define the required Style for the table */    
        CTTableStyleInfo table_style = cttable.addNewTableStyleInfo();
        table_style.setName("TableStyleMedium9");   

            /* Set Table Style Options */
        table_style.setShowColumnStripes(false); //showColumnStripes=0
        table_style.setShowRowStripes(true); //showRowStripes=1

        /* Define the data range including headers */
        AreaReference my_data_range = new AreaReference(new CellReference(0, 0), new CellReference(4,2),null);

        /* Set Range to the Table */
            cttable.setRef(my_data_range.formatAsString());
            cttable.setDisplayName("MYTABLE");      /* this is the display name of the table */
        cttable.setName("Test");    /* This maps to "displayName" attribute in <table>, OOXML */            
        cttable.setId(1L); //id attribute against table as long value



            /* Define Header Information for the Table */
        int ColumnCount =8;
        Boolean muser=proxy.getMultiUserFalg();
        if(muser.equals(Boolean.TRUE)){
         ColumnCount =9;
        }
        CTTableColumns columns = cttable.addNewTableColumns();
        columns.setCount(ColumnCount); //define number of columns
        for (int i = 0; i < ColumnCount; i++)
        {
        CTTableColumn column = columns.addNewTableColumn();   
        column.setName("Column" + i);      
            column.setId(i+1);
        }
        //heading-row
        XSSFRow rowH = (XSSFRow) sheet.createRow(tb);          
        rowH.createCell(0).setCellValue(ExcelUtil.INVOICE_NUMBER);
        rowH.createCell(1).setCellValue(ExcelUtil.RETURN_RECIEPT);
        rowH.createCell(2).setCellValue(ExcelUtil.REFUNDED_DATE);
        rowH.createCell(3).setCellValue(ExcelUtil.CUETOMER_PATIENT);
        rowH.createCell(4).setCellValue(ExcelUtil.INVOICE_AMOUNT);
        rowH.createCell(5).setCellValue(ExcelUtil.CASH_REFUNDED);
        rowH.createCell(6).setCellValue(ExcelUtil.CARD_REFUNDED);
        rowH.createCell(7).setCellValue(ExcelUtil.TOTAL_AMOUNT);
        if(muser.equals(Boolean.TRUE)){
            rowH.createCell(8).setCellValue(ExcelUtil.USER);
        }

             /* Add remaining Table Data */
             for (int i=tb+1;i<=parentList.getPharmacyReturnslist().size()+tb;i++) //we have to populate 4 rows
             {
             /* Create a Row */
         XSSFRow row = (XSSFRow) sheet.createRow(i);
         PharmacyReturnListProxy values =parentList.getPharmacyReturnslist().get(valueIndex);          
         row.createCell(0).setCellValue(values.getReceiptNo());
         row.createCell(1).setCellValue(values.getInvoiceNo());
         row.createCell(2).setCellValue(values.getRefundedDate());
         row.createCell(3).setCellValue(values.getPatientName());
         row.createCell(4).setCellValue(values.getInvoiceAmt());
         row.createCell(5).setCellValue(values.getCash());
         row.createCell(6).setCellValue(values.getCard());
         row.createCell(7).setCellValue(values.getTotalAmt());
        if(muser.equals(Boolean.TRUE)){
            rowH.createCell(8).setCellValue(values.getUser());
        }
       valueIndex++;

             }

    }




   /* Write output as File */
    FileOutputStream fileOut = new FileOutputStream("EXCELTESTER.xlsx");
    wb.write(fileOut);
    fileOut.close();
    return true;

Поскольку моя таблица обновляется динамически, я не получаю таблицу TableStyleMedium9, как ожидалось. Я ожидал получить результат, как показано ниже

ожидаемый результат: enter image description here

Надеюсь, кто-нибудь может мне помочь.

...