创建Excel XSSFWorkbook文档时创建排序顺序

时间:2018-09-12 14:01:53

标签: excel worksheet

我正在用Java创建一个Excel电子表格,如下所示。样式或工作表对象中是否有一个选项,我们可以在创建过程中指定列的排序顺序,而不是在Excel中进行选择?我想按第四列进行排序。

    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("Account by Acct Event-PC");

    Font headerFont = workbook.createFont();
    headerFont.setBold(true);

           CellStyle style = workbook.createCellStyle();
           style.setWrapText(true);
           Font font = workbook.createFont();
           font.setBoldweight(Font.BOLDWEIGHT_BOLD);
           style.setFont(font);
           style.setAlignment(CellStyle.ALIGN_CENTER);
           style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
           style.setFillPattern(CellStyle.SOLID_FOREGROUND);   

    //header row
    Row headerRow = sheet.createRow(0);
    for(int i = 0; i < HEADER_FIELDS.length; i++) {
        Cell cell = headerRow.createCell(i);
        cell.setCellValue(HEADER_FIELDS[i]);
        cell.setCellStyle(style);
    }

    //data rows
    for(int i = 1; i <= tableData.getRecords().size(); i++) {
        Row row = sheet.createRow(i);
        A8SPT138 record = (A8SPT138) tableData.getRecords().get(i-1);

        row.createCell(0).setCellValue(record.getAccountingEvent());
                    row.createCell(1).setCellValue(record.getA8spt002().getDescription()) ;
        row.createCell(2).setCellValue(record.getPostCode());
                    row.createCell(3).setCellValue(record.getA8spt143().getDescription()) ;
        row.createCell(4).setCellValue(record.getAcqCode());
        row.createCell(5).setCellValue(record.getA8spt004().getDescription());
        row.createCell(6).setCellValue(record.getChartOfAccountI());
        row.createCell(7).setCellValue(SamsConstants.JSP_FORMAT.format(SamsConstants.DATE_UPDATED_FORMAT
                .parse(record.getDateUpdated())));
        row.createCell(8).setCellValue(record.getUpdatedBy());
    }

再次感谢。

0 个答案:

没有答案